Readonly
elReadonly
optsIf true (default) canvas is cleared when a resize happens
Colour space to use. Defaults to sRGB.
By default, the helper emits pointer events from the canvas. Set this to true to disable.
Optional
draw?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void)If specified, this function be called in an animation loop.
Drawing context
Viewport size
CanvasHelper instance
Logical height of canvas. This is used for establishing the desired aspect ratio.
Optional
onCallback when canvas is resized
By default the display DPI is used for scaling. If this is set, this will override.
Optional
resizeAutomatic canvas resizing logic.
If true, it won't add any position CSS
Logical width of canvas. This is used for establishing the desired aspect ratio.
If set, the z-index for this canvas. By default, fullscreen canvas will be given -1
Gets the center coordinate of the canvas
Gets the drawing context
Returns the width or height, whichever is largest
Returns the width or height, whichever is smallest
Gets the current scaling ratio being used to compensate for high-DPI display
Returns a Scaler that converts from relative to absolute coordinates. This is based on the canvas size.
// Assuming a canvas of 800x600
toAbsolute({ x: 1, y: 1 }); // { x: 800, y: 600}
toAbsolute({ x: 0, y: 0 }); // { x: 0, y: 0}
toAbsolute({ x: 0.5, y: 0.5 }); // { x: 400, y: 300}
Returns a Scaler that converts from absolute to relative coordinates. This is based on the canvas size.
* // Assuming a canvas of 800x500
toRelative({ x: 800, y: 600 }); // { x: 1, y: 1 }
toRelative({ x: 0, y: 0 }); // { x: 0, y: 0 }
toRelative({ x: 400, y: 300 }); // { x: 0.5, y: 0.5 }
Adds event listener.
Events
Event name
Event handler
Protected
fireFire event
Type of event
Arguments for event
Returns the canvas frame data as a writable grid.
When editing, make as many edits as needed before calling
flip
, which writes buffer back to the canvas.
* const g = helper.getWritableBuffer();
// Get {r,g,b,opacity} of pixel 10,10
const pixel = g.get({ x: 10, y: 10 });
// Set a colour to pixel 10,10
g.set({ r: 0.5, g: 1, b: 0, opacity: 0 }, { x: 10, y: 10 });
// Write buffer to canvas
g.flip();
Uses 'physical' size of canvas. Eg with a high-DPI screen, this will mean a higher number of rows and columns compared to the logical size.
Remove event listener
A wrapper for the CANVAS element that scales the canvas for high-DPI displays and helps with resizing.
Draw whenever it is resized using the 'resize' event
Or provide a function when initialising:
Automatically draw at animation speeds: