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