npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-pinch-zoom-responder

v0.1.2

Published

A React Native gesture responder to recognize pinch and zoom

Downloads

98

Readme

pinch-zoom-responder

Easily add pinch and zoom gestures to your React Native components.

Setup

npm install --save react-native-pinch-zoom-responder

Usage

import React, {
  Component
} from 'react'
import {
  View,
  Text
} from 'react-native'
import PinchZoomResponder from 'react-native-pinch-zoom-responder'

class MyComponent extends Component {
  constructor (props) {
    super(props)
    this.state = {
      width: 1,
      height: 1
    }
    this.pinchZoomResponder = new PinchZoomResponder({
      onPinchZoomStart: (e) => {
        console.log('pinch started')
      },

      onPinchZoomEnd: (e) => {
        console.log('pinch ended')
      },

      onResponderMove: (e, gestureState) => {
        if (gestureState) {
          console.log('GestureState is ', gestureState)
          var transform = this._applyOriginTransform(gestureState.transform)
          this._setTransform(transform)
        }
      }
    })
  }

  _onLayout (event) {
    this.setState({
      width: event.nativeEvent.layout.width,
      height: event.nativeEvent.layout.height
    })
  }

  _setTransform (matrix) {
    this.transformView.setNativeProps({ style: { transform: [{perspective: 1000}, { matrix: matrix }] } })
  }

  /*
  React Native view transforms have the component center as their origin,
  so we need to wrap our transform with translations that compensate for this and
  place the origin at 0,0
  */
  _applyOriginTransform (matrix) {
    var translate = MatrixMath.createIdentityMatrix()
    var copy = matrix.slice()
    MatrixMath.reuseTranslate2dCommand(translate, (this.state.width / 2.0), (this.state.height / 2.0))
    MatrixMath.multiplyInto(copy, matrix, translate)
    MatrixMath.reuseTranslate2dCommand(translate, -(this.state.width / 2.0), -(this.state.height / 2.0))
    MatrixMath.multiplyInto(copy, translate, copy)
    return copy
  }

  render () {
    return (
      <View {...this.pinchZoomResponder.handlers} onLayout={(e) => this._onLayout(e)}>
        <Text ref={(ref) => { this.transformView = ref }}>Pinch me!</Text>
      </View>
    )
  }
}

The gestureState object has the shape:

{
  transform, cx, cy, scaleX, scaleY, dx, dy
}
  • transform is a 4x4 matrix that can be used with react-native/Libraries/Utilities/MatrixMath to easily transform points and graphics.
  • cx is the pinch X center
  • cy is the pinch Y center
  • scaleX is the pinch x scale
  • scaleY is the pinch y scale
  • dx is the current delta x of the center of the pinch
  • dy is the current delta y of the center of the pinch

Configuration

The responder can also receive a set of options as the second parameter.

var responders = {
  onPinchZoomStart: (e) => {}
  onPinchZoomEnd: (e) => {}
  onResponderMove: (e, gestureState) => {}
}
var options = {
  transformX: true,
  transformY: true
}
new PinchZoomResponder(responders, options)

The options object is optional and it's fields can include:

  • transformX true | false. True by default. Enables transformation along the X axis
  • transformY true | false. True by default. Enables transformation along the Y axis.