Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SVGManager

A manager class for interactive SVG's

Initializing

<!-- Rest of DOM... -->
<div id="svg-root">
     <!-- Here the SVG will be inserted -->
</div>
<!-- Rest of DOM... -->
import { SVGManager } from 'ts-svgmanager'

// This will initialize a interactive SVG in the container
const manager = new SVGManager().init('svg-root')

Examples

Example 1 - Rendering a circle

import { SVGManager } from 'ts-svgmanager'
import ViewBox from 'ts-svgmanager/helpers/ViewBox'
import { circle } from 'ts-svgmanager/Shapes'

// Initializing the SVGManager with a viewBox of '-30 -30 60 60'
const manager = new SVGManager()
    .init('svg-root')
    .viewBox(new ViewBox(-30, -30, 60, 60))

// Rendering a circle with a radius of 20 at (0,0)
manager.render(circle(20))

Example 2 - Rendering a custom cursor

import { SVGManager } from 'ts-svgmanager'
import ViewBox from 'ts-svgmanager/helpers/ViewBox'
import { circle } from 'ts-svgmanager/Shapes'

// Initializing the SVGManager with a viewBox of '-30 -30 60 60'
const manager = new SVGManager()
    .init('svg-root')
    .viewBox(new ViewBox(0, 0, 200, 200))
    .width(200)
    .height(200)
    .set('cursor', 'none') // Remove the normal cursor

// Rendering a circle with a radius of 10 at (0,0)
manager.render(circle(10).tag('custom-cursor'))

// Adding the onmousemove listener
manager.on('mousemove', (ev: MouseEvent, svgNode) => {
    // Get the position of the SVG element
    const svgX = svgNode.element.getBoundingClientRect().x,
        svgY = svgNode.element.getBoundingClientRect().y

    // Get the x and y of the mouse relative to the SVG
    const x = ev.clientX - svgX,
        y = ev.clientY - svgY

    // Move the cursor to this location
    manager.tagged('custom-cursor').forEach((cursor) => cursor.cx(x).cy(y))
})

Hierarchy

Index

Constructors

constructor

Accessors

attributes

  • get attributes(): AttributeMap

children

element

  • get element(): SVGElement

events

  • get events(): SVGManagerEventDefinition[]

id

  • get id(): string
  • The id connected to this SVGManager, also set as the DOM-id for the SVG element

    Returns string

innerText

  • get innerText(): string

tagName

  • get tagName(): SVGTag

tags

  • get tags(): string[]

Methods

animate

append

class

  • class(className: string): this

clean

  • clean(): void
  • Removes the rendered items, definitions, events and innerText

    Returns void

clearEvents

  • clearEvents(eventName?: SVGEventName): this

copy

cx

  • cx(val: AttributeValue): this

cy

  • cy(val: AttributeValue): this

define

  • define(node: SVGNode): SVGManagerDefinition

destruct

  • destruct(): void

equals

fill

  • fill(color: AttributeValue, opacity?: AttributeValue): this
  • Shortcut for setting fill attributes

    Parameters

    • color: AttributeValue
    • Optional opacity: AttributeValue

    Returns this

fillDef

  • fillDef(definition: SVGManagerDefinition, opacity?: AttributeValue): this

get

  • get(attr: SVGAttribute): AttributeValue | undefined

height

init

  • init(rootId: string): this
  • Initialize the SVGManager

    Parameters

    • rootId: string

      container-id in which to put the SVG

    Returns this

map

  • map(attr: SVGAttribute, f: (_value: AttributeValue) => AttributeValue): SVGNode
  • Mutates the SVGNode to change an attribute attr by putting its value through the function f. Then, it returns itself, for easy programming.

    Note

    If the attribute was not set, the call still succeeds but does nothing.

    Parameters

    • attr: SVGAttribute
    • f: (_value: AttributeValue) => AttributeValue
        • (_value: AttributeValue): AttributeValue
        • Parameters

          • _value: AttributeValue

          Returns AttributeValue

    Returns SVGNode

on

  • on(eventName: SVGEventName, func: SVGManagerEventHandler): this

r

  • r(radius: AttributeValue): this
  • Setter for the r attribute

    Parameters

    • radius: AttributeValue

    Returns this

removeChild

  • removeChild(index: number): this

removeChildren

  • removeChildren(): this

render

renderDef

  • renderDef(definition: SVGManagerDefinition, args?: undefined | { attributes?: { attrName: SVGAttribute; attrValue: AttributeValue }[]; events?: SVGManagerEventDefinition[]; tags?: string[] }): this
  • Renders a figure to the SVG using a definition string.

    Note

    Requires a definition to present for the figure ID otherwise it throws a Error

    Parameters

    • definition: SVGManagerDefinition

      Definition id string

    • Optional args: undefined | { attributes?: { attrName: SVGAttribute; attrValue: AttributeValue }[]; events?: SVGManagerEventDefinition[]; tags?: string[] }

      Optional arguments from the rendering, including the tags, attributes and events

    Returns this

set

  • set(attr: SVGAttribute, value: AttributeValue): this

shallowEquals

  • shallowEquals(node: SVGNode): boolean

stroke

  • stroke(color: AttributeValue, width?: AttributeValue, opacity?: AttributeValue): this
  • Shortcut for setting stroke attributes

    Parameters

    • color: AttributeValue
    • Optional width: AttributeValue
    • Optional opacity: AttributeValue

    Returns this

strokeDef

  • strokeDef(definition: SVGManagerDefinition, width?: AttributeValue, opacity?: AttributeValue): this
  • Shortcut for setting stroke attributes to a definition (See more at SVGManager.define)

    Parameters

    • definition: SVGManagerDefinition
    • Optional width: AttributeValue
    • Optional opacity: AttributeValue

    Returns this

tag

  • tag(tag: string): this

tagged

text

  • text(s: string): this

toHTML

  • toHTML(): SVGElement

toHash

  • toHash(): string

untag

  • untag(tag: string): this

viewBox

width

x

  • x(val: AttributeValue): this
  • Setter for the x attribute

    Parameters

    • val: AttributeValue

    Returns this

y

  • y(val: AttributeValue): this
  • Setter for the y attribute

    Parameters

    • val: AttributeValue

    Returns this

Generated using TypeDoc