Remotion Rule: Measuring Text
Remotion skill rule: Measuring text dimensions, fitting text to containers, and checking overflow. Part of the official Remotion Agent Skill for programmatic video in React.
What it is
This is a Remotion skill rule that covers measuring text dimensions, fitting text to containers, and checking for overflow. It is part of the official Remotion Agent Skill for programmatic video creation in React.
Developers and AI agents building Remotion videos need precise text measurement to avoid clipping, overflow, or misaligned layouts. This rule provides the patterns and API calls to handle those cases.
How it saves time or tokens
Text measurement in HTML canvas or SVG requires boilerplate: creating off-screen elements, computing bounding boxes, handling font loading. This skill rule gives agents a ready-made pattern so they produce correct text-fitting code on the first attempt, saving token_estimate of approximately 500 tokens per invocation.
Instead of trial-and-error rendering, the agent applies the rule and generates a working composition without visual debugging.
How to use
- Include this skill rule in your Remotion agent configuration
- When composing text-heavy scenes, the agent references the rule to pick the right measurement API
- Use
measureText()from the canvas 2D context or Remotion's layout utilities to get bounding box dimensions - Apply conditional font sizing or truncation based on measured width vs container width
Example
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '48px Inter';
const metrics = ctx.measureText('Hello Remotion');
const textWidth = metrics.width;
const textHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
if (textWidth > containerWidth) {
// Reduce font size or truncate
const scale = containerWidth / textWidth;
ctx.font = `${Math.floor(48 * scale)}px Inter`;
}
Related on TokRepo
- Coding tools -- AI-powered coding and video development
- Design tools -- Visual design and layout tooling
Common pitfalls
- Measuring text before fonts are loaded returns incorrect widths; always wait for
document.fonts.ready - Canvas measureText does not account for line-height or letter-spacing; add manual padding
- Remotion renders frames server-side, so DOM-based measurement must happen during the render lifecycle, not in module scope
Frequently Asked Questions
CSS auto-sizing works in browsers but Remotion renders frames programmatically. You need explicit pixel dimensions to ensure consistent output across all frames. Canvas measureText gives you those exact numbers before rendering.
Yes, but you must ensure the font is loaded before measuring. Use the Font Face API or Remotion's font loading utilities to preload the font, then measure. Measuring before the font loads will fall back to a default font and produce wrong dimensions.
Canvas measureText handles single lines. For multi-line text, split by newlines, measure each line individually, and sum the heights. Add line-height spacing between lines. Alternatively, use an off-screen DOM element with the same CSS for automatic wrapping measurement.
Canvas measureText is fast, typically under 1 millisecond per call. It is safe to call per frame if needed. The DOM-based approach is slower due to layout reflow, so prefer canvas measurement for per-frame calculations.
The Remotion Agent Skill is a collection of rules that guide AI agents in building Remotion videos. This specific rule covers text measurement, one of the most common tasks. Other rules handle animation timing, composition structure, and asset loading.
Citations (3)
- Remotion GitHub— Remotion is a framework for creating videos programmatically using React
- MDN Web Docs— Canvas measureText returns a TextMetrics object with width and bounding box info
- MDN Web Docs— Font Face API enables programmatic font loading detection
Related on TokRepo
Source & Thanks
Created by Remotion. Licensed under MIT. remotion-dev/skills — Rule:
measuring-text
Part of the Remotion AI Skill collection on TokRepo.
Discussion
Related Assets
Claude-Flow — Multi-Agent Orchestration for Claude Code
Layers swarm and hive-mind multi-agent orchestration on top of Claude Code with 64 specialized agents, SQLite memory, and parallel execution.
ccusage — Real-Time Token Cost Tracker for Claude Code
CLI that reads ~/.claude logs and breaks down Claude Code token spend by day, session, and project — pluggable into your statusline.
SuperClaude — Workflow Framework for Claude Code
Adds 16+ slash commands, 9 cognitive personas, and a smart flag system to Claude Code in one pipx install.