Giter VIP home page Giter VIP logo

react-prismazoom's Introduction

react-prismazoom

About

A pan and zoom component for React, using CSS transformations.

Depends only upon prop-types, react and react-dom modules.
Works on both desktop and mobile.

Online demo here!

Zoom features ๐Ÿ”Ž

  • Zoom with the mouse-wheel or a two-finger pinch
  • Zoom using double-click or double-tap
  • Zoom on selected area and center

Pan features ๐Ÿ‘†

  • Pan with the mouse pointer or with one finger when zoomed-in
  • Intuitive panning depending on available space when zoomed-in
  • Adjusts cursor style to indicate in which direction the element can be moved

Contribution

If you want to contribute, feel free to send a merge request or open a discussion. Currently, I just have time to maintain the package, but not enough to make big changes or add important features.

All contribution would be quite appreciated! ๐Ÿ˜‰

Among changes I would like to apply:

  • Migrate to TypeScript
  • Transform to a functional component (that could help splitting the code)
  • Make motion logic less dependant to React

Breaking changes on v3

  • The locked prop has been replaced by allowZoom and allowPan to handle zooming and panning events separately

Breaking changes on v2

  • The package now requires React v16.3 or higher (in order to use react references)
  • The zoom feature through gestures or mousewheel got some improvements to react better with all devices. You may need to adjust the scrollVelocity property passed to the component in order to keep the same effect.

Installation

Install the component

$ npm i -D react-prismazoom

Install the example project

This project includes a full-featured application example.

First clone the project.

Then, install it:

$ npm i

Run the Webpack Dev Server:

$ npm run start

Go to http://localhost:1664.

Note: sources of this example can be found in example/src.

Run unit tests

You can either run all tests at once:

$ npm test

Or run tests each time a change on source files occured:

$ npm run test:watch

Usage

Implementation

import PrismaZoom from 'react-prismazoom'

<PrismaZoom>
  <img src="my-image.png" />
  <p>A text that can be zoomed and dragged</p>
</PrismaZoom>

Props

Name Type Default Description
className string None Class name to apply on the zoom wrapper.
style object None Style to apply on the zoom wrapper. Note that transform, transition, cursor, touch-action and will-change cannot be overridden. Example: style={{backgroundColor: 'red'}}.
minZoom number 1 Minimum zoom ratio.
maxZoom number 5 Maximum zoom ratio.
scrollVelocity number 0.1 Zoom increment or decrement on each scroll wheel detection.
onZoomChange function null Function called each time the zoom value changes.
onPanChange function null Function called each time the posX or posY value changes (aka images was panned).
animDuration number 0.25 Animation duration (in seconds).
doubleTouchMaxDelay number 300 Max delay between two taps to consider a double tap (in milliseconds).
decelerationDuration number 750 Decelerating movement duration after a mouse up or a touch end event (in milliseconds).
allowZoom boolean true Enable or disable zooming in place.
allowPan boolean true Enable or disable panning in place.
allowTouchEvents boolean false Enables touch event propagation.

Note: all props are optional.

Public methods

These functions can be called from parent components.

zoomIn (value)
Increments the zoom with the given value.
Param {value: Number} : Zoom value

zoomOut (value)
Decrements the zoom with the given value.
Param {value: Number} : Zoom value

zoomToZone (relX, relY, relWidth, relHeight)
Zoom-in on the specified zone with the given relative coordinates and dimensions.
Param {relX: Number} : Relative X position of the zone left-top corner in pixels
Param {relY: Number} : Relative Y position of the zone left-top corner in pixels
Param {relWidth: Number} : Zone width in pixels
Param {relHeight: Number} : Zone height in pixels

reset ()
Resets the component to its initial state.

getZoom ()
Returns the current zoom value.
Return {Number} : Zone value

License

React PrismaZoom is licensed under the ISC license. See the LICENSE.md file for more details.

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.