-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep SVG content visible when zooming #37
Comments
Here's one way of getting this done. self.gameWidth and self.gameHeight are the size of the SVG, and the gutter leaves a bit of space to the sides.
|
Cool! I'll add this when I get a minute. Thanks. On Sun, May 25, 2014 at 7:13 AM, James Lefrère notifications@git.luolix.topwrote:
|
Hey guys! Thanks a lot for an impressive library! |
Thanks - glad you find it useful! I haven't actually added the code above from James to the library yet. Are you using James's solution to keep the content visible? |
@JamesLefrere and @bumbu, finally getting to this and wanted to make sure we're on the same page with what to do. My thoughts:
|
@ariutta I tried to use James's solution but my SVG files use negative coordinates. Also, I am using the whole body element of the DOM as a container ( similar to how google maps look like) . What I tried to do is obtaining the window width and height values before initializing the svg-pan-zoom object and updating those values when the window is resized. I do so in order to calculate the gutter for a resizable window so that at least 1/4 of the SVG is always visible. |
@ariutta: I had the same in mind. But there are 2 things that I would expose to API:
But now if @ledancs says that he uses a 1/4 of page as gutter maybe it will be a good idea to allow gutter value to be number or a function? About |
@bumbu, agreed -- ability to disable and to set custom gutter values would be good to include in the API. For specifying gutter, I think a function might be more complex than is needed. What about if the acceptable inputs are CSS lengths like @ledancs, are you using |
@ariutta, I don't see why a function would be too complex if used in pair with numbers.. I just have a feeling that as some point somebody will ask for ability to customize this parameter. By allowing a number of a function we can cover most use-cases:
|
I needed to prevent the user from panning away from the SVG viewport. What I did was check to see if the user has panned farther than the current dimensions of the SVG viewport. This is probably not the most optimal setup and the pan event handler should probably be throttled, but you'll get the idea:
I'm checking the current size of the SVG element (using getBBox to account for zoom) and then checking to see if the user has panned farther than the width/height of the SVG canvas. If they have, I call pan() manually to reset the position to the closest limit. It's pretty smooth on Chrome but I'm not sure about other browsers. I'd personally rather cancel the pan event than call pan() manually, but I don't know if thats possible. I still have to write a handler for zooming to re-pan the SVG if the user zooms out while near an edge. If you zoom in and pan to an edge, then zoom out, you can see outside of the SVG canvas. |
Actually, I just figured out a better way to do it that also keeps the canvas in view while zooming:
I'm interested to hear any opinions on the performance implications of this approach. |
@bumbu, I don't know of a case where a user would need a function, but I'm OK with having it as an option in the API if you want to implement it. |
Thanks, @abecks. I'll take a closer look at this. |
I added possibility to cancel panning and zooming from inside of Callbacks have access to the public API object through I set up an example in This approach allows many customizations (such as panning in steps requested in #71) while keeping the codebase clean. Right now changes are in dev branch |
Can I suggest a better option is changing beforePan to return an {x, y} object, the same as is passed in. That makes it much more flexible. For instance, to cancel the pan just return "oldPan". To accept the pan as requested, return "newPan". To clip at the edges, the X and Y values can be limited to the edges if they're scrolled beyond - for example, an oversimplified example showing how to prevent the top-left scrolling out of view: beforePan(oldpan, newpan) { The reason for taking this approach is if I grab the SVG and do a great swipe left/right: if I return a boolean I can only cancel the pan, but if I return x and y I can accept the pan but limit it to the bounds of the image. Edit: In fact, to really improve things, add a "z" parameter for zoom, and roll all of the minzoom/maxzoom/pan limiting functionality into one simple function. |
Hi @faceless2, It sounds like a reasonable proposal. I was thinking about something like this but I wasn't sure if it is a good approach. The idea sounds promising as it may allow to do such things as pan sensitivity or panning by the limit when user tried to pan over limit. But at the same time it may be a trap as pan events are computed relatively to pan starting point and they do not take in account pan canceling or any modifications at beforePan time. What do you mean by
Thanks for your feedback. |
Currently you have minZoom and maxZoom variables, you have a beforePan method and a beforeZoom method. It occurs to me that you could replace all of these with a single "beforeChange" method that takes before and after values of x, y and zoom, and returns an object with the same set of values. You could use this to limit zoom levels or to adjust pan position after a zoom. Why do this? If I have scrolled to the far right of the SVG bounding box and zoom out, the left and top edge of my image are fixed, which means I get white space to the right when I zoom out. Ideally I would like to pan the image as well at this point to ensure the viewBox is always respected. An example. This will (well, should - untested) limit the panned area to the viewbox, and limit zoom to between 1 and 4. I'm assuming the SVG is >= in size to the width/height available, which I can do because it's just an example :-) function(op, np) {
var s = this.getSizes();
var x, y, zoom;
zoom = Math.max(1, Math.min(4, np.z));
x = Math.min(np.x, Math.max(s.viewBox.x, op.x));
x = Math.max(x, Math.min(op.x, s.width - ((s.viewBox.x + s.viewBox.width) * zoom)));
y = Math.min(np.y, Math.max(s.viewBox.y, op.y));
y = Math.max(y, Math.min(op.y, s.height - ((s.viewBox.y + s.viewBox.height) * zoom)));
return { x:x, y:y, z:zoom };
}, |
I added ability to alter pan from beforePan callback.
If this is still a wanted feature then please open a new issue for this. Thanks |
I made it work to limit the pan using the demo limit-pan.html but this seems to be different to the solution with a callback. Could you explain how one could use the beforePan / oldPan method to limit panning? Best, hirschferkel |
@hirschferkel you can find more description on home page. Look for the description about |
@bumbu sorry I had only a sketchy look at it when I asked for help. Initially I used the method already... thank's for your help. |
Right now, it's possible to zoom into a blank area outside the bounding box of the SVG content. The view should never entirely leave the bounding box of the SVG content.
The text was updated successfully, but these errors were encountered: