KeyframeStrokeText
SVG text effect that draws the stroke first, reveals the fill later, and recalculates text metrics when typography inputs change.
Usage
Use short text and tune stroke/fill colors through props. Numeric fontSize values become pixels.
Loading demo...
Best Practices
- Use it for short phrases. Long sentences become hard to read and make SVG measurement/animation visually heavy.
- Keep the surrounding layout width predictable. The SVG height follows
fontSize, while the viewBox width follows measured text length. - Prefer semantic page headings outside the component when the animated text is decorative; use
textfor the visual label itself. - Avoid re-rendering the component on every keystroke in large lists. Each typography/text change schedules a measurement pass.
- Test custom web fonts in the target surface; font loading can change measured width after the first paint.
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
text | string | '' | Text content rendered into all SVG text layers. |
strokeColor | string | '#4C4CFF' | Color of the stroke phase (the outline that animates in first); pick a high-contrast value on dark backgrounds or the first stroke pass is nearly invisible. |
fillColor | string | '#111827' | Final text color revealed after the stroke animation completes. |
durationMs | number | 1800 | Animation duration in milliseconds. Used by both stroke and fill animations. |
strokeWidth | number | 2 | SVG stroke width; also drives viewBox padding, so large values (e.g. above 3) visibly change the laid-out size. |
fontSize | string | number | 64 | Font size. Numbers are converted to px; strings are passed through. |
fontWeight | string | number | 700 | SVG font weight; heavier weights thicken the glyph outline and make the stroke animation feel bolder. |
fontFamily | string | 'inherit' | SVG font family. |
Events
No events are emitted.
Slots
No slots are exposed. Text must come from the text prop so measurement and accessibility stay deterministic.
Multilingual Text
The component measures the rendered SVG text, so CJK copy and mixed-language labels can use the same API.
Loading demo...
Overview
- The root is an SVG with
role="img". - Non-empty
textis mirrored toaria-label; emptytextomitsaria-labeland renders a non-breaking-space fallback to keep measurement stable. - The component measures
getBBox()andgetComputedTextLength()after mount and after text, font, or stroke-width inputs change. document.fonts.readytriggers an additional measurement pass when the browser exposes the Font Loading API.- The measuring
<text>stays at the origin while the two painted layers take the computed offset, so every pass reads the same box and repeated measurements agree. strokeColor,fillColor,durationMs,strokeWidth, measured stroke length, font size, font weight, and font family are applied through CSS variables.- The component respects
prefers-reduced-motion: reduceby disabling the animations and showing the final state.
Technologies
- Accessibility note: The source always renders an SVG with
role="img"; non-emptytextbecomes the accessible name, but there is no prop foraria-hidden. Keep semantic headings or labels outside the component when the animation is decorative. - Measurement: the measuring layer used to take the same offset as the painted layers, so each pass after the first read back its own offset and cancelled it. Once
document.fonts.readyre-measured, the glyphs sat at x=0, y≈0 — above the viewBox and against its left edge — on every docs demo as well as the gallery. The unit test mocksgetBBox()with a constant, which is why it never showed. - Verified coverage:
keyframe-stroke-text.test.tschecks the accessible SVG/text layers, CSS variable mapping, empty-text fallback, metric updates after prop changes, and install registration. - Component source:
packages/tuffex/packages/components/src/keyframe-stroke-text/src/TxKeyframeStrokeText.vue. - Types:
packages/tuffex/packages/components/src/keyframe-stroke-text/src/types.tsexportsKeyframeStrokeTextProps. - Export alias:
packages/tuffex/packages/components/src/keyframe-stroke-text/index.tsexportsKeyframeStrokeText,TxKeyframeStrokeText,KeyframeStrokeTextProps, andTxKeyframeStrokeTextInstance. - Coverage:
packages/tuffex/packages/components/src/keyframe-stroke-text/__tests__/keyframe-stroke-text.test.tsverifies measurement-driven rendering and installation.
查看源码
packages/tuffex/packages/components/src/keyframe-stroke-text/index.ts