Skip to content

Fix SVG keyframe collisions with dynamic precision#3

Merged
jackspirou merged 2 commits into
mainfrom
fix/svg-keyframe-precision
Aug 7, 2025
Merged

Fix SVG keyframe collisions with dynamic precision#3
jackspirou merged 2 commits into
mainfrom
fix/svg-keyframe-precision

Conversation

@jackspirou

Copy link
Copy Markdown
Member

Summary

This PR fixes jerky/incorrect SVG animations for large recordings by implementing dynamic percentage precision based on keyframe count.

Problem

When generating SVG animations with many keyframes (e.g., 4000+ for complex demos), the fixed 1-decimal precision for percentages caused collisions:

  • 4161 keyframes would collapse into only ~1000 unique percentages
  • Multiple keyframes had identical percentage values (e.g., five keyframes all at "20.6%")
  • Browsers couldn't determine which transform to apply, resulting in jerky or incorrect animations

Example from a real recording:

0.1% { transform: translateX(-8120px); }
0.1% { transform: translateX(-9280px); }  // Same percentage\!
0.1% { transform: translateX(-10440px); } // Browser confused\!

Solution

Implemented a new formatPercentage() function with dynamic precision that adapts to the number of keyframes:

Keyframe Count Precision Use Case
< 100 1 decimal Small demos, quick recordings
< 1,000 2 decimals Medium animations
< 10,000 3 decimals Large recordings
< 100,000 4 decimals Very large recordings
≥ 100,000 5 decimals Extreme cases

Results

  • No more collisions: Each keyframe gets a unique percentage
  • Optimal file size: Uses minimal precision needed (small demos stay small)
  • Automatic: No configuration required - adapts based on animation complexity
  • Smooth animations: Browsers can correctly interpolate between all states

Testing

  • Added comprehensive unit tests for formatPercentage()
  • Added tests verifying no collisions for various animation sizes
  • Tested with real-world recordings of different complexities
  • All existing tests continue to pass

Example Impact

For the problematic tokenizer demo (4161 keyframes):

  • Before: 1001 unique percentages → jerky animation
  • After: 4161 unique percentages → smooth animation
  • File size: Minimal increase (~1-2%) from extra decimal places

Related

This addresses rendering issues mentioned in the upstream discussion: charmbracelet#644

🤖 Generated with Claude Code

jackspirou and others added 2 commits August 6, 2025 18:58
Fixes jerky/incorrect animations in large SVG files by implementing
dynamic percentage precision based on keyframe count.

Problem:
- Large animations (4000+ keyframes) had multiple keyframes with same
  percentage values due to 1 decimal precision limit
- Browser couldn't determine which transform to apply, causing jerky rendering

Solution:
- Implemented formatPercentage() with dynamic precision:
  - < 100 keyframes: 1 decimal
  - < 1,000 keyframes: 2 decimals
  - < 10,000 keyframes: 3 decimals
  - < 100,000 keyframes: 4 decimals
  - >= 100,000 keyframes: 5 decimals
- Ensures unique percentages for all keyframes
- Optimizes file size by using minimal required precision

Results:
- Smooth animations for recordings of any size
- Optimal file sizes (small demos stay small)
- No configuration needed - automatically adapts

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Replace if statement with strings.TrimSuffix as suggested by staticcheck (S1017)
@jackspirou jackspirou merged commit 8584720 into main Aug 7, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant