CanvasSpace

CanvasSpace is an implementation of the abstract class Space. It represents a space for HTML Canvas. Learn more about the concept of Space in this guide.

Class extends MultiTouchSpace in src/Canvas.ts

Constructor

new CanvasSpace ( elem, callback )

Create a CanvasSpace which represents a HTML Canvas Space

parameters
elem: string | Element

Specify an element by its "id" attribute as string, or by the element object itself. An element can be an existing <canvas>, or a <div> container in which a new <canvas> will be created. If left empty, a <div id="pt_container"><canvas id="pt" /></div> will be added to DOM. Use css to customize its appearance if needed.

callback: Function

an optional callback function(boundingBox, spaceElement) to be called when canvas is appended and ready. Alternatively, a "ready" event will also be fired from the <canvas> element when it's appended, which can be traced with spaceInstance.canvas.addEventListener("ready")

returns
An instance of CanvasSpace
example

new CanvasSpace( "#myElementID" )

source

Accessors

GET SET autoResize : boolean

Set whether the canvas element should resize when its container is resized.

a boolean value indicating if auto size is set

GET SET background : string

Set a background color for this canvas. Alternatively, you may use clear() function.

background color as hex or rgba string

GET ctx : PtsCanvasRenderingContext2D

Get the rendering context of canvas

GET element : HTMLCanvasElement

Get the html canvas element

GET hasOffscreen : boolean

Check if an offscreen canvas is created

GET offscreenCanvas : HTMLCanvasElement

Get the offscreen canvas element

GET offscreenCtx : PtsCanvasRenderingContext2D

Get the rendering context of offscreen canvas (if created via setup())

GET parent : Element

Get the parent element that contains the canvas element

GET pixelScale : number

pixelScale property returns a number that let you determine if the screen is "retina" (when value >= 2)

GET ready : boolean

A property to indicate if the Space is ready

GET center : Pt

The center of this space's bounding box.

Inherited from Space.center in src/Space.ts

GET SET customRendering : Fn(context:any, self:Space)

Set a custom rendering function fn(graphics_context, canvas_space) if needed.

Inherited from Space.customRendering in src/Space.ts

GET height : number

The height of this space's bounding box.

Inherited from Space.height in src/Space.ts

GET innerBound : Bound

The inner bounding box of the space, excluding its positions.

Inherited from Space.innerBound in src/Space.ts

GET isPlaying : boolean

Indicate whether the animation is playing.

Inherited from Space.isPlaying in src/Space.ts

GET outerBound : Bound

The outer bounding box which includes its positions.

Inherited from Space.outerBound in src/Space.ts

GET pointer : Pt

Get the mouse or touch pointer that stores the last action.

Inherited from MultiTouchSpace.pointer in src/Space.ts

GET size : Pt

The size of this space's bounding box.

Inherited from Space.size in src/Space.ts

GET width : number

The width of this space's bounding box.

Inherited from Space.width in src/Space.ts

Methods

clear ( bg )

Clear the canvas with its background color. Overrides Space's clear function.

parameters
bg: string

Optionally specify a custom background color in hex or rgba string, or "transparent". If not defined, it will use its bgcolor property as background color to clear the canvas.

returns
this

source
Overrides Space.clear in src/Canvas.ts

clearOffscreen ( bg )

Similiar to clear() but clear the offscreen canvas instead

parameters
bg: string

Optionally specify a custom background color in hex or rgba string, or "transparent". If not defined, it will use its bgcolor property as background color to clear the canvas.

returns
this

source

dispose ( )

Dispose of browser resources held by this space and remove all players. Call this before unmounting the canvas.

returns
this

source

getForm ( )

Get a new CanvasForm for drawing

returns
CanvasForm

see

CanvasForm

source
Overrides Space.getForm in src/Canvas.ts

playItems ( time )

Main animation function.

parameters
time: number

current time

returns
void

source
Overrides Space.playItems in src/Canvas.ts

recorder ( downloadOrCallback, filetype, bitrate )

Get a MediaRecorder to record the current CanvasSpace. You can then call its start() function to start recording, and stop() to either download the video file or handle the blob data in the callback function you provided.

parameters
downloadOrCallback: boolean | Fn(blobURL:string)

Either true to download the video, or provide a callback function to handle the Blob data, when recording is completed.

filetype: string (default value: "webm")

video format. Default is "webm".

bitrate: number (default value: 15000000)

bitrate per second

returns
MediaRecorder

example

let rec = space.recorder(true); rec.start(); setTimeout( () => rec.stop(), 5000); // record 5s of video and download the file

source

resize ( b, evt )

This overrides Space's resize function. It's used as a callback function for window's resize event and not usually called directly. You can keep track of resize events with resize: (bound ,evt) callback in your player objects.

parameters
b: Bound

a Bound object to resize to

evt: Event

Optionally pass a resize event

returns
this

see

Space.add

source
Overrides Space.resize in src/Canvas.ts

setup ( opt )

Set up various options for CanvasSpace. The opt parameter is an object with the following fields. This is usually set during instantiation, eg new CanvasSpace(...).setup( { opt } )

parameters
opt: CanvasSpaceOptions

a CanvasSpaceOptions object with optional settings, ie { bgcolor:string, resize:boolean, retina:boolean, offscreen:boolean, pixelDensity:number }.

returns
this

example

space.setup({ bgcolor: "#f00", retina: true, resize: true })

source

add ( p )

Add an IPlayer object or a AnimateCallbackFn callback function to handle events in this Space. An IPlayer is an object with the following callback functions:

  • required: animate: fn( time, ftime, space )
  • optional: start: fn(bound, space)
  • optional: resize: fn( size, event )
  • optional: action: fn( type, x, y, event ) Subclasses of Space may define other callback functions.
parameters
p: IPlayer | AnimateCallbackFn

returns
this

source
Inherited from Space.add in src/Space.ts

bindCanvas ( evt, callback, options, customTarget )

Bind event listener in canvas element. You can also use MultiTouchSpace.bindMouse or MultiTouchSpace.bindTouch to bind mouse or touch events conveniently.

parameters
evt: string

an event string such as "mousedown"

callback: EventListener

callback function for this event

options: any (default value: {})

options for addEventListener.

customTarget: Element

If needed, this is an optional parameter to set another event target that's not the canvas element itself. See Technical Notes guide for use cases.

returns
void

source
Inherited from MultiTouchSpace.bindCanvas in src/Space.ts

bindDoc ( evt, callback, options )

parameters
evt: string

callback: EventListener

options: any (default value: {})

returns
void

source
Inherited from MultiTouchSpace.bindDoc in src/Space.ts

bindKeyboard ( bind )

parameters
bind: boolean (default value: true)

returns
this

source
Inherited from MultiTouchSpace.bindKeyboard in src/Space.ts

bindMouse ( bind, customTarget )

A convenient method to bind (or unbind) all mouse events in canvas element. All IPlayer objects added to this space that implement an action callback property will receive mouse event callbacks. The types of mouse actions are defined by UIPointerActions constants: "up", "down", "move", "drag", "drop", "over", and "out".

parameters
bind: boolean (default value: true)

a boolean value to bind mouse events if set to true. If false, all mouse events will be unbound. Default is true.

customTarget: Element

If needed, this is an optional parameter to set another event target that's not the canvas element itself. See Technical Notes guide for use cases.

returns
this

source
Inherited from MultiTouchSpace.bindMouse in src/Space.ts

bindTouch ( bind, passive, customTarget )

A convenient method to bind (or unbind) all touch events in canvas element. All IPlayer objects added to this space that implement an action callback property will receive touch event callbacks. The types of mouse actions are defined by UIPointerActions constants: "up", "down", "move", "drag", "drop", "over", and "out".

parameters
bind: boolean (default value: true)

a boolean value to bind touch events if set to true. If false, all mouse events will be unbound. Default is true.

passive: boolean (default value: false)

a boolean value to set passive mode, ie, it won't block scrolling. Default is false.

customTarget: Element

If needed, this is an optional parameter to set another event target that's not the canvas element itself. See Technical Notes guide for use cases.

returns
this

source
Inherited from MultiTouchSpace.bindTouch in src/Space.ts

minFrameTime ( ms )

Set a minimum frame time

parameters
ms: number (default value: 0)

at least this amount of miniseconds must have elapsed before frame advances

returns
void

source
Inherited from Space.minFrameTime in src/Space.ts

pause ( toggle )

Pause the animation.

parameters
toggle: boolean (default value: false)

a boolean value to set if this function call should be a toggle (between pause and resume)

returns
this

source
Inherited from Space.pause in src/Space.ts

play ( time )

Main play loop. This implements window.requestAnimationFrame and calls it recursively. You may override this play() function to implement your own animation loop.

parameters
time: number (default value: 0)

current time

returns
this

source
Inherited from Space.play in src/Space.ts

playOnce ( duration )

Play animation loop once. Optionally set a duration time to play for that specific duration.

parameters
duration: number (default value: 0)

a value in millisecond to specify a time period to play before stopping, or -1 to play forever

returns
this

source
Inherited from Space.playOnce in src/Space.ts

refresh ( b )

Set whether the rendering should be repainted on each frame.

parameters
b: boolean

a boolean value to set whether to repaint each frame

returns
this

source
Inherited from Space.refresh in src/Space.ts

remove ( player )

Remove a player from this Space.

parameters
player: IPlayer

an IPlayer that has an animateID property

returns
this

source
Inherited from Space.remove in src/Space.ts

removeAll ( )

Remove all players from this Space.

returns
this

source
Inherited from Space.removeAll in src/Space.ts

render ( context )

Custom rendering.

parameters
context: any

rendering context

returns
this

source
Inherited from Space.render in src/Space.ts

replay ( )

Replay the animation after Space.stop. This resets the end-time counter. You may also use Space.pause and resume for temporary pause.

returns
void

source
Inherited from Space.replay in src/Space.ts

resume ( )

Resume the pause animation.

returns
this

source
Inherited from Space.resume in src/Space.ts

stop ( t )

Specify when the animation should stop: immediately, after a time period, or never stops.

parameters
t: number (default value: 0)

a value in millisecond to specify a time period to play before stopping, or -1 to play forever, or 0 to end immediately. Default is 0 which will stop the animation immediately.

returns
this

source
Inherited from Space.stop in src/Space.ts

touchesToPoints ( evt, which )

A convenient method to convert the touch points in a touch event to an array of Pts.

parameters
evt: TouchEvent

a touch event which contains touches, changedTouches, and targetTouches list

which: TouchPointsKey (default value: "touches")

a string to select a touches list: "touches", "changedTouches", or "targetTouches". Default is "touches"

returns
Pt[]

an array of Pt, whose origin position (0,0) is offset to the top-left of this space

source
Inherited from MultiTouchSpace.touchesToPoints in src/Space.ts

unbindCanvas ( evt, callback, options, customTarget )

Unbind a callback from the event listener.

parameters
evt: string

an event string such as "mousedown"

callback: EventListener

callback function to unbind

options: any (default value: {})

options for removeEventListener. This should match the options set in bindCanvas.

customTarget: Element

If customTarget is set in bindCanvas, you'll need to pass the same instance here to unbind

returns
void

source
Inherited from MultiTouchSpace.unbindCanvas in src/Space.ts

unbindDoc ( evt, callback, options )

parameters
evt: string

callback: EventListener

options: any (default value: {})

returns
void

source
Inherited from MultiTouchSpace.unbindDoc in src/Space.ts