1.0.15 • Published 2 months ago

use-pinch-zoom v1.0.15

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

usePinchZoom hook

This is a react hook that allows you to zoom and drag using touch gestures and mouse events.

Demo

https://use-pinch-zoom.vercel.app/

Setup

To run this project, install it locally using npm:

npm i use-pinch-zoom

Quick start

  1. Import the “usePinchZoom” hook:
import usePinchZoom from "use-pinch-zoom";

const {
  pinchZoomTransitionX,
  pinchZoomTransitionY,
  onMouseDown,
  onDragStart,
  isDragging,
  zoom,
  onDraging,
  onTouchEnd,
  onMouseWheel,
} = usePinchZoom({});
  1. Assign event functions and pinchZoomTransitionX, pinchZoomTransitionY, zoom as CSS style properties to the elements you want to zoom.
<img
  onMouseDown={onMouseDown}
  onTouchMove={onDraging}
  onTouchEnd={onTouchEnd}
  onTouchStart={onDragStart}
  onWheel={onMouseWheel}
  style={{
    transform: `scale(${zoom}) translateX(${pinchZoomTransitionX}px) translateY(${pinchZoomTransitionY}px)`,
  }}
/>
  1. By default, this feature disables the browser's native zooming and scrolling during pinch zoom or drag when the zoom level is higher than 1. This means that when the zoom level is set to 1, you can still zoom on the page using native pinch zoom when your fingers are not on the element that supports this function. When the zoom level exceeds 1, the feature will prevent native zooming and scrolling of the entire document. However, this can be disabled using the "preventDefaultTouchBehavior" and "preventDefaultWheelBehavior" properties.

  2. If you want to disable native zooming and scrolling for the entire document or content, such as a modal, you can add the CSS style "touch-action: none" to the "body" tag or modal container. If you want to disable native pinch zoom but still be able to scroll vertically and horizontally, you can set "touch-action: pan-y pan-x".

  3. In order for the function to calculate everything correctly (margins, etc.), the enlarged element must always be centered relative to its parent or fill it completely. If you do not want the element that you are zooming in on to be centered in relation to the viewport, you can still achieve this. Simply wrap your element in a container that will either center it or completely fill it. Set the property "relativeTo" to "parent" or "both". You can place the container anywhere on the screen (left-aligned, right-aligned) and everything should work as intended.

 

Usage details

PropertyDefaultTypeDetails
boundaryResistance?50numberHow far you can drag the zoomed element out of scope.
maxZoom?3numberMaximum zoom value.
doubleTapSensivity?300(ms)numberDouble tap sensivity.
minDistBetweenFingers?80(px)numberThe minimum distance between fingers that allows for zooming with two fingers.
keepZoom?truebooleanYou can set this to false if you want after touchend zoom return to the initial position.
disableDoubleTap?falsebooleanYou can set this option to true if you want to disable double-tap zoom.
zoomFactor?4numberDefines how many times the increaseZoom function will need to be called to obtain the maximum zoom. For example, if you set 5, each function call will increase the element by 20 percent of its size.
relativeTo?"viewport""viewport", "parent", "both"Specifies the element against which the boundaries will be set and the zoom calculations will be performed. It is important to keep in mind that the enlarged element should always be centered in relation to its parent. The default value is "viewport". You have the option to pass different values here, such as: "viewport", "parent", "both". If you choose "both", it means that the element will be positioned relative to the parent when it exceeds the parent's size, and relative to the viewport when it exceeds the viewport's size. This option combines the "parent" and "viewport" values and keeps track of both parent and viewport boundaries.
allowDragWhenZooming?truebooleanIt defines whether you can move an element during pinch zoom.
preventDefaultTouchBehavior?truebooleanPrevents the default pinch zoom and scroll functionality of the web browser when an element is enlarged with two fingers or when the zoom level is greater than 1.
preventDefaultWheelBehavior?truebooleanPrevents the default scroll functionality of the web browser when the zoom level of an element is greater than 1.

 

Functions and variables that return a hook.

FunctionTypeUsage
onMouseDownfunctionFunction to handle "onMouseDown" event.
onDragStartfunctionFunction to handle "onTouchStart" event.
onDraggingfunctionFunction to handle "onTouchMove" event.
onTouchEndfunctionFunction to handle "onTouchEnd" event.
onTouchEndfunctionFunction to handle "onTouchEnd" event.
onMouseWheelfunctionFunction to handle "onWheel" event. You should add this if you want to be able to scroll the zoomed element using the mouse wheel.
handleDecreaseZoomfunctionCall this function to decrease the zoom.
handleIncreaseZoomfunctionCall this function to increase the zoom.
handleResetZoomfunctionCall this function to reset the zoom/translation to its initial values.
enableDragAndZoomfunctionCall this function to enable zooming/dragging.
disableDragAndZoomfunctionCall this function to disable zooming/dragging.
pinchZoomTransitionXnumberAdd this as a transform css property to your zoom element.
pinchZoomTransitionYnumberAdd this as a transform css property to your zoom element.
zoomnumberAdd this as css property to your zoom element.
isDraggingbooleanChanges the value to true when dragging.
isZoomingbooleanChanges the value to true when zooming with two fingers.
wasDoubleTappedbooleanChanges the value to true after double tap.

 

 

Example on Codesandbox

https://codesandbox.io/p/sandbox/usepinchzoom-mj589m

1.0.15

2 months ago

1.0.14

2 months ago

1.0.13

2 months ago

1.0.12

2 months ago

1.0.11

2 months ago

1.0.10

2 months ago

1.0.9

2 months ago

1.0.8

2 months ago

1.0.7

2 months ago

1.0.6

2 months ago

1.0.5

2 months ago

1.0.4

2 months ago

1.0.3

3 months ago

1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago