![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@szydlovski/canvas-utility
Advanced tools
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.
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();
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.
HTMLImageElement | HTMLCanvasElement | OffscreenCanvas
{ width: number, height: number }
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)(ctx: CanvasRenderingContext2d) => void
createCanvas(source, callback)
Arguments:
DimensionsSource
- used to determine dimensions for the created canvasDrawCallback
- optional, allows you to immediately draw on the created canvasReturns: 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:
Drawable
- the entity to be copiedReturns: 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:
Drawable
- the entity to be trimmedReturns: 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
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
extractPixelsFromCanvas
Full TypeScript rewrite.
maskCanvas
fillCircle
, strokeCircle
fillPolygon
, strokePolygon
fillImage
, strokeImage
ImageData
- ImageDataHandle
Canvas
adjustCanvas
, a function which allows pixel by pixel color adjustmentflipCanvas
FAQs
simple canvas 2d utility - trimming, rotating, resizing
The npm package @szydlovski/canvas-utility receives a total of 0 weekly downloads. As such, @szydlovski/canvas-utility popularity was classified as not popular.
We found that @szydlovski/canvas-utility demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.