panzoom
Extensible, mobile friendly pan and zoom framework (supports DOM and SVG).
Demo
Usage
Grab it from npm and use with your favorite bundler:
npm install panzoom --save
Or download from CDN:
<script src='https://unpkg.com/panzoom@9.2.4/dist/panzoom.min.js'></script>
If you download from CDN the library will be available under panzoom
global name.
Pan and zoom DOM subtree
var element = document.querySelector('#scene')
panzoom(element)
SVG panzoom example
<body>
<svg>
<g id='scene'>
<circle cx='10' cy='10' r='5' fill='pink'></circle>
</g>
</svg>
</body>
var element = document.getElementById('scene')
panzoom(element)
If your use case requires dynamic behavior (i.e. you want to make a element
not
draggable anymore, or even completely delete an SVG element) make sure to call
dispose()
method:
var instance = panzoom(element)
instance.dispose()
This will make sure that all event handlers are cleared and you are not leaking
memory
Events notification
The library allows to subscribe to transformation changing events. E.g. when
user starts/ends dragging the element
, the element
will fire panstart
/panend
events. Here is example of all supported events:
var instance = panzoom(element);
instance.on('panstart', function(e) {
console.log('Fired when pan is just started ', e);
});
instance.on('pan', function(e) {
console.log('Fired when the `element` is being panned', e);
});
instance.on('panend', function(e) {
console.log('Fired when pan ended', e);
});
instance.on('zoom', function(e) {
console.log('Fired when `element` is zoomed', e);
});
instance.on('zoomend', function(e) {
console.log('Fired when zoom animation ended', e);
});
instance.on('transform', function(e) {
console.log('Fired when any transformation has happened', e);
});
See JSFiddle console for a demo.
Ignore mouse wheel
Sometimes zooming interferes with scrolling. If you want to alleviate it you
can provide a custom filter, which will allow zooming only when modifier key is
down. E.g.
panzoom(element, {
beforeWheel: function(e) {
var shouldIgnore = !e.altKey;
return shouldIgnore;
}
});
See JSFiddle for the demo. The tiger will be
zoomable only when Alt
key is down.
Ignore mouse down
If you want to disable panning or filter it by pressing a specific key, use the
beforeMouseDown()
option. E.g.
panzoom(element, {
beforeMouseDown: function(e) {
var shouldIgnore = !e.altKey;
return shouldIgnore;
}
});
Ignore keyboard events
By default, panzoom will listen to keyboard events, so that users can navigate the scene
with arrow keys and +
, -
signs to zoom out. If you don't want this behavior you can
pass the filterKey()
predicate that returns truthy value to prevent panzoom's default
behavior:
panzoom(element, {
filterKey: function() {
return true;
}
});
Zoom Speed
You can adjust how fast it zooms, by passing optional zoomSpeed
argument:
panzoom(element, {
zoomSpeed: 0.065
});
Pinch Speed
On touch devices zoom is achieved by "pinching" and depends on distance between
two fingers. We try to match the zoom speed with pinch, but if you find
that too slow (or fast), you can adjust it:
panzoom(element, {
pinchSpeed: 2
});
Get current transform (scale, offset)
To get the current zoom (scale) level use the getTransform()
method:
console.log(panzoom.getTransform()); // prints {scale: 1.2, x: 10, y: 10}
Fixed transform origin when zooming
By default when you use mouse wheel or pinch to zoom, panzoom
uses mouse
coordinates to determine the central point of the zooming operation.
If you want to override this behavior and always zoom into center
of the
screen pass transformOrigin
to the options:
panzoom(element, {
transformOrigin: {x: 0.5, y: 0.5}
});
You specify transformOrigin
as a pair of {x, y}
coordinates. Here are some examples:
let topLeft = {x: 0, y: 0};
let topRight = {x: 1, y: 0};
let bottomLeft = {x: 0, y: 1};
let bottomRight = {x: 1, y: 1};
let centerCenter = {x: 0.5, y: 0.5};
panZoom(element, {
transformOrigin: centerCenter
});
To get or set new transform origin use the following API:
let instance = panzoom(element, {
transformOrigin: {x: 0.5, y: 0.5}
});
let origin = instance.getTransformOrigin();
instance.setTransformOrigin({x: 0, y: 0});
instance.setTransformOrigin(null);
Min Max Zoom
You can set min and max zoom, by passing optional minZoom
and maxZoom
argument:
var instance = panzoom(element, {
maxZoom: 1,
minZoom: 0.1
});
You can later get the values using getMinZoom()
and getMaxZoom()
assert(instance.getMaxZoom() === 1);
assert(instance.getMinZoom() === 0.1);
Disable Smooth Scroll
You can disable smooth scroll, by passing optional smoothScroll
argument:
panzoom(element, {
smoothScroll: false
});
With this setting the momentum is disabled.
Pause/resume the panzoom
You can pause and resume the panzoom by calling the following methods:
var element = document.getElementById('scene');
var instance = panzoom(element);
instance.isPaused();
instance.pause();
instance.isPaused();
instance.resume();
instance.isPaused();
Script attachment
If you want to quickly play with panzoom without using javascript, you can configure it via
script
tag:
<!DOCTYPE html>
<html>
<head>
<script src='https://unpkg.com/panzoom@9.2.4/dist/panzoom.min.js'
query='#scene' name='pz'></script>
</head>
<body>
<svg>
<g id='scene'>
<circle cx='10' cy='10' r='5' fill='pink'></circle>
</g>
</svg>
</body>
</html>
Most importantly, you can see query
attribute that points to CSS selector. Once the element is found
panzoom is attached to this element. The controller will become available under window.pz
name. And you
can pass additional options to the panzoom via attributes prefixed with pz-
.
Here is a demo: Script based attributes
Adjust Double Click Zoom
You can adjust the double click zoom multiplier, by passing optional zoomDoubleClickSpeed
argument.
When double clicking, zoom is multiplied by zoomDoubleClickSpeed
, which means that a value of 1 will disable double click zoom completely.
panzoom(element, {
zoomDoubleClickSpeed: 1,
});
Set Initial Position And Zoom
You can set the initial position and zoom, by chaining the zoomAbs
function with x position, y position and zoom as arguments:
panzoom(element, {
maxZoom: 1,
minZoom: 0.1
}).zoomAbs(
300,
500,
0.1
);
Handling touch events
The library will handle ontouch
events very aggressively, it will preventDefault
, and
stopPropagation
for the touch events inside container. Sometimes this is not a desirable behavior.
If you want to take care about this yourself, you can pass onTouch
callback to the options object:
panzoom(element, {
onTouch: function(e) {
return false;
}
});
Note: if you don't preventDefault
yourself - make sure you test the page behavior on iOS devices.
Sometimes this may cause page to bounce undesirably.
Handling double click events
By default panzoom will prevent default action on double click events - this is done to avoid
accidental text selection (which is default browser action on double click). If you prefer to
allow default action, you can pass onDoubleClick()
callback to options. If this callback
returns false, then the library will not prevent default action:
panzoom(element, {
onDoubleClick: function(e) {
return false;
}
});
Bounds on Panzoom
By default panzoom will not prevent Image from Panning out of the Container. bounds
(boolean) and
boundsPadding
(number) can be defined so that it doesn't fall out. Default value for boundsPadding
is 0.05
.
panzoom(element, {
bounds: true,
boundsPadding: 0.1
});
Triggering Pan
To Pan the object using Javascript use moveTo(<number>,<number>)
function . It expects x, y value to where to move.
panzoom.moveTo(0, 0);
license
MIT