logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Canvas
    • Introduction
    • Options
    • Built-in objects
    • Coordinate system
    • Scenegraph & Lifecycle
    • Event
    • OffscreenCanvas & Server-side Rendering
    • CustomElementRegistry
    • Frequently Asked Questions
  • Renderer
    • Introduction
    • Canvas Renderer
    • Canvaskit Renderer
    • SVG Renderer
    • WebGL Renderer
    • WebGPU Renderer
    • Custom Renderer
  • Camera
    • Introduction
    • Camera Parameters
    • Camera action
    • Camera animation
  • Event
    • Introduction
    • Event Object
    • Gesture & Drag'n'Drop
    • Frequently Asked Questions
  • Animation
    • Web Animations API
    • Lottie
  • Basic Shapes
    • Basic Concepts
    • DisplayObject
    • Group
    • Text
    • Circle
    • Ellipse
    • Rect
    • Image
    • Line
    • Polygon
    • Polyline
    • Path
    • HTML
  • Style System
    • Introduction
    • CSS Typed OM
    • Inheritance
    • CSS Properties & Values API
    • CSS Layout API
    • Pattern
    • Gradient
  • 3D
    • 材质
    • 几何
    • Mesh
    • 光源
    • 雾
    • 交互
  • Built-in Objects
    • EventTarget
    • Node
    • Element
    • Document
    • MutationObserver
    • Utils
  • GPGPU
    • Introduction
    • Programming Model
    • Kernel API
    • Principles of classical GPGPU implementation
    • webgpu-graph
  • Declarative programming
    • 使用 Web Components
  • Devtools
    • G 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

Text

Previous
Group
Next
Circle

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Provides simple single/multi-line text layout capabilities, with single-line support for horizontal alignment and character spacing; multi-line support for explicit line breaks as well as automatic line breaks and vertical alignment.

The following properties can be adjusted in this example.

Inherited from

  • DisplayObject

The position of a text/text block is described by a text anchor point around which it adjusts itself through properties such as textBaseline (single/multi-line), textAlign (multi-line), etc.

text

Text content, which can contain line breaks, e.g. "test text \n another line"

Initial valueApplicable elementsInheritableAnimatableComputed value
''-nono<string>

textTransform

Consistent with CSS text-transform, the following enumeration values are supported for text content transformation.

  • 'capitalize'
  • 'uppercase'
  • 'lowercase'
  • 'none' default value
Initial valueApplicable elementsInheritableAnimatableComputed value
'none'-nono<keywords>

dx / dy

Corresponds to the SVG dx / dy attribute to add offsets in the horizontal and vertical directions.

Both px and em units are supported, and the default px unit when using the number type.

{
dx: 10;
dx: '10px';
dx: '0.5em';
}
Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<percentage> <length>

isBillboard

Whether or not to always face the camera in 3D scenes, defaults to false, also known as the "billboard effect".

In example, the text is rendered compressed when the camera is rotated without being turned on:

disable billboard effect

Turning it on doesn't change the position of the text, but it will always face the camera. This is in line with what is usually required for 2D graphics like text in 3D scenes:

enable billboard effect

billboardRotation

Rotation angle in billboard mode, clockwise in radians.

In example, we add a rotation angle to the text:

label.style.isBillboard = true;
label.style.billboardRotation = Math.PI / 8;

billboard rotation

isSizeAttenuation

Whether or not to apply size attenuation in perspective projection. This option can be turned on if you want to keep the size consistent regardless of depth, following the "near big, far small" visual effect in perspective projection.

In example, we enable size attenuation for text:

label.style.isSizeAttenuation = true;

enable size attenuation

Font Related

fontFamily

Font type, e.g. 'PingFang SC' 'Microsoft Yahei'

Corresponds to the CSS font-family.

Initial valueApplicable elementsInheritableAnimatableComputed value
''-yesno<keywords>

fontSize

Corresponds to the CSS font-size.

Initial valueApplicable elementsInheritableAnimatableComputed value
'16px'-yesyes<percentage> <length>

fontWeight

Corresponds to the CSS font-weight.

  • 'normal' Normal font weight. Same as 400.
  • 'bold' Bold font weight. Same as 700.
  • 'bolder'
  • 'lighter'
  • number A number value between 1 and 1000.
Initial valueApplicable elementsInheritableAnimatableComputed value
'normal'-yesno<keywords>

fontStyle

Corresponds to the CSS font-style.

Font style, for example, the image below shows the tilted italic effect.

fontStyle

Initial valueApplicable elementsInheritableAnimatableComputed value
'normal'-yesno<keywords>

fontVariant

Corresponds to the CSS font-variant.

Font style, for example, the following image is the small-cap effect.

fontVariant

Initial valueApplicable elementsInheritableAnimatableComputed value
'normal'-yesno<keywords>

Single row layout

textBaseline

Corresponds to the Canvas textBaseline.

Alignment in the vertical direction is achieved by textBaseline, and the following figure shows the effect of alignment with different values.

textBaseline

Using the current position of the text as the anchor point, the following figure shows the effect of top, middle and bottom in turn. In addition to single line also applies to multi-line text blocks.

textBaseline

Initial valueApplicable elementsInheritableAnimatableComputed value
'alphabetic'-yesno<keywords>

letterSpacing

Corresponds to the Canvas letterSpacing.

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-yesno<percentage> <length>

Multi-row layout

Line feeds occur in the following two cases:

  1. Line breaks in text
  2. When wordWrap is turned on, the part beyond wordWrapWidth will be automatically line wrapped, similar to word-break in CSS.

Therefore, both cases need to be considered when parsing raw text. However, when dealing with CJK (Chinese/Japanese/Korean) characters, their special language specification needs to be taken into account. In fact, the CSS word-break also provides a value that takes into account the CJK case.

textAlign

Corresponds to the CSS text-align.

In multi-line text, each line can be horizontally aligned with an anchor

  • 'start'
  • 'center'
  • 'end'
  • 'left' Same as 'start'.
  • 'right' Same as 'end'.

The following figure shows the effect of left, center and right in that order:

text align
Initial valueApplicable elementsInheritableAnimatableComputed value
'left'-yesno<keywords>

wordWrap

Whether to turn on automatic line feed, default value is false.

wordWrapWidth

When wordWrap is turned on, the line will break beyond that width.

wordWrapWidth
Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-yesno<length>

textOverflow

Used to determine how to prompt the user for the presence of hidden text overflow content, such as direct cropping, appending an ellipsis or a custom string. Need to be used with wordWrap, wordWrapWidth and [maxLines](/en/api/ basic/text#maxlines) are used together.

Corresponds to the CSS text-overflow.

The following values are supported.

  • 'clip' truncates the text directly
  • 'ellipsis' uses ... to indicate the truncated text
  • Custom strings, using it to indicate the truncated text

Caution.

  • 'clip' and 'ellipsis' are reserved words, so custom strings cannot use them.
  • If the length of custom text exceeds wordWrapWidth, it will be truncated directly, and the effect is the same as 'clip'.
  • The truncation only affects the visual effect, the original text content text is not affected
Initial valueApplicable elementsInheritableAnimatableComputed value
'clip'-nono<keywords>

maxLines

Max lines, text overflow will be truncated, need to use with wordWrap, wordWrapWidth and textOverflow are used together.

The following figure shows limiting text to be displayed on one line and truncated with an ellipsis after it is exceeded.

text overflow
Initial valueApplicable elementsInheritableAnimatableComputed value
'Infinity'-nono<number>

lineHeight

Corresponds to the CSS line-height.

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<length>

leading

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-nono<length>

Methods

getLineBoundingRects

Get the bounding box for each line of text, e.g.:

text.getLineBoundingRects(); // Rectangle[]

where the enclosing box structure is as follows, where x/y is relative to the local coordinate system of the text:

interface Rectangle {
x: number;
y: number;
width: number;
height: number;
}

In example, we draw the bounding box for each line of the multi-line text, and we can implement advanced text features such as underline and strikethrough based on the bounding box information:

getLineBoundingRects

text.getLineBoundingRects().forEach(({ x, y, width, height }) => {
const block = new Rect({
style: {
x,
y,
width,
height,
stroke: 'black',
lineWidth: 2,
},
});
text.appendChild(block);
});

isOverflowing

Used to determine if there is overflow content. Useful for Tooltip-like components to determine if the full text needs to be displayed.

text.isOverflowing(); // true

Note that the presence of a line break does not necessarily mean that there is overflow. For example, in the following figure, even though maxLines and wordWrapWidth are set, there is no content overflow and the method returns false.

no onverflowing

And only if the content does overflow, i.e. the textOverflow attribute does take effect (whatever its value is), will it return true.

text overflow

Loading Fonts

In addition to the system default fonts, sometimes we want to load third-party fonts.

In this case, you can use Web Font Loader, which is created in the active callback function when it is loaded successfully, example:

import WebFont from 'webfontloader';
WebFont.load({
google: {
families: ['Gaegu'],
},
active: () => {
const text = new Text({
style: {
x: 100,
y: 100,
fontFamily: 'Gaegu',
text: 'Almost before we knew it, we had left the ground.',
fontSize: 30,
fill: '#1890FF',
stroke: '#F04864',
lineWidth: 5,
},
});
canvas.appendChild(text);
},
});

More CanvasKit-based configuration items

CanvasKit provides enhanced paragraph drawing capabilities. We've integrated them into our g-canvaskit renderer.

FAQ

The bounding box acquisition is not accurate

Since many properties of Text are inheritable, this means that if their values are not explicitly passed in, the correct inherited value can only be obtained after adding the document, so as to calculate an accurate bounding box. If you really want to get the size of the bounding box before adding the document, for example, to get the exact bounding box immediately after instantiation, you need to manually pass in their values:

const text = new Text({
style: {
text: 'abcde',
},
});
text.getBounds(); // Wrong empty bounding box.
text.attr({
fontSize: '16px',
fontFamily: 'sans-serif',
fontWeight: 'normal',
fontVariant: 'normal',
fontStyle: 'normal',
textAlign: 'start',
textBaseline: 'alphabetic',
lineWidth: 0,
});
text.getBounds(); // Right bounding box.