CxJS

Text

import { Text } from 'cx/svg'; Copied

The Text component renders SVG text as a bounded object. The initial cursor position is the top-left corner of the bounding box. Use textAnchor and dominantBaseline attributes to position the text within its bounds.

This example demonstrates all 9 text alignment combinations using anchors, textAnchor, and dominantBaseline:

<div class="flex flex-wrap gap-2">
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="0 1 1 0" style="fill: lightblue" />
    <Text anchors="0 1 1 0" textAnchor="start" dominantBaseline="hanging">
      Top-left
    </Text>
  </Svg>
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="0 1 1 0.5" style="fill: lightblue" />
    <Text anchors="0 1 1 0.5" textAnchor="middle" dominantBaseline="hanging">
      Top-center
    </Text>
  </Svg>
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="0 1 1 1" style="fill: lightblue" />
    <Text anchors="0 1 1 1" textAnchor="end" dominantBaseline="hanging">
      Top-right
    </Text>
  </Svg>

  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="0.5 1 1 0" style="fill: lightblue" />
    <Text anchors="0.5 1 1 0" textAnchor="start" dominantBaseline="middle">
      Middle-left
    </Text>
  </Svg>
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="0.5 1 1 0.5" style="fill: lightblue" />
    <Text anchors="0.5 1 1 0.5" textAnchor="middle" dominantBaseline="middle">
      Center
    </Text>
  </Svg>
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="0.5 1 1 1" style="fill: lightblue" />
    <Text anchors="0.5 1 1 1" textAnchor="end" dominantBaseline="middle">
      Middle-right
    </Text>
  </Svg>

  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="1 1 1 0" style="fill: lightblue" />
    <Text anchors="1 1 1 0" textAnchor="start">
      Bottom-left
    </Text>
  </Svg>
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="1 1 1 0.5" style="fill: lightblue" />
    <Text anchors="1 1 1 0.5" textAnchor="middle">
      Bottom-center
    </Text>
  </Svg>
  <Svg style="width: 100px; height: 100px; border: 1px dashed #ddd">
    <Rectangle anchors="1 1 1 1" style="fill: lightblue" />
    <Text anchors="1 1 1 1" textAnchor="end">
      Bottom-right
    </Text>
  </Svg>
</div>

Alignment Guide

Horizontal alignment is controlled by textAnchor:

  • textAnchor="start" — align to left edge
  • textAnchor="middle" — center horizontally
  • textAnchor="end" — align to right edge

Vertical alignment is controlled by dominantBaseline:

  • dominantBaseline="hanging" — top alignment (text hangs below the anchor)
  • dominantBaseline="middle" — center vertically
  • Default (no value) — bottom alignment (baseline sits on the anchor)

Configuration

PropertyTypeDescription
anchorsstring/numberPosition within parent bounds. Format: "t r b l". See Svg for details.
offsetstring/numberTranslate edges in pixels. Format: "t r b l". See Svg for details.
marginstring/numberCSS-like margin. See Svg for details.
textAnchor / tastringHorizontal alignment: "start", "middle", or "end".
dominantBaseline / dbstringVertical alignment: "hanging", "middle", "auto", etc.
dxstringHorizontal offset from the anchor point.
dystringVertical offset from the anchor point.
fillstringText fill color.
colorIndexnumberIndex in the default color palette.