Skip to content

ShapeFactory

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

Built-in shape builders are automatically registered when the factory is created. See Built-in shapes for the complete list of available shapes and their specific parameters.

TIP

The complete collection can also be browsed and previewed in the Shape Studio.

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.

Built-in shape builders are automatically registered when the factory is created.

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.list()
// ['circle', 'cross', 'diamond', ...]

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')
// true

factory.has('custom-shape')
// false

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
buildFnfunctionyesFunction receiving build parameters and returning the shape definition

A builder must return at least the following properties:

NameTypeDescription
widthnumberPositive integer width of the shape
heightnumberPositive integer height of the shape
marginnumberNon-negative integer margin around the shape
shapestringSVG markup representing the shape

Examples

js
factory.register('custom-shape', (params) => ({
  width: 50,
  height: 50,
  margin: 0,
  shape: '<circle cx="50" cy="50" r="50" />'
}))

build

Signature

js
factory.build(params)

Description

Builds a shape object from the given parameters.

The factory looks up the builder registered for params.shape, invokes it, and merges the generated shape definition with the provided parameters.

The returned object exposes toSVG() and toPNG() rendering methods.

Throws if:

  • params does not have a shape property
  • params.shape is not a registered shape type
  • The generated width or height is not a positive integer
  • The generated margin is not a non-negative integer

Parameters

NameTypeRequiredDescription
paramsobjectyesBuild parameters
params.shapestringyesRegistered shape type to build
params.sizenumber[]noSize of the shape as [width, height]. Standard shapes default to 50 × 50 when neither size nor radius is provided
params.radiusnumbernoAlternate way to define the shape size. Conversion from radius to width and height depends on the shape
params.colorstringnoFill color. Any valid HTML color. Defaults to 'black'
params.opacitynumbernoFill opacity, from 0.0 to 1.0. 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.transformobjectnoSVG transform applied to the shape
params.stylestringnoSVG style element associated with the shape

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 displayundefined
urlURL of the image to display as the iconundefined
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

If text.label is omitted or empty, no text element is rendered.

PropertyDescriptionDefault
labelText content to displayundefined
sizeFont size before scaling to the shape height12
colorText fill color. Any valid HTML colorinherited
fontFont familyinherited
styleFont style, for example normal or italicinherited
weightFont weight, for example normal, bold, or a numeric weightinherited
variantFont variantinherited
cursorCSS cursor displayed when hovering the text, for example pointer, default, or textinherited
transformSVG transform applied to the textundefined

The text is horizontally centered and vertically aligned around its origin using:

svg
text-anchor="middle"
alignment-baseline="central"

The font size is automatically scaled according to the shape height.

js
text: {
  label: 'i',
  size: 12,
  color: 'white',
  font: 'Arial',
  weight: 'bold',
  cursor: 'pointer',
  transform: {
    translate: [50, 50]
  }
}

transform sub-object

The transform object defines SVG transformations applied to the shape.

PropertyDescription
rotateRotation as an array passed to the SVG rotate() transform, for example [angle] or [angle, cx, cy]
translateTranslation as an array passed to the SVG translate() transform, typically [x, y]
scaleScaling as an array passed to the SVG scale() transform, for example [factor] or [x, y]
skewXHorizontal skew angle in degrees
skewYVertical skew angle in degrees

Multiple transformations can be combined in the same object.

They are rendered in the following order: rotate, translate, scale, skewX, skewY.

js
transform: {
  rotate: [45, 50, 50],
  translate: [10, 20],
  scale: [0.8, 0.8]
}

style

The style property defines CSS rules embedded in the generated SVG.

It can be used to customize the rendering of SVG elements, for example to define hover states or other CSS-based effects.

js
style: `
  path:hover {
    opacity: 0.8;
  }
`