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

canvas-transform-context

v1.1.1

Published

A wrapper for HTML canvas context for easy zooming/panning/translating

Downloads

557

Readme

Canvas Transform Context

npm version

A canvas context extension based on this example by phrogz.

A class wrapper for a 2D canvas context that keeps track of transform information, allowing for easy coordinate control with scaled/transformed canvases. Perfect for visual web apps that requires extra canvas functionalities without the hassle of custom canvas implementations.

Demo

Installation

via npm

npm i canvas-transform-context
import { TransformContext } from "canvas-transform-context"

via browser

import { TransformContext } from "https://unpkg.com/canvas-transform-context@latest/dist/bundle.min.js";

Usage

Basic setup

const canvas = getDocumentById(/* canvas id */)
const ctx = canvas.getContext('2d')

const transformCtx = new TransformContext(ctx);

transformCtx.onDraw((ctx) => {
  /* Draw on canvas... */
})

// Mouse dragging
canvas.addEventListener("mousedown", (e) => transformCtx.beginMousePan(e));
canvas.addEventListener("mousemove", (e) => transformCtx.moveMousePan(e));
canvas.addEventListener("mouseup", (e) => transformCtx.endPan(e));

// Wheel zooming
canvas.addEventListener("wheel", (e) => transformCtx.zoomByMouse(e));

Documentation

Action Methods

Batteries-included methods for commonly use actions, included methods that can directly take mouse events as a parameter.

beginMousePan(e)

Begins a pan given the current position from the mouse event. Use on mousedown.

| Param | Description | | ------- | - | | e | mousedown event |

moveMousePan(e)

Pans the canvas to the new position from the mouse event. Use on mousemove. Does nothing if beginMousePan wasn't called, or if endPan was just called.

| Param | Description | | ------- | - | | e | mousemove event |

endMousePan()

Ends a mouse pan. Use on mouseup.

zoomByMouse(e, zoomScale)

Zooms via the mouse wheel event.

| Param | Default | Description | | ----------- | ------------------ | ------------------------------------------------- | | e | | mouse wheel event | | zoomScale | 1.1 | The scale percentage to zoom by. Default is 1.1 |

beginPan(start, transform)

Sets the anchor for a panning action.

| Param | Default | Description | | ----------- | ------------------- | --------------------------------------------------- | | start | | Starting coordinates for a pan | | transform | true | Whether or not to transform the start coordinates |

movePan(current, transform)

Pans the canvas to the new coordinates given the starting point in beginPan. Does nothing if beginPan was not called, or if endPan was just called.

| Param | Default | Description | | ----------- | ------------------- | --------------------------------------------------- | | current | | | | transform | true | Whether or not to transform the start coordinates |

endPan()

Stops a pan

zoomBy(amount, zoomScale, center, transform) ⇒

Zoom by a given integer amount.

Returns: Current zoom amount in integers

| Param | Default | Description | | ----------- | ------------------------ | --------------------------------------------------------------------------------------------- | | amount | | Amount to zoom by in integers. Positive integer zooms in | | zoomScale | 1.1 | The scale percentage to zoom by. Default is 1.1 | | center | undefined | The point to zoom in towards. If undefined, it will zoom towards the latest panned position | | transform | true | Whether or not to transform the center coordinates |

reset()

Resets all transformations

Action Helpers

onDraw(callback)

Creates a callback to be called after each action method above.

| Param | Description | | ------------- | -------------------- | | callback | A callback function with the canvas context as a parameter |

Transform Helpers

Helper methods to deal with coordinate transformations

transformPoint(canvasPoint) ⇒

Converts canvas coordinates to transformed coordinates

Returns: Transformed coordinates

| Param | Description | | ------------- | -------------------- | | canvasPoint | Canvas coordinates |

mouseToTransformed(e) ⇒

Converts a mouse event to the transformed coordinates within the canvas

Returns: Transformed point

| Param | Description | | ------- | ------------- | | e | mouse event |

clearCanvas()

Clear the canvas given the current transformations

General Helpers

General purpose canvas helpers unrelated to transform

mouseToCanvas(e) ⇒

Converts a mouse event to the correct canvas coordinates

Returns: Canvas coordinates

| Param | Description | | ------- | ------------- | | e | mouse event |

Attributions

Main implementation based on code by phrogz:

  • http://phrogz.net/tmp/canvas_zoom_to_cursor.html
  • https://github.com/Phrogz