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.
Extends
Section titled “Extends”EventEmitterT
<CanvasEvents
>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ResponsiveCanvas(
container
,mode
,layerCount
,mirror
,aspectRatio
):ResponsiveCanvas
Constructor
Parameters
Section titled “Parameters”container
Section titled “container”HTMLElement
Container of responsive canvas
CanvasMode
= "crop"
Fitting mode
layerCount
Section titled “layerCount”number
= 1
Number of canvas layers
mirror
Section titled “mirror”boolean
= false
Mirror the output
aspectRatio
Section titled “aspectRatio”number
= 1
Target aspect ratio
Returns
Section titled “Returns”ResponsiveCanvas
Overrides
Section titled “Overrides”Properties
Section titled “Properties”aspectRatio
Section titled “aspectRatio”
protected
aspectRatio:number
=1
Target aspect ratio
container
Section titled “container”
protected
container:HTMLElement
Container of responsive canvas
layerCount
Section titled “layerCount”
protected
layerCount:number
=1
Number of canvas layers
layers
Section titled “layers”layers:
HTMLCanvasElement
[] =[]
Canvas layers
mirror
Section titled “mirror”
protected
mirror:boolean
=false
Mirror the output
protected
mode:CanvasMode
="crop"
Fitting mode
observer?
Section titled “observer?”
protected
optional
observer:ResizeObserver
Resize observer
optional
pads: [HTMLCanvasElement
,HTMLCanvasElement
]
Padding canvases
padsSize
Section titled “padsSize”padsSize: [
number
,number
]
Relative size of padding canvases
Methods
Section titled “Methods”addListener()
Section titled “addListener()”addListener<
E
>(event
,listener
):this
Adds the listener function to the end of the listeners array
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”dispose()
Section titled “dispose()”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.
Returns
Section titled “Returns”void
emit()
Section titled “emit()”emit<
E
>(event
, …args
):boolean
Synchronously calls each of the listeners registered for the event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
…Args
<CanvasEvents
[E
]>
Arguments passed to the listeners
Returns
Section titled “Returns”boolean
True if the event had listeners, False otherwise
Inherited from
Section titled “Inherited from”eventNames()
Section titled “eventNames()”eventNames(): (
string
|symbol
)[]
List of emitter’s events
Returns
Section titled “Returns”(string
| symbol
)[]
List of emitter’s events
Inherited from
Section titled “Inherited from”getMaxListeners()
Section titled “getMaxListeners()”getMaxListeners():
number
Maximum number of listeners per event
Returns
Section titled “Returns”number
Maximum number of listeners per event
Inherited from
Section titled “Inherited from”getMirror()
Section titled “getMirror()”getMirror():
boolean
Get mirror mode
Returns
Section titled “Returns”boolean
Current mirror mode
getMode()
Section titled “getMode()”getMode():
CanvasMode
Get fitting mode
Returns
Section titled “Returns”Current fitting mode
handleResize()
Section titled “handleResize()”
protected
handleResize(entries
):void
Resize event callback
Callback attached to resize observer to handle size updates. Basically, it calls updateSizes() to adjust canvas sizes.
Parameters
Section titled “Parameters”entries
Section titled “entries”ResizeObserverEntry
[]
Elements being resized
Returns
Section titled “Returns”void
listenerCount()
Section titled “listenerCount()”listenerCount<
E
>(event
):number
The number of listeners listening to the event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
Returns
Section titled “Returns”number
Number of listeners
Inherited from
Section titled “Inherited from”listeners()
Section titled “listeners()”listeners<
E
>(event
):Function
[]
Copy of the array of listeners for the event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
Returns
Section titled “Returns”Function
[]
Copy of the listeners array
Inherited from
Section titled “Inherited from”off<
E
>(event
,listener
):this
Removes the listener from the listener array for the event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”on<
E
>(event
,listener
):this
Adds the listener function to the event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”once()
Section titled “once()”once<
E
>(event
,listener
):this
Adds a one-time listener function for the event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”prependListener()
Section titled “prependListener()”prependListener<
E
>(event
,listener
):this
Adds the listener function to the beginning of the listeners array
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”prependOnceListener()
Section titled “prependOnceListener()”prependOnceListener<
E
>(event
,listener
):this
Adds a one-time listener function to the beginning of the listeners array
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
Inherited from
Section titled “Inherited from”EventEmitterT
.prependOnceListener
rawListeners()
Section titled “rawListeners()”rawListeners<
E
>(event
):Function
[]
Copy of the array of listeners for the event including wrappers
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
Returns
Section titled “Returns”Function
[]
Copy of the listeners array
Inherited from
Section titled “Inherited from”removeAllListeners()
Section titled “removeAllListeners()”removeAllListeners<
E
>(event?
):this
Removes all listeners, or those of the specified event
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”event?
Section titled “event?”E
The name of the event
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”EventEmitterT
.removeAllListeners
removeListener()
Section titled “removeListener()”removeListener<
E
>(event
,listener
):this
Removes the specified listener from the listener array
Type Parameters
Section titled “Type Parameters”E
extends "resize"
Parameters
Section titled “Parameters”E
The name of the event
listener
Section titled “listener”CanvasEvents
[E
]
The callback function
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”setAspectRatio()
Section titled “setAspectRatio()”setAspectRatio(
ratio
):void
Set aspect ratio
Responsive canvas will preserve provided aspect ratio and resize itself within container according to fitting mode.
Parameters
Section titled “Parameters”number
Aspect ratio
Returns
Section titled “Returns”void
setMaxListeners()
Section titled “setMaxListeners()”setMaxListeners(
n
):this
Sets maximum number of listeners per event
Parameters
Section titled “Parameters”number
Maximum number of listeners
Returns
Section titled “Returns”this
This EventEmitter
Inherited from
Section titled “Inherited from”setMirror()
Section titled “setMirror()”setMirror(
mirror
):void
Set mirror mode
ResponsiveCanvas mirrors the output layers.
Parameters
Section titled “Parameters”mirror
Section titled “mirror”boolean
Mirror the output
Returns
Section titled “Returns”void
setMode()
Section titled “setMode()”setMode(
mode
):void
Set fitting mode
Canvas layers preserve aspect ratio according to fitting mode, by cropping, fitting, or padding.
Parameters
Section titled “Parameters”Fitting mode
Returns
Section titled “Returns”void
updateSizes()
Section titled “updateSizes()”
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).
Parameters
Section titled “Parameters”containerRatio
Section titled “containerRatio”number
Aspect ratio of the container
Returns
Section titled “Returns”void