SVG getNumberOfChars() Method
getNumberOfChars() returns the total character count in SVG text, the essential loop bound for per-character text processing.
Drop your image here
Supports PNG, JPG, BMP, WEBP up to 5MB
Counting Characters in SVG Text
Calling getNumberOfChars() on an SVG text element returns the total number of addressable characters in its content, which provides the loop boundary needed when iterating over each character to apply per-character measurements or effects using methods like getExtentOfChar() or getStartPositionOfChar().
- getNumberOfChars() returns the total addressable character count
- Provides the loop boundary for per-character iteration
- Pairs with getExtentOfChar() and similar per-character methods
Using getNumberOfChars() in Per-Character Loops
The typical pattern calls getNumberOfChars() once to establish the total, then loops from 0 to that count, calling a per-character method inside the loop to measure or position each character individually — the foundation for building staggered animations, per-character interactivity, or precise custom text layout.
- Call getNumberOfChars() once to get the total, then loop over each index
- Inside the loop, call per-character methods to measure or position each one
- Foundation for staggered animations and per-character interactivity
Frequently Asked Questions
Does getNumberOfChars() count whitespace and spaces as characters?
Yes, it counts all addressable characters in the text content including spaces, so a per-character loop based on it will include space characters unless you specifically skip them.
Does getNumberOfChars() include characters across multiple tspan children?
It returns the count for the addressable characters of the text content it's called on, so behavior across nested tspan elements is worth testing for your specific structure if you rely on precise counts.
Related guides
Ready to Convert Your Image to SVG?
Free online converter — no sign-up, no watermarks, results in under 3 seconds.
Try It Free — Convert Image to SVG