1.1.1 • Published 2 years ago

canvas-transform-context v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.

ParamDescription
emousedown 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.

ParamDescription
emousemove event

endMousePan()

Ends a mouse pan. Use on mouseup.

zoomByMouse(e, zoomScale)

Zooms via the mouse wheel event.

ParamDefaultDescription
emouse wheel event
zoomScale1.1The scale percentage to zoom by. Default is 1.1

beginPan(start, transform)

Sets the anchor for a panning action.

ParamDefaultDescription
startStarting coordinates for a pan
transformtrueWhether 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.

ParamDefaultDescription
current
transformtrueWhether 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

ParamDefaultDescription
amountAmount to zoom by in integers. Positive integer zooms in
zoomScale1.1The scale percentage to zoom by. Default is 1.1
centerundefinedThe point to zoom in towards. If undefined, it will zoom towards the latest panned position
transformtrueWhether 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.

ParamDescription
callbackA 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

ParamDescription
canvasPointCanvas coordinates

mouseToTransformed(e) ⇒

Converts a mouse event to the transformed coordinates within the canvas

Returns: Transformed point

ParamDescription
emouse 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

ParamDescription
emouse event

Attributions

Main implementation based on code by phrogz:

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago