Img

Img provides convenient functions to support image operations on HTML Canvas and CanvasSpace. Combine this with other Pts functions to experiment with visual forms that integrate bitmaps and vector graphics.

Class in src/Image.ts

Constructor

new Img ( editable, space, crossOrigin )

Create an Img

parameters
editable: boolean (default value: false)

Specify if you want to manipulate pixels of this image. Default is false.

space: CanvasSpace

Set the CanvasSpace reference. This is optional but will make sure the image's pixelScale match the canvas and set the context for creating pattern.

crossOrigin: boolean

an optional parameter to enable loading cross-domain images if set to true. The image server's configuration must also be set correctly. For more, see this documentation.

returns
An instance of Img
source

Accessors

GET canvas : HTMLCanvasElement

Get the internal canvas

GET canvasSize : Pt

Get size of the canvas

GET ctx : CanvasRenderingContext2D

Get the internal canvas' context. You can use this to draw directly on canvas, or create a new CanvasForm instance with it.

GET current : CanvasImageSource

Get current image source. If editable, this will return the canvas, otherwise it will return the original image.

GET data : ImageData

Get the internal canvas' ImageData

GET image : HTMLImageElement

Get the original image

GET imageSize : Pt

Get size of the original image

GET loaded : boolean

Get whether the image is loaded

GET pixelScale : number

Get pixel density scale

GET scaledMatrix : Mat

Get a Mat instance with a scale transform based on current pixelScale. This can be useful for generating a domMatrix for transforming patterns consistently across different pixel-density screens.

Methods

bitmap ( size )

Get an efficient, readonly bitmap of the current canvas.

parameters
size: PtLike

Optional size to crop

returns
Promise

a Promise that resolves to an ImageBitmap

source

STATIC blank ( size, space, scale )

Create an editable blank image

parameters
size: PtLike

of image

space: CanvasSpace

Set the CanvasSpace reference. This is optional but will make sure the image's pixelScale match the canvas and set the context for creating pattern.

scale: number

Optionally set a specific pixel scale (density) of the image canvas.

returns
Img

source

cleanup ( )

Remove the elements and data associated with this Img.

returns
void

source

crop ( box )

Crop an area of the image.

parameters
box: Bound

bounding box

returns
ImageData

source

filter ( css )

Apply filters such as blur and grayscale to the canvas image. The original image is unchanged until sync().

parameters
css: string

a css filter string such as "blur(10px) | contrast(200%)". See MDN documentation for a list of filter functions.

returns
this

source

STATIC fromBlob ( blob, editable, space )

Create a blob url that can be passed to Img.load

parameters
blob: Blob

an image blob such as new Blob([my_Uint8Array], {type: 'image/png'})

editable: boolean (default value: false)

Specify if you want to manipulate pixels of this image. Default is false.

space: CanvasSpace

returns
Promise

source

getForm ( )

Get a CanvasForm for drawing on the internal canvas if this Img is editable

returns
CanvasForm

source

STATIC getPixel ( imgData, p )

Given an ImaegData object and a position, return the RGBA pixel value at that position.

parameters
imgData: ImageData

an ImageData object

p: PtLike

a position on the image

returns
Pt

values of the pixel at the specific position

source

STATIC imageDataToBlob ( data )

Convert ImageData object to a Blob, which you can then create an Img instance via Img.fromBlob. Note that the resulting image's dimensions will not account for pixel density.

parameters
data: ImageData

returns
Promise

source

initCanvas ( width, height, canvasScale )

Initiate an editable canvas

parameters
width: number

width of canvas

height: number

height of canvas

canvasScale: number | PtLike (default value: 1)

pixel scale

returns
void

source

load ( src )

Load an image.

parameters
src: string

an url of the image in same domain. Alternatively you can use a base64 string. To load from Blob, use Img.fromBlob.

returns
Promise

a Promise that resolves to an Img

source

STATIC load ( src, editable, space, ready )

A static function to load an image with an optional ready callback. The Img instance will returned immediately before the image is loaded. To use async/await, use the loadAsync function or new Img(...).load(...).

parameters
src: string

an url of the image in same domain. Alternatively you can use a base64 string. To load from Blob, use Img.fromBlob.

editable: boolean (default value: false)

Specify if you want to manipulate pixels of this image. Default is false.

space: CanvasSpace

Set the CanvasSpace reference. This is optional but will make sure the image's pixelScale match the canvas and set the context for creating pattern.

ready: Fn(img:any)

An optional ready callback function

returns
Img

source

STATIC loadAsync ( src, editable, space )

A static method to load an image using async/await.

parameters
src: string

an url of the image in same domain. Alternatively you can use a base64 string. To load from Blob, use Img.fromBlob.

editable: boolean (default value: false)

Specify if you want to manipulate pixels of this image. Default is false.

space: CanvasSpace

Set the CanvasSpace reference. This is optional but will make sure the image's pixelScale match the canvas and set the context for creating pattern.

returns
Promise

source

STATIC loadPattern ( src, space, repeat, editable )

A static method to load an image pattern using async/await.

parameters
src: string

an url of the image in same domain. Alternatively you can use a base64 string. To load from Blob, use Img.fromBlob.

space: CanvasSpace

Set the CanvasSpace reference. This is optional but will make sure the image's pixelScale match the canvas and set the context for creating pattern.

repeat: CanvasPatternRepetition (default value: "repeat")

set how the pattern will repeat fills

editable: boolean (default value: false)

Specify if you want to manipulate pixels of this image. Default is false.

returns
Promise

a CanvasPattern instance for use in fill()

source

pattern ( reptition, dynamic )

Create a canvas pattern for fill()

parameters
reptition: CanvasPatternRepetition (default value: "repeat")

set how the pattern should repeat-fill

dynamic: boolean (default value: false)

If true, use this Img's internal canvas content as pattern fill. This enables the pattern to update dynamically.

returns
CanvasPattern

a CanvasPattern instance for use in fill()

source

pixel ( p, rescale )

Get the RGBA values of a pixel in the image

parameters
p: PtLike

position of the pixel

rescale: boolean | number (default value: true)

Specify if the pixel position should be scaled. Usually use rescale when tracking image and don't rescale when tracking canvas. You may also set a custom scale value.

returns
Pt

values of the pixel at the specific position

source

resize ( sizeOrScale, asScale )

Resize the canvas image. The original image is unchanged until sync().

parameters
sizeOrScale: PtLike

A PtLike array specifying either [x, y] scales or [x, y] sizes.

asScale: boolean (default value: false)

If true, treat the first parameter as scales. Otherwise, treat it as specific sizes.

returns
this

source

sync ( )

Replace the image with the current canvas data. For example, you can use CanvasForm's static functions to draw on this.ctx and then update the current image. To display the internal canvas, you can also use form.image( img.canvas ) directly.

returns
void

source

toBase64 ( )

Export current canvas image as base64 string

returns
string

source

toBlob ( )

Export current canvas image as a blob

returns
Promise

source