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-touch-canvas

v0.4.4

Published

React higher order components to create zoomable/pannable canvas

Downloads

39

Readme

react-touch-canvas

This is a set of two React higher order components, for hooking mouse and touch events panning/zooming functionality into your application.

The project doesn't have dependencies to any other libraries than React (and TypeScript types for React).

Project home page: https://jussikinnula.github.io/react-touch-canvas

See the project on GitHub: https://github.com/jussikinnula/react-touch-canvas

Installation

$ npm install --save react-touch-canvas

Usage

Example

You can check an example application, which sets canvas full screen and also updates the canvas size on resize, and other fancy things:

Live version: https://jussikinnula.github.io/react-touch-canvas/example

Source: https://github.com/jussikinnula/react-touch-canvas/tree/master/example/src

Minimal example

Here's a very minimal version, which writes the seconds after animation started to the canvas.

import * as React from 'react'
import { Touch, Canvas } from 'react-touch-canvas'

const style = { width: '800px', border: '1px solid red' }

export default const App = () => (
  <div style={style}>
    <Touch>
      <Canvas
        width={800}
        height={600}
        onAnimationFrame={(ctx, time) => {
          ctx.font = '30px Arial'
          ctx.fillText(`time: ${Math.round(time)}`, 25, 50)
        }}
      />
    </Touch>
  </div>
)

Touch -component

All parameters are optional.

Parameters wheelMax and touchSensitivity are device and operating system specific. So you need to test with real devices. If you want, you could share good values with this project so we could have good defaults in future.

If you are hooking to an external Canvas, you want to set context based on onTranslate and onScale values (the coordinates are relative). The onReset is called on resize, when matrix is reset.

Panning and pinching raw events are untouched (can be either React.MouseEvent<HTMLDivElement> or React.TouchEvent<HTMLDivElement>).

<Touch
  scaleFactor={1.1} // set the scale factor (default 1.1) for zooming
  wheelMax={5} // maximum wheel zoom amount (default 5)
  touchSensitivity={5} // touch sensitivity (default 5)
  onPanStart={(event) => { ... }} // raw events, when panning starts
  onPan={(event) => { ... }} // raw events, when panning is active (e.g. touch or mouse moves)
  onPanEnd={(event) => { ... }} // raw events, when panning ends
  onPinchStart={(event) => { ... }} // raw events, when pinch zoom starts
  onPinch={(event) => { ... }} // raw events, when pinch zooming is active (e.g. pinch gesture between two touch points is happening)
  onPinchEnd={(event) => { ... }} // raw events, when pinch zoom ends
  onTranslate={(x: number, y: number) => { ... }) // raw coordinates, when matrix is translated
  onScale={(x: number, y: number) => { ... }) // raw coordinates, when matrix is scaled
  onReset={() => { ... }} // called when matrix is reset
>
  ...
</Touch>

Canvas -component

The ctx in onAnimationFrame and getContext callbacks is CanvasRenderingContext2D, and time is a number (starting where the animation started).

<Canvas
  width={800} // Set width for the canvas
  height={600} // Set height for the canvas
  onAnimationFrame={(ctx, time) => { ... }} // draw anything to Canvas 2D context here (uses requestAnimationFrame to trigger redraw)
  getContext={(ctx) => { ... }} // get Canvas 2D context (this should be linked by Touch -component, to set scale and translate)
/>

Development

Utilities

The utils -library used internally by Touch and Canvas -components is used to do matrix and distance calculations. You can look the inner workings also from the source.

Remote Logging

In order to test with real touch -capable devices, you can use remote logging (e.g. get device console.log's into your console), with the following environment variables:

$ REMOTE_LOG=ws://x.x.x.x:12345 npm start

Note! Replace x.x.x.x with your computer's IP address, accessible from your touch device.

Start remote log in separate shell:

$ npm run remote-log