A library for panning and zooming elements using CSS transforms :mag:
A library for panning and zooming elements using CSS transforms :mag:
Panzoom is a small library (~3.7kb gzipped) to add panning and zooming functionality to an element. Rather than using absolute positioning or setting width and height, Panzoom uses CSS transforms to take advantage of hardware/GPU acceleration in the browser, which means the element can be anything: an image, a video, an iframe, a canvas, text, WHATEVER.
For common support questions, see the FAQ.
Here is a list of currently supported browsers.
iOS, Android, and Windows Mobile are supported.
Panzoom includes support for touch gestures and even supports pinch gestures for zooming. It is perfectly suited for both mobile and desktop browsers. It uses pointer events by default wherever supported.
Panzoom supports panning and zooming SVG elements directly.
In IE11, CSS animations/transitions do not work on SVG elements, at least for the transform style. They do work in other browsers.
One could implement transitions manually in IE11 using the setTransform option and integrating a tweening library for javascript animations (such as tween.js).
With npm:
$ npm install --save @panzoom/panzoomWith yarn:
$ yarn add @panzoom/panzoomPanzoom uses UMD and can be loaded a lot of ways.
With ES6 imports:
import Panzoom from '@panzoom/panzoom'With commonjs or browserify:
const Panzoom = require('@panzoom/panzoom')With an AMD loader in an anonymous module:
define(['@panzoom/panzoom'], function (Panzoom) {
const elem = document.getElementById('panzoom-element')
Panzoom(elem)
})With a script tag:
<script src="/js/panzoom.js"></script>With a script tag from a CDN:
<script src="https://unpkg.com/@panzoom/[email protected]/dist/panzoom.min.js"></script>const elem = document.getElementById('panzoom-element')
const panzoom = Panzoom(elem, {
maxScale: 5
})
panzoom.pan(10, 10)
panzoom.zoom(2, { animate: true })
// Panning and pinch zooming are bound automatically (unless disablePan is true).
// There are several available methods for zooming
// that can be bound on button clicks or mousewheel.
button.addEventListener('click', panzoom.zoomIn)
elem.parentElement.addEventListener('wheel', panzoom.zoomWithWheel)1. What is transform-origin and why is it added to the panzoom element?
transform-origin is the origin from which transforms are applied. Panzoom ensures the defaults are set to what it expects to calculate focal point zooming.2. I am using Panzoom with an <object> tag and it's not working. What's wrong?
Object elements can eat up events, making it so they never reach Panzoom. To fix this, disable pointer events (pointer-events: none) on the <object> tag and call Panzoom using a wrapper.
3. My links aren't working! How do I enable an anchor within a panzoom element?
Add class options.excludeClass (default is "panzoom-exclude") to whatever element you want to be clickable. Panzoom will check for this class before handling the event.
Alternatively, add a reference to the element to the exclude option, or call event.stopImmediatePropagation() in an event handler on the clickable element.
In some cases, setting one thing and then setting another synchronously will not work as intended.
For instance, the following usually works fine.
const panzoom = Panzoom(elem)
panzoom.zoom(2)
panzoom.pan(100, 100)However, you might find that the things start breaking when the contain option is set.
This is due to the fact that in order for Panzoom to retrieve proper dimensions, the scale needs to be painted.
If you find that things aren't looking quite right, try the following instead...
panzoom.zoom(2)
setTimeout(() => panzoom.pan(100, 100))4. I'm using Panzoom with SVG text elements and am seeing some weird text resizing. How do I fix this?
Add text-rendering="geometricPrecision" to your <text> elements.
<text text-rendering="geometricPrecision" x="40" y="120">Hello World</text>5. I'm using Panzoom on a canvas element that renders a PDF. How do I avoid the PDF getting blurry when scaled?
See this stackoverflow question
Panzoom(
elem,options?):[PanzoomObject](#PanzoomObject)
Defined in: panzoom.ts:60
HTMLElement | SVGElement
[PanzoomGlobalOptions](#PanzoomGlobalOptions)
[PanzoomObject](#PanzoomObject)
PanzoomOptionsIncludes MiscOptions, PanOptions, and ZoomOptions
PanzoomGlobalOptionsIdentical to PanzoomOptions, but excludes the force option.
These options can be passed to Panzoom(), as well as any pan or zoom function. One exception is force, which can only be passed to methods like pan() or zoom(), but not Panzoom() or setOptions() as it should not be set globally.
Defined in: types.ts:19
[
key:string]:any
Pass through any options like data
optionalanimate?:boolean
Defined in: types.ts:21
Whether to animate transitions
optionalcanvas?:boolean
Defined in: types.ts:32
This option treats the Panzoom element's parent as a canvas. Effectively, Panzoom binds the down handler to the parent instead of the Panzoom element, so that pointer events anywhere on the "canvas" moves its children. See issue #472.
Note: setting this option to true also changes
where the cursor style is applied (i.e. the parent).
optionalduration?:number
Defined in: types.ts:34
Duration of the transition (ms)
optionaleasing?:string
Defined in: types.ts:36
CSS Easing used for transitions
optionalexclude?:Element[]
Defined in: types.ts:43
Add elements to this array that should be excluded from Panzoom handling. Ancestors of event targets are also checked. e.g. links and buttons that should not propagate the click event.
optionalexcludeClass?:string
Defined in: types.ts:50
Add this class to any element within the Panzoom element that you want to exclude from Panzoom handling. That element's children will also be excluded. e.g. links and buttons that should not propagate the click event.
optionalforce?:boolean
Defined in: types.ts:67
The force option can only be passed directly
to zoom and pan methods. It is not a global option
and will be ignored if passed to Panzoom() or setOptions().
force should be used sparingly to temporarily
override and ignore options such as disablePan,
disableZoom, and panOnlyWhenZoomed.
// Overrides disablePan and panOnlyWhenZoomed
panzoom.pan(50, 100, { force: true })
// Overrides disableZoom
panzoom.zoom(1, { force: true })
optionalhandleStartEvent?: (event) =>void
Defined in: types.ts:92
On the first pointer event, when panning starts,
the default Panzoom behavior is to call
event.preventDefault() and event.stopPropagation()
on that event. The former is almost certainly a necessity;
the latter enables Panzoom elements within Panzoom elements.
But there are some cases where the default is not the desired behavior. Set this option to override that behavior.
// Only call preventDefault()
Panzoom(elem, {
handleStartEvent: (event) => {
event.preventDefault()
}
})
// Do nothing.
// This can change dragging behavior on mobile.
Panzoom(elem, {
handleStartEvent: () => {}
})Event
void
optionalnoBind?:boolean
Defined in: types.ts:96
Skip binding the default Panzoom event listeners
optionalorigin?:string
Defined in: types.ts:110
Change this at your own risk.
The transform-origin is the origin from which transforms are applied.
Default: '50% 50%' for HTML and '0 0' for SVG.
The defaults are set because changing the transform-origin on
SVG elements doesn't work in IE.
Changing this should work with many things, but it will break focal point zooming, which assumes the defaults are set to do the more complicated calculations.
And again, changing this for SVG in IE doesn't work at all.
optionaloverflow?:string
Defined in: types.ts:112
The overflow CSS value for the parent. Defaults to 'hidden'
optionalpinchAndPan?:boolean
Defined in: types.ts:125
Set to true to enable panning during pinch zoom. Note: this is zooming to a point and panning in the same frame. In other words, the zoom has not yet painted and therefore the pan is working with old dimensions. Essentially, it may be best to avoid using this option when using contain.
Related issues: https://github.com/timmywil/panzoom/issues/512 https://github.com/timmywil/panzoom/issues/606
optionalsetTransform?: (elem,__namedParameters,_options?) =>void
Defined in: types.ts:129
Set the transform using the proper prefix.
Set the transform using the proper prefix
Override the transform setter. This is exposed mostly so the user could set other parts of a transform aside from scale and translate. Default is defined in src/css.ts.
// This example always sets a rotation
// when setting the scale and translation
const panzoom = Panzoom(elem, {
setTransform: (elem, { scale, x, y }) => {
panzoom.setStyle('transform', `rotate(0.5turn) scale(${scale}) translate(${x}px, ${y}px)`)
}
})HTMLElement | SVGElement
CurrentValues
PanzoomOptions
void
No open issues yet, or sync has not completed.