Text Animations Tutorial
Note: this feature is only available from Konva v10.0.0.
Konva provides powerful text animation capabilities through the charRenderFunc
property. This function allows you to customize how each character is rendered, enabling character-by-character animations and effects.
var text = new Konva.Text({
x: 10,
y: 10,
text: 'AB',
fontSize: 20,
charRenderFunc: function ({ context, index }) {
if (index === 1) {
// shift only the second character
context.translate(0, 10);
}
},
});
The charRenderFunc
receives a context object with the following parameters:
char
- The actual character string being renderedindex
- Zero-based index of the character in the entire textx
- X position where the character will be renderedy
- Y position where the character will be renderedlineIndex
- Zero-based index of the line containing this charactercolumn
- Zero-based column position within the current lineisLastInLine
- Boolean indicating if this is the last character in its linewidth
- Width of the charactercontext
- Canvas 2D rendering context for applying transformations, opacity, colors, etc.
This allows you to apply transformations, opacity changes, or other effects to individual characters based on their position and properties.
- Vanilla
- React
- Vue