vael-ui v0.5.7

Drag & drop

usePanZoom

Pans, pinches and zooms content in a box: an image, a map, a diagram. Pan with a finger or the mouse, pinch with two fingers, double tap to zoom.

Pans, pinches and zooms content inside a box: an image, a map, a diagram. It moves the content with its own translate and scale, from its top left corner. One finger or the mouse pans, two fingers pinch, and a double tap zooms in and back out. Hold ctrl and scroll to zoom around the cursor; in Safari, a trackpad pinch does the same. Content stays inside the box, gives a little at the edges, and keeps coasting after a flick. It sets touch-action: none on the box, so the page does not scroll there on touch.

Install

import { usePanZoom } from 'vael-ui'

Examples

Parameters

NameTypeDescription
options.elMaybeRefOrGetter<HTMLElement | null>The box you pan and zoom in. It receives the pointer, the wheel and the focus.
options.contentMaybeRefOrGetter<HTMLElement | null>The element that moves. Default: the first child of el.
options.minScaleMaybeRefOrGetter<number>The smallest scale. Default 1, the content at its own size.
options.maxScaleMaybeRefOrGetter<number>The largest scale. Default 4.
options.doubleTapScaleMaybeRefOrGetter<number>The scale a double tap zooms to. Default 2.
options.disabledMaybeRefOrGetter<boolean>Ignores the pointer, the wheel and the keys.
options.springMaybeRefOrGetter<Spring | SpringName>The spring it settles on: a name or { damping, response }. Default: the ConfigProvider's spring, else the default every component uses.

Returns

NameTypeDescription
x, yRef<number>Where the top left of the content is, in px from where it sits at rest.
scaleRef<number>How big the content is. 1 is its own size.
isDraggingRef<boolean>Whether a pointer is panning or pinching.
isZoomedComputedRef<boolean>Whether it is zoomed in past minScale.
zoomTo(scale, { at?, animate? })(scale: number, options?) => voidZooms to scale around at, a point in px inside el. Default: its centre.
zoomBy(factor, { at?, animate? })(factor: number, options?) => voidZooms by a factor from where it is: 1.5 in, 1 / 1.5 out.
panBy({ x, y }, { animate? })(by: { x, y }, options?) => voidMoves by by px, kept inside the content.
reset({ animate? })(options?) => voidBack to minScale, centred.
onKeydown(event: KeyboardEvent) => void+, -, 0 and the arrow keys. Bind it to @keydown on el.