Coder Social home page Coder Social logo

retyui / react-fast-pinch-zoom Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 3.0 883 KB

React component for pinch zooming DOM elements

Home Page: https://react-fast-pinch-zoom.netlify.app/

HTML 2.36% TypeScript 97.64%
pinch-to-zoom zoom reactjs react react-component gestures drag

react-fast-pinch-zoom's Introduction

<PinchZoom/> - a react component for pinch zooming DOM elements.

Code quality react-fast-pinch-zoom on npm react-fast-pinch-zoom downloads react-fast-pinch-zoom bundle size

This is a migration of GoogleChromeLabs/pinch-zoom to React!

Demo

Simple image pinch-zoom. Although you can put any element in <PinchZoom/>.

Usage

yarn add pinch-zoom-element
import React, { useRef, useCallback } from "react";
import {
  PinchZoom,
  applyCssProperties,
  getTransform3DValue,
  getTransform2DValue,
} from "react-fast-pinch-zoom";

const App = () => {
  const ref = useRef(null);
  const onTransform = useCallback(({ x, y, scale }) => {
    // Use CSS Custom Properties (Variables)
    applyCssProperties(ref.current, { x, y, scale });

    // Or use literal transform value
    // ref.current.style.setProperty('transform',getTransform3DValue({x,y,scale}));
    // ref.current.style.setProperty('transform',getTransform2DValue({x,y,scale}));
  }, []);

  return (
    <PinchZoom onTransform={onTransform}>
      <img
        ref={ref}
        alt="two black cats look at you"
        src="https://cdn.glitch.com/d824d0c2-e771-4c9f-9fe2-a66b3ac139c5%2Fcats.jpg"
      />
    </PinchZoom>
  );
};

Now the above can be pinch-zoomed!

API

const App = () => {
  const pinchZoomRef = React.useRef();

  return (
    <PinchZoom ref={pinchZoomRef}>
      <img />
    </PinchZoom>
  );
};

Properties

pinchZoomRef.current.x; // x offset
pinchZoomRef.current.y; // y offset
pinchZoomRef.current.scale; // scale

Methods

Set the transform. All values are optional.

pinchZoomRef.current.setTransform({
  scale: 1,
  x: 0,
  y: 0,
});

Scale in/out of a particular point.

const scale = 2;

pinchZoomRef.current.scaleTo(scale, {
  // Transform origin. Can be a number, or string percent, eg "50%"
  originX: 0,
  originY: 0,
  // Should the transform origin be relative to the "container", or "content"?
  relativeTo: "content",
});

react-fast-pinch-zoom's People

Contributors

dependabot[bot] avatar retyui avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.