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

@szydlovski/canvas-utility

v2.1.2

Published

simple canvas 2d utility - trimming, rotating, resizing

Downloads

1

Readme

This library contains various utilities designed for working with the HTML5 Canvas. It facilitates operations like creating, copying, cropping, trimming, flipping, merging, resizing and rotating canvases.

Usage

npm install @szydlovski/canvas-utility

Example usage using the Canvas wrapper:

import { Canvas, loadImage } from '@szydlovski/canvas-utility';
(async () => {
	const image = await loadImage('https://source.unsplash.com/random');
	const canvas = Canvas.create([500, 500])
		.draw((ctx) => ctx.drawImage(image, 0, 0, 500, 500))
		.flip()
		.resize({ width: 800 })
		.crop('16:9')
		.rotate(90)
		.toElement();
	document.body.appendChild(canvas);
})();

This will result in a random image drawn at 500 by 500 pixels, flipped horizontally, resized to 800 by 800 pixels, cropped to a 16:9 ratio (800x450) and then rotated by 90 degrees. The resulting canvas will be 450 pixels wide and 800 pixels tall.

All of the operations used in this example are also exported as individual functions, so using the wrapper is completely optional:

import { flipCanvas, cropCanvas, resizeCanvas, loadImage } from '@szydlovski/canvas-utility';
(async () => {
	const image = await loadImage('https://source.unsplash.com/random');
	document.body.appendChild(
		flipCanvas(cropCanvas(resizeCanvas(image, { width: 500 }), '16:9'))
	);
})();

This will result in a random image, resized to a width of 500 pixels (height will be calculated from the image's aspect ratio), cropped to 16:9 and flipped horizontally.

The functions can be neatly composed to create utilities:

import { cropCanvas, resizeCanvas } from '@szydlovski/canvas-utility';
const resizeAndCropPreviewImage = (image) =>
	resizeCanvas(cropCanvas(image, '1:1'), { width: 250 });

// or...

import { Canvas } from '@szydlovski/canvas-utility';
const resizeAndCropPreviewImage = (image) =>
	Canvas.fromImage(image)
		.crop('1:1')
		.resize({ width: 250 })
		.toElement();

Functional API

The functional API allows non-destructive, immutable operations on the canvas. Note that in addition to canvases, all the functions accept images and offscreen canvases.

Types

  • Drawable - HTMLImageElement | HTMLCanvasElement | OffscreenCanvas
  • Dimensions - { width: number, height: number }
  • DimensionsSource - Dimensions | [number, number] | number - an object with width and height properties, a [width, height] tuple or a single number (in which case it will be used for both width and height)
  • DrawCallback - (ctx: CanvasRenderingContext2d) => void

createCanvas(source, callback)

Arguments:

  • source - DimensionsSource - used to determine dimensions for the created canvas
  • callback - DrawCallback - optional, allows you to immediately draw on the created canvas

Returns: HTMLCanvasElement, the newly created canvas


Creates a new canvas using the provided dimensions. Optionally, you can provide a callback which can be used to draw on the newly created canvas.

Example useage:

const c1 = createCanvas(500) // a 500 by 500 canvas
const c2 = createCanvas([200,400]) // a 200 by 400 canvas
const c3 = createCanvas({ width: 800, height: 1600 }) // a 800 by 1600 canvas
const c4 = createCanvas(c2) // a 200 by 400 canvas
const c5 = createCanvas(c3, ctx => ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)); // a 800 by 1600 canvas, filled with black pixels

copyCanvas(canvas)

Arguments:

  • canvas - Drawable - the entity to be copied

Returns: HTMLCanvasElement, a canvas with the copied entity


Creates a copy of the provided Drawable by drawing it on a new, identically sized canvas.

Example usage:

const blackRect = createCanvas(c3, ctx => ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height));
const blackRectCopy = copyCanvas(blackRect);
console.log(blackRect !== blackRectCopy); // true

trimCanvas(canvas)

Arguments:

  • canvas - Drawable - the entity to be trimmed

Returns: HTMLCanvasElement, a canvas with the trimmed entity


Trims the provided Drawable by copying its contents to a new canvas and removing empty lines of pixels.

Example usage:

const imgToTrim = createCanvas(500, ctx => ctx.fillRect(100, 100, 300, 300));
const trimmedImg = trimCanvas(imgToTrim);
console.log(trimmedImg.width); // 300
console.log(trimmedImg.height); // 300

Chainable Wrapper API

The entire functional API is also available in a convenient, chainable wrapper, exported as Canvas, with methods named the same as the functional operations but without the Canvas suffix (i.e. rotateCanvas becomes Canvas.rotate).

Example usage:

const img = await loadImage('path/to/image.png');
const result = Canvas.fromImage(img)
	.trim()
	.resize({ width: 500 })
	.crop('1:1')
	.rotate(45)
	.grayscale()
	.toElement(); // returns a canvas element

Changelog

[2.1.0] - 2021-08-13

Added

  • extractPixelsFromCanvas

[2.0.0] - 2021-08-09

Full TypeScript rewrite.

[1.2.0] - 2021-08-01

Added

  • maskCanvas

[1.1.0] - 2021-08-01

Added

  • Utilities for drawing circles - fillCircle, strokeCircle
  • Utilities for drawing polygons - fillPolygon, strokePolygon
  • Utilities for drawing images - fillImage, strokeImage
  • Wrapper for ImageData - ImageDataHandle
  • Wrapper for canvas operations - Canvas
  • adjustCanvas, a function which allows pixel by pixel color adjustment

[1.0.1] - 2021-07-30

Added

  • flipCanvas

[1.0.0] - 2021-07-30

Initial release