SVGSpace

[Experimental] SVGSpace extends DOMSpace to support SVG elements. Use it with SVGForm to express Pts in svg. You may easily switch between html canvas and svg renderings with minimal code changes. Check out the Space guide for details, and see a demo here.

Class extends DOMSpace in src/Svg.ts

Constructor

new SVGSpace ( elem, callback )

Create a SVGSpace which represents a Space for SVG elements.

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 <svg>, or a <div> container in which a new <svg> will be created. If left empty, a <div id="pt_container"><svg 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 <svg> element when it's appended, which can be traced with spaceInstance.canvas.addEventListener("ready")

returns
An instance of SVGSpace
example

new SVGSpace( "#myElementID" )

source
Overrides DOMSpace.__constructor in src/Svg.ts

Accessors

GET element : Element

Get the DOM element.

Overrides DOMSpace.element in src/Svg.ts

GET SET autoResize : boolean

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

Inherited from DOMSpace.autoResize in src/Dom.ts

a boolean value indicating if auto size is set

GET SET background : string

Set a background color on the container element.

Inherited from DOMSpace.background in src/Dom.ts

background color as hex or rgba string

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 parent : Element

Get the parent DOM element that contains this DOM element.

Inherited from DOMSpace.parent in src/Dom.ts

GET pointer : Pt

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

Inherited from MultiTouchSpace.pointer in src/Space.ts

GET ready : boolean

A property to indicate if the Space is ready.

Inherited from DOMSpace.ready in src/Dom.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

getForm ( )

Get a new SVGForm for drawing.

returns
SVGForm

see

SVGForm

source
Overrides DOMSpace.getForm in src/Svg.ts

remove ( player )

Remove an item from this Space.

parameters
player: IPlayer

returns
this

source
Overrides Space.remove in src/Svg.ts

removeAll ( )

Remove all items from this Space.

returns
this

source
Overrides Space.removeAll in src/Svg.ts

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 IPlayer objects (See Space.add).

parameters
b: Bound

a Bound object to resize to

evt: Event

Optionally pass a resize event

returns
this

source
Overrides DOMSpace.resize in src/Svg.ts

STATIC svgElement ( parent, name, id )

A static function to add a svg element inside a node. Usually you don't need to call this directly. See methods in SVGForm instead.

parameters
parent: Element

the parent element, or null to use current <svg> as parent.

name: string

a string of element name, such as rect or circle

id: string

id attribute of the new element

returns
SVGElement

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

clear ( bg )

Clear the element's contents, and optionally set a new background color. This 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 Inherited from DOMSpace.clear in src/Dom.ts

STATIC createElement ( elem, id, appendTo )

Helper function to create a DOM element.

parameters
elem: string (default value: "div")

element tag name

id: string

element id attribute

appendTo: Element

Optional, if specified, the created element will be appended to this element

returns
Element

source
Inherited from DOMSpace.createElement in src/Dom.ts

dispose ( )

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

returns
this

source
Inherited from DOMSpace.dispose in src/Dom.ts

STATIC getInlineStyles ( data )

A static helper function to compose an inline style string from a object of styles.

parameters
data: object

an object with key-value pairs

returns
string

exmaple

DOMSpace.getInlineStyles( {width: "100px", "font-size": "10px"} )

source
Inherited from DOMSpace.getInlineStyles in src/Dom.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

playItems ( time )

Main animate function. This calls all the items to perform.

parameters
time: number

current time

returns
void

source
Inherited from Space.playItems 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

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

STATIC setAttr ( elem, data )

A static helper function to add or update Element attributes.

parameters
elem: Element

Element to update

data: object

an object with key-value pairs

returns
Element

this DOM element

source
Inherited from DOMSpace.setAttr in src/Dom.ts

setup ( opt )

Set up various options for DOMSpace. This is usually set during instantiation, eg new DOMSpace(...).setup( {opt} ).

parameters
opt: { bgcolor:`string`, resize:`boolean` }

an object with these optional properties: bgcolor is a hex or rgba string to set initial background color of the canvas, or use false or "transparent" to set a transparent background; resize a boolean to set whether <canvas> size should auto resize to match its container's size, which can also be set using autoSize().

returns
this

example

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

source
Inherited from DOMSpace.setup in src/Dom.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

style ( key, val, update )

Add or update a style definition, and optionally update that style in the Element.

parameters
key: string

style name

val: string

style value

update: boolean (default value: false)

a boolean to update the element's style immediately if set to true. Default is false.

returns
this

source
Inherited from DOMSpace.style in src/Dom.ts

styles ( styles, update )

Add of update a list of style definitions, and optionally update those styles in the Element.

parameters
styles: object

a key-value objects of style definitions

update: boolean (default value: false)

a boolean to update the element's style immediately if set to true. Default is false.

returns
this

this

source
Inherited from DOMSpace.styles in src/Dom.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