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

canvas2d-zoom

v0.1.6

Published

A webcomponent that adds zoom and pan behaviour to a canvas element

Downloads

187

Readme

canvas2d-zoom

This package provides a webcomponent <canvas2d-zoom> that can be used like a 2D canvas element. Its purpose is to automatically add pan and zoom behaviour to the canvas. It is in an early phase of development and likely has some rough edges.

Content

Example

https://cnoelle.github.io/canvas2d-zoom/index.html

Installation

Using npm:

npm install canvas2d-zoom 

Usage

Import the project as an ES module, call Canvas2dZoom.register() once in your javascript, and add a tag <canvas2d-zoom> to your HTML. Drawing on the canvas works the same as for ordinary <canvas> elements.

HTML standalone example:

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <canvas2d-zoom width="640" height="480" min-zoom="0.25" max-zoom="8"></canvas2d-zoom>
    <script type="module">
        import { Canvas2dZoom } from "https://unpkg.com/canvas2d-zoom@latest/dist/canvas2d-zoom.js";
        Canvas2dZoom.register();
        const canvas = document.querySelector("canvas2d-zoom");
        const ctx = canvas.getContext("2d");
        ctx.beginPath();
        ctx.arc(200, 100, 15, 0, 2 * Math.PI);
        ctx.stroke();
    </script>
</body>

Typescript example:

import { Canvas2dZoom } from "canvas2d-zoom";
Canvas2dZoom.register();
const canvas: Canvas2dZoom = document.querySelector("canvas2d-zoom");
const ctx: CanvasRenderingContext2D = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(200, 100, 15, 0, 2 * Math.PI);
ctx.stroke();

Configuration

The canvas attributes width and height are supported, and furthermore:

  • zoom: A boolean value, can be used to disable zoom behaviour. Default value: "true"
  • pan: A boolean value, can be used to disable pan behaviour. Default value: "true"
  • max-zoom (property: maxZoom): A positive number, typically > 1, representing the maximum scale value. Example: 8. Default value: undefined
  • min-zoom (property: minZoom): A positive number, typically <= 1, the minimum scale value. Example: 0.125. Default value: undefined
  • zoom-factor (property: zoomFactor): A number > 1 that determines the zoom velocity. Default value: 2.
  • double-click-mode (property: doubleClickMode): Either "reset" (reset zoom and pan state on doubleclick) or "zoom" (zoom in on doubleclick, or zoom out if ctrl key is pressed at the same time) or absent (default, no action).
  • circle-min-radius (property: circleMinRadius): A minimum radius for circles/arcs. When zooming out and the limit is hit, the circle will not shrink any further. Default value: undefined (since 0.1.2)
  • rect-min-width and rect-min-height (properties rectMinWidth and rectMinHeight): Minimum width and height for rectangles. When zooming out and the limit is hit, the rectangle width or height will not shrink any further. Default value: undefined (since 0.1.2)

Example

<canvas2d-zoom width="640" height="480" pan="false" zoom="true" max-zoom="8" min-zoom="0.125" double-click-mode="reset"></canvas2d-zoom>

Interactions

Currently only mouse and keyboard interactions are supported (no mobile gestures)

Zoom

  • Mouse wheel
  • Ctrl + '+' (zoom in) or Ctrl + '-' (zoom out) (requires focus on canvas, e.g. by clicking the canvas once)

Pan

  • Mouse drag
  • Ctrl + keyboard arrow buttons (requires focus on canvas, e.g. by clicking the canvas once)

Reset zoom and pan

  • Mouse double click, if attribute double-click-mode is set to reset
  • Ctrl + Enter (requires focus on canvas, e.g. by clicking the canvas once)

How it works

The element remembers all calls to the CanvasRenderingContext2D methods relevant to drawing, such as ctx.beginPath(), ctx.rect() or ctx.stroke(). When the user pans or zooms (via mouse or keyboard interactions), the canvas is cleared, a transformation appropriate for the zoom and pan state is set, and the canvas is redrawn.

Development

  • Prerequisites: NodeJS/npm (a current version)
  • Install dependencies: npm install
  • Run: a sample HTML page is included in the repository, see index.html. Start the dev webserver with npm run start, then open the browser at http://localhost:8080. Note: in order to run the sample page with the current source code instead of the latest published version replace the url https://unpkg.com/canvas2d-zoom@latest/dist/canvas2d-zoom.js by "./bundle.js" at the end of the html file.
  • Tests: not yet

License

MIT