20.0.0 • Published 5 years ago

paperduck v20.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

PaperDuck

Manipulates a canvas using the native functions of a 2D rendering context.

setup

npm

npm i paperduck

es6

import PaperDuck from 'paperduck';

node

let PaperDuck = require('paperduck');

browser

<script src="https://unpkg.com/paperduck"></script>

The module is globally available as PaperDuck.

members

constructor

new PaperDuck(context)

argumentdescription
contextThe 2D rendering context.
let canvas = document.getElementById('myCanvas');
let context = canvas.getContext('2d');
let duck = new PaperDuck(context);
console.log(instance.canvas === canvas); // => true

static methods

.from(value)

Creates a PaperDuck instance from the given value.

argumentdescription
valueThe value to create from.

Supported value types are ...

let canvas = document.getElementById('myCanvas');
let duck = PaperDuck.from(canvas);
console.log(duck.canvas === canvas); // => false
let otherDuck = PaperDuck.from(duck);
console.log(otherDuck.canvas === duck.canvas); // => false

.fromExcept(value)

Creates a PaperDuck instance from the given value if it's not one.

argumentdescription
valueThe value to create from.
let canvas = document.getElementById('myCanvas');
let duck = PaperDuck.fromExcept(canvas);
console.log(duck.canvas === canvas); // => false
let otherDuck = PaperDuck.fromExcept(duck);
console.log(otherDuck === duck); // => true

.loadFrom(value)

Loads a PaperDuck instance from the given value.

argumentdescription
valueThe value to load from.
let duck = await PaperDuck.loadFrom('/path/to/image.jpg');
document.body.appendChild(duck.canvas);

Supported value types are ...

... and everything the function .from supports.


.loadFromExcept(value)

Loads a PaperDuck instance from the given value if it's not one.

argumentdescription
valueThe value to load from.

.blank(width = 0, height = 0)

Creates a PaperDuck instance with a blank transparent canvas of the given size.

argumentdescription
widthThe width of the canvas.
heightThe height of the canvas.

properties

.canvas

The canvas.


.context

The 2D rendering context of the canvas.


.width

The width of the canvas.

let duck = PaperDuck.blank(200, 100);
console.log(duck.width); // => 200

.height

The height of the canvas.

let duck = PaperDuck.blank(200, 100);
console.log(duck.height); // => 100

methods

.resize(width = 'auto', height = 'auto', smoothing)

Resizes the canvas to the given size.

argumentdescription
widthThe width of the resized canvas. If the value is 'same', the value will be set to the current width. If the value is 'auto', the width will be set proportionally to the height.
heightThe height of the resized canvas. If the value is 'same', the value will be set to the current height. If the value is 'auto', the height will be set proportionally to the width.
smoothingThe smoothing factor. Not supported right now.

npm.io

let duck = PaperDuck.from(source).resize(300, 'auto');

.crop(left = 0, top = 0, width = 'auto', height = 'auto')

Positions and crops the canvas to the given size.

argumentdescription
leftThe left offset of the clipping. A negative value is a right offset.
topThe top offset of the clipping. A negative value is a bottom offset.
widthThe width of the clipped canvas. A negative value reverses the direction. If the value is invalid, the value will be set to the current width.
heightThe height of the clipped canvas. A negative value reverses the direction. If the value is invalid, the value will be set to the current height.
let duck = PaperDuck.from(source).crop(128, -512, null, 256);

.cropAlign(width = 'auto', height = 'auto', alignment = 'center')

Aligns and crops the canvas to the given size.

argumentdescription
widthThe width of the cropped canvas. If the value is invalid, the value will be set to the current width.
heightThe height of the cropped canvas. If the value is invalid, the value will be set to the current height.
alignmentThe alignment of the cropping. Possible values are 'left top', 'left center', 'left bottom', 'right top', 'right center', 'right bottom', 'center top', 'center center' and 'center bottom'. The order of words does not matter. The word center can be omitted.
let duck = PaperDuck.from(source).cropAlign(256, 128, 'bottom');

.scale(scaling, smoothing)

Resizes the canvas proportionally by the given factor.

argumentdescription
scalingThe scaling factor. If the value is greater than 1, the scale is an enlargement. If the value is less than 1, the scale is a reduction.
smoothingThe smoothing factor.

npm.io

let duck = PaperDuck.from(source);
let enlargedCanvas = duck.scale(3).canvas;
let reducedCanvas = duck.scale(1/2).canvas;

.scaleToContain(width, height, smoothing)

Scales the canvas proportionally to the minimum of the given size.

argumentdescription
widthThe minimum width of the scaled canvas.
heightThe minimum height of the scaled canvas.
smoothingThe smoothing factor.

.scaleToCover(width, height, smoothing)

Scales the canvas proportionally to the maximum of the given size.

argumentdescription
widthThe maximum width of the scaled canvas.
heightThe maximum height of the scaled canvas.
smoothingThe smoothing factor.

.contain(width, height, alignment = 'center', smoothing)

Scales the canvas proportionally to the minimum of the given size and crops it.

argumentdescription
widthThe width of the final canvas.
heightThe height of the final canvas.
alignmentThe alignment of the original canvas.
smoothingThe smoothing factor.

.cover(width, height, alignment = 'center', smoothing)

Scales the canvas proportionally to the maximum of the given size and crops it.

argumentdescription
widthThe width of the final canvas.
heightThe height of the final canvas.
alignmentThe alignment of the original canvas.
smoothingThe smoothing factor.

.flipHorizontally()

Flips the canvas horizontally.

npm.io


.flipVertically()

Flips the canvas vertically.

npm.io


.rotate90()

Rotates the canvas by 90 degrees clockwise.


.rotate180()

Rotates the canvas by 180 degrees.


.rotate270()

Rotates the canvas by 90 degrees counterclockwise.


.drawForeground(image, alignment = 'center')

Draws an image before the canvas.

argumentdescription
imageAn image to draw before.
alignmentThe alignment of the drawing.

.drawBackground(image, alignment = 'center')

Draws an image behind the canvas.

argumentdescription
imageAn image to draw behind.
alignmentThe alignment of the drawing.

.toDataURL(format = 'image/png', quality = 0.92)

Creates a data URL string representing the content.

argumentdescription
formatThe image format.
qualityThe image quality.

.toImageData()

Creates an ImageData object representing the content.


.toImage(format, quality)

browser only

Creates an HTMLImageElement instance representing the content.

argumentdescription
formatThe image format.
qualityThe image quality.
let duck = PaperDuck.from(source).cover(128, 128);
let image = duck.toImage('image/jpeg', 0.75);
image.style.border = '1px solid BlueViolet';
document.body.appendChild(image);

.saveToImage(format = 'image/png', quality = 0.92)

browser only

Saves an HTMLImageElement instance representing the content.

argumentdescription
formatThe image format.
qualityThe image quality.
let duck = PaperDuck.from(source).cover(128, 128);
let image = await duck.saveToImage('image/jpeg', 0.75);
image.style.border = '1px solid BlueViolet';
document.body.appendChild(image);

.toBlob(format = 'image/png', quality = 0.92)

browser only

Creates a Blob instance representing the content.

argumentdescription
formatThe image format.
qualityThe image quality.

.toBuffer(format = 'image/png', quality = 0.92)

node only

Creates a Buffer instance representing the content.

argumentdescription
formatThe image format.
qualityThe image quality.
20.0.0

5 years ago

19.0.0

5 years ago

18.10.0

5 years ago

18.9.25

6 years ago

18.9.13

6 years ago

18.9.12

6 years ago

18.6.3

6 years ago

18.6.2

6 years ago

18.5.30

6 years ago

17.9.17

7 years ago