Skip to content

ShapeFactory

Factory class for registering shape builders and producing shape objects that can be rendered as SVG or PNG.

constructor

Signature

js
new ShapeFactory (options)

Description

Creates a new ShapeFactory instance with three internal LRU caches: one for the shape builder registry, one for SVG output, and one for PNG output.

Parameters

NameTypeRequiredDescription
optionsobjectnoConfiguration options
options.registrySizenumbernoMaximum number of entries in the shape registry. Defaults to 100
options.svgCacheSizenumbernoMaximum number of entries in the SVG cache. Defaults to 100
options.pngCacheSizenumbernoMaximum number of entries in the PNG cache. Defaults to 100

Examples

js
// Default cache sizes
const factory = new ShapeFactory()

// Custom cache sizes
const factory = new ShapeFactory({ registrySize: 50, svgCacheSize: 200, pngCacheSize: 200 })

list

Signature

js
factory.list()

Description

Returns an array of all registered shape type keys.

Returns

TypeDescription
string[]The list of registered shape type identifiers

Examples

js
factory.register('circle', buildCircle)
factory.register('rect', buildRect)

factory.list()
// ['circle', 'rect']

has

Signature

js
factory.has (type)

Description

Returns true if a shape builder is registered under the given type key.

Throws if type is not a string.

Parameters

NameTypeRequiredDescription
typestringyesThe shape type identifier to look up

Returns

TypeDescription
booleantrue if the type is registered, false otherwise

Examples

js
factory.has('circle')
// false

factory.register('circle', buildCircle)
factory.has('circle')
// true

register

Signature

js
factory.register (type, buildFn)

Description

Registers a builder function under the given shape type key. If a builder is already registered for that type, it is replaced.

Throws if type is not a string or if buildFn is not a function.

Parameters

NameTypeRequiredDescription
typestringyesA unique identifier for the shape type (e.g. 'circle', 'rect')
buildFnfunctionyesA function that receives build params and returns shape geometry (width, height, margin)

Examples

js
factory.register('circle', (params) => ({
  width: params.size ?? 32,
  height: params.size ?? 32,
  margin: 4
}))

build

Signature

js
factory.build (params)

Description

Builds a shape object from the given params. Looks up the registered builder for params.shape, calls it, and merges the result with the original params. The returned object exposes toSVG() and toPNG() render methods.

Throws if:

  • params does not have a shape property
  • params.shape is not a registered type
  • params.zoom is provided but is not a positive number
  • The builder function does not return positive integer values for width, height, and margin

Parameters

NameTypeRequiredDescription
paramsobjectyesBuild parameters
params.shapestringyesThe registered shape type to build
params.sizenumber[]noSize of the shape as [width, height]. Defaults to [24, 24]
params.radiusnumbernoAlternate way to define size. Each shape converts a radius into a size
params.colorstringnoFill color. Any valid HTML color. Defaults to 'black'
params.opacitynumbernoFill opacity, from 0.0 (transparent) to 1.0 (opaque). Defaults to 1.0
params.strokeobjectnoStroke properties. See stroke sub-object below
params.iconobjectnoIcon element to group with the shape. See icon sub-object below
params.textobjectnoText element to group with the shape. See text sub-object below
params.stylestringnoA SVG style element to assign to the shape
params.zoomnumbernoOverall zoom factor. Must be a positive number. Defaults to 1

stroke sub-object

PropertyDescriptionDefault
widthWidth of the stroke
colorStroke color. Any valid HTML color. If set to transparent, all stroke properties are ignored'black'
opacityStroke opacity, from 0.0 to 1.01.0
capLine cap style at the end of open subpaths'round'
joinLine join style at path corners'round'
dashArrayDash pattern of dashes and gapsnone
dashOffsetOffset on the dash array rendering0
miterLimitMiter limit on the miter length to stroke width ratio4

icon sub-object

PropertyDescriptionDefault
classesIcon classes to display. If undefined or empty, all icon properties are ignoredundefined
colorIcon color. Any valid HTML color'black'
opacityIcon opacity, from 0.0 to 1.01.0
sizeFont size used to render the icon'1em'
transformSVG transform applied to the iconundefined

text sub-object

PropertyDescriptionDefault
labelText to display. If undefined or empty, all text properties are ignoredundefined
colorText color. Any valid HTML color'black'
opacityText opacity, from 0.0 to 1.01.0
sizeFont size'1em'
fontFont familydepends on user agent
faceFont style'normal'
weightFont weight'normal'
variantFont variant'normal'
transformSVG transform applied to the textundefined

Returns

A shape object with the following properties:

NameTypeDescription
shapestringThe shape type identifier
zoomnumberThe zoom factor used
widthnumberPositive integer width returned by the builder
heightnumberPositive integer height returned by the builder
marginnumberPositive integer margin returned by the builder
toSVGfunctionRenders the shape as SVG. Returns a string
toPNGfunctionRenders the shape as PNG. Returns a Buffer

Examples

js
factory.register('circle', (params) => ({
  width: 32,
  height: 32,
  margin: 4
}))

const shape = factory.build({ shape: 'circle', color: 'red' })
// { shape: 'circle', zoom: 1, color: 'red', width: 32, height: 32, margin: 4, toSVG: fn, toPNG: fn }

const svg = shape.toSVG()
const png = shape.toPNG()

// With zoom
const shape = factory.build({ shape: 'circle', zoom: 2 })
// { shape: 'circle', zoom: 2, width: 32, height: 32, margin: 4, toSVG: fn, toPNG: fn }