Skip to content

graphiks

Graphiks is a lightweight and extensible library for creating, composing, and rendering parametric shapes using SVG. It provides a simple factory pattern that lets you register your own shape generators and render them as SVG elements.

Principle

Graphics provides a simple factory pattern that lets you register your own shape generators and render them as SVG elements. By default, Graphiks comes with a set of predefined marker shapes: circle, rect, rounded-rect, diamond and so on... The complete list of shapes is available here.

Any shapes can be customized with the following specifications:

PropertyDescriptionDefault
shapespecifies the shape identifier to create
sizespecifies the size of the shape. It must be an array of positive numbers.[24, 24]
radiusspecifies an alternate way to define the size of the shape. Each shape implements a function that converts a radius into a size.`undefined'
colorspecifies the color used to render the shape. It must be any HTML color.black
opacityspecifies the opacity used to render the shape. It must ranges from 0.0 (transparent) to 1.0 (opaque)1.0
strokespecifies the stroke parameters to render the shape. Refer to the description above.undefined
iconspecifies an icon element to be grouped with the shape. Refer to the description above.undefined
textspecifies an text element to be grouped with the shape. Refer to the description above.undefined
stylespecifies a style element to be assigned to the shapeundefined
zoomspecifies the overall zoom used to render the shape. It must be a positive number.1

stroke sub-object

PropertyDescriptionDefault
widthspecifies the width used to render the stroke.
colorspecifies the color used to render the stroke. It must be any HTML color.black
opacityspecifies the opacity used to render the stroke. It must ranges from 0.0 (transparent) to 1.0 (opaque).1.0
capspecifies the style to be used at the end of open subpaths.round
joinspecifies the style to be used at the corners of paths.round
dashArrayspecifies the pattern of dashes and gaps used to render the stroke.none
dashOffsetspecifies an offset on the rendering of the associated dash array.0
miterLimitspecifies a  limit on the ratio of the miter length to the stroke width used to draw a miter join.4

NOTE

If the color is set to transparent, the stroke properties are ignored.

icon sub-object

PropertyDescriptionDefault
classesspecifies the text to be displayedundefined
colorspecifies the color used to render the icon. It must be any HTML color.black
opacityspecifies the opacity used to render the icon. It must ranges from 0.0 (transparent) to 1.0 (opaque)1.0
sizespecifies the font size used to render the icon1em
transformspecifies the transformation used to render the iconundefined

NOTE

If the classes property is undefined or empty, the icon properties are ignored.

text sub-object

PropertyDescriptionDefault
labelspecifies the text to be displayedundefined
colorspecifies the color used to render the text. It must be any HTML color.black
opacityspecifies the opacity used to render the text. It must ranges from 0.0 (transparent) to 1.0 (opaque)1.0
sizespecifies the font size used to render the text1em
fontspecifies the font family used to render the textdepends on user agent
facespecifies the font face used to render the textnormal
weightspecifies the font weight used to render the textnormal
variantspecifies the font variant used to render the textnormal
transformspecifies the transformation used to render the textundefined

NOTE

If the label property is undefined or empty, the text properties are ignored.

For instance, to create an orange four-pointed star with a red stroke, it’s as simple as this:

js
import { Graphiks } from '@kalisio/graphiks'
// create a factory instance
const graphiks = Graphiks()
// create the shape with the required paramaters
const star4 = graphiks('star4', { fill: 'orange', stroke: { red } })
// add the shape to the dom
const container = document.getElementById('#star4-container')
container.appendChild(star4.toSVG())

IMPORTANT

Some shapes expose additional properties that allow further customization.

Installation

Install with your preferred package manager:

shell
pnpm add @kalisio/graphiks
shell
npm install @kalisio/graphiks
shell
yarn add @kalisio/graphiks

Or use it directly from a CDN:

html
<script type="module">
  import { Graphiks } from 'https://unpkg.com/@kalisio/graphiks/dist/graphiks.es.js'
</script>

Examples

Open the live example page in your browser