1.0.12 • Published 4 years ago

react-use-zoom v1.0.12

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

react-use-zoom

Nearly native image scaling (:iphone:/:computer:), rich in functionality. It is extremely easy to use. it's built by comparing scaling features existing in 'Gallery' apps for smartphones. It doesn't seem slow, but it doesn't seem ridiculously fast either, to be honest, I didn't measure performance. This will also boost your mobile app and website user experience.

Preview

Note

The image must take up the entire width and height of the page

Features


Mobile

  • Pinch
  • Pan
  • Touch move

Desktop

  • Mouse wheel
  • Mouse move

General

  • Maximum scaling value calculated automatically
  • The image is limited by its border
  • It is lightweight
  • It feels "native"

Installation

via NPM
npm i react-use-zoom
via CDN (unpkg)
https://unpkg.com/react-use-zoom@latest/build/index.umd.js

UMD library exposed as reactUseZoom

Usage

It is also very important that your image contains the following css properties.

.img {
  width: 100%;
  height: 100%;
  will-change: transform;
  backface-visibility: hidden;
  touch-action: none;
  transform-origin: 0 0;
  position: absolute;
  -webkit-user-drag: none;
  user-drag: none;
}

.basic-transition {
  transition: all 0.2s;
}
import { useZoom } from "react-use-zoom";

const MyComponent = () => {
  const { imgRef, fit, events } = useZoom({
    transitionClassName: "basic-transition",
  });
  return (
    <img
      className="img"
      src={"..."}
      ref={imgRef}
      style={{ objectFit: fit }}
      {...events}
    />
  );
};

Manual zooming

If for some reason you want to zoom in by calling the function manually. You can use the zoomIn and zoomOut functions. It will scale in the center of the image.

import { useZoom } from "react-use-zoom";

const MyComponent = () => {
const { imgRef, fit, events, `zoomIn`, `zoomOut` } = useZoom();
const handleInZoom = () => `zoomIn()`;
const handleOutZoom = () => `zoomOut()`;
};

API useZoom

PropTypeDescription
eventsReact.TouchEvent,React.MouseEvent,EventEvents must be assigned to an element to work with it. onLoad, onMouseDown or onTouchStart
zoomInFunctionCall the zoom in manually. The transformation point will be at the center
zoomOutFunctionCall the zoom out manually. The transformation point will be at the center
fitReact.CSSPropertiesDecides when the image should be `object-fit: contain;' or 'object-fit: none;'
visibilityReact.CSSPropertiesThe visibility property specifies whether or not an element is visible. This is mainly done to remove flickering from the image.
imgRefReact.RefObjectAccess an image by providing a reference
transitionClassNameStringClass name to provide a smooth transition when scaling
1.0.11

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.3.2

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago