Skip to content

Text

A Text element renders a text string at a given position. Unlike other built-in elements, Text extends Element directly (not Shape) because it uses the context's text rendering API rather than a path.

Example

Text on Path

Text can be rendered along an arbitrary path by providing a pathData string (an SVG path d attribute). The text follows the curve of the path, with each character positioned and rotated to match the path direction. This works with both SVG and Canvas contexts.

Use startOffset (0–1) to control where along the path the text begins.

Usage

ts
import {
    createText,
} from '@ripl/core';

const text = createText({
    fillStyle: '#333333',
    x: 100,
    y: 100,
    content: 'Hello, Ripl!',
    font: '24px sans-serif',
    textAlign: 'center',
    textBaseline: 'middle',
});

Properties

PropertyTypeRequiredDescription
xnumberYesX coordinate
ynumberYesY coordinate
contentstring | numberYesThe text to render
pathDatastringNoSVG path d string to render text along
startOffsetnumberNoPosition along the path to start text (0–1)

The following inherited style properties are particularly relevant for text:

PropertyTypeDescription
fontstringCSS font string (e.g. '16px sans-serif')
textAlign'start' | 'end' | 'left' | 'right' | 'center'Horizontal alignment relative to x
textBaseline'top' | 'hanging' | 'middle' | 'alphabetic' | 'ideographic' | 'bottom'Vertical alignment relative to y
fillStylestringText fill color (renders filled text)
strokeStylestringText stroke color (renders stroked/outlined text)

NOTE

If strokeStyle is set, the text is stroked (outlined). If fillStyle is set, the text is filled. If both are set, strokeStyle takes priority.