Skip to content

ResponsiveCanvas

Responsive canvas

Responsive canvas is useful helper for applications doing rendering. It creates canvas layers within any html element. Layers preserve aspect ratio according to fitting mode. Ratio of canvas usually follows the ratio of the input video. In “crop” mode canvases are cropped to have the same aspect ratio as the container and scaled to fill the whole area. This is default mode preferred in most cases. In “fit mode” canvases are scaled down to fit into the container and leaving margins to stay in the center, “pad” mode behavior is the same, but margins are filled with highly blurred parts of the input video instead of still background. ResponsiveCanvas tracks size changes of container and updates layers and margins to preserve aspect ratio.

new ResponsiveCanvas(container, mode, layerCount, mirror, aspectRatio): ResponsiveCanvas

Constructor

HTMLElement

Container of responsive canvas

CanvasMode = "crop"

Fitting mode

number = 1

Number of canvas layers

boolean = false

Mirror the output

number = 1

Target aspect ratio

ResponsiveCanvas

EventEmitterT.constructor

protected aspectRatio: number = 1

Target aspect ratio


protected container: HTMLElement

Container of responsive canvas


protected layerCount: number = 1

Number of canvas layers


layers: HTMLCanvasElement[] = []

Canvas layers


protected mirror: boolean = false

Mirror the output


protected mode: CanvasMode = "crop"

Fitting mode


protected optional observer: ResizeObserver

Resize observer


optional pads: [HTMLCanvasElement, HTMLCanvasElement]

Padding canvases


padsSize: [number, number]

Relative size of padding canvases

addListener<E>(event, listener): this

Adds the listener function to the end of the listeners array

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.addListener


dispose(): void

Dispose responsive canvas

Destructs all canvas layers and other instances created by responsive canvas. Object must not be used after dispose() method has been called.

void


emit<E>(event, …args): boolean

Synchronously calls each of the listeners registered for the event

E extends "resize"

E

The name of the event

Args<CanvasEvents[E]>

Arguments passed to the listeners

boolean

True if the event had listeners, False otherwise

EventEmitterT.emit


eventNames(): (string | symbol)[]

List of emitter’s events

(string | symbol)[]

List of emitter’s events

EventEmitterT.eventNames


getMaxListeners(): number

Maximum number of listeners per event

number

Maximum number of listeners per event

EventEmitterT.getMaxListeners


getMirror(): boolean

Get mirror mode

boolean

Current mirror mode


getMode(): CanvasMode

Get fitting mode

CanvasMode

Current fitting mode


protected handleResize(entries): void

Resize event callback

Callback attached to resize observer to handle size updates. Basically, it calls updateSizes() to adjust canvas sizes.

ResizeObserverEntry[]

Elements being resized

void


listenerCount<E>(event): number

The number of listeners listening to the event

E extends "resize"

E

The name of the event

number

Number of listeners

EventEmitterT.listenerCount


listeners<E>(event): Function[]

Copy of the array of listeners for the event

E extends "resize"

E

The name of the event

Function[]

Copy of the listeners array

EventEmitterT.listeners


off<E>(event, listener): this

Removes the listener from the listener array for the event

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.off


on<E>(event, listener): this

Adds the listener function to the event

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.on


once<E>(event, listener): this

Adds a one-time listener function for the event

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.once


prependListener<E>(event, listener): this

Adds the listener function to the beginning of the listeners array

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.prependListener


prependOnceListener<E>(event, listener): this

Adds a one-time listener function to the beginning of the listeners array

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

EventEmitterT.prependOnceListener


rawListeners<E>(event): Function[]

Copy of the array of listeners for the event including wrappers

E extends "resize"

E

The name of the event

Function[]

Copy of the listeners array

EventEmitterT.rawListeners


removeAllListeners<E>(event?): this

Removes all listeners, or those of the specified event

E extends "resize"

E

The name of the event

this

This EventEmitter

EventEmitterT.removeAllListeners


removeListener<E>(event, listener): this

Removes the specified listener from the listener array

E extends "resize"

E

The name of the event

CanvasEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.removeListener


setAspectRatio(ratio): void

Set aspect ratio

Responsive canvas will preserve provided aspect ratio and resize itself within container according to fitting mode.

number

Aspect ratio

void


setMaxListeners(n): this

Sets maximum number of listeners per event

number

Maximum number of listeners

this

This EventEmitter

EventEmitterT.setMaxListeners


setMirror(mirror): void

Set mirror mode

ResponsiveCanvas mirrors the output layers.

boolean

Mirror the output

void


setMode(mode): void

Set fitting mode

Canvas layers preserve aspect ratio according to fitting mode, by cropping, fitting, or padding.

CanvasMode

Fitting mode

void


protected updateSizes(containerRatio): void

Update canvas sizes to preserve aspect ratio

Updates relative sizes of canvas within container to preserve fixed aspect ratio according to fitting mode. Called when container is resized (by resize observer).

number

Aspect ratio of the container

void