SVG Animations Reference Notes

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

Coordinate size (viewBox): The "drawing paper" dimensions -> viewBox="0 0 100 100"
Display size (width/height): How many pixels it takes on screen -> width="200px"
Key insight: Shapes use viewBox coordinates, SVG scales them to display size
All SVG shapes start as paths (outlines) that get filled/stroked:
fill = color inside the shape
stroke = outline color
stroke-width = outline thickness
<g><g stroke="red" fill="none" stroke-width="2">
<rect x="10" y="10" width="50" height="30" />
<circle cx="50" cy="50" r="20" />
</g>
Shared attributes apply to all children
Like a "config wrapper" (similar to framer-motion patterns)
Transforms applied to groups affect all children
.element {
transform: translateY(-2px); /* Move up 2 units */
transform: translateX(10px); /* Move right 10 units */
transform: rotate(45deg); /* Rotate 45 degrees */
}
/* ✅ Correct -> CSS requires explicit units */
transform: translateY(-2px);
/* ❌ Invalid -> CSS won't accept unitless values */
transform: translateY(-2);
Note: -2px means "2 units in viewBox coordinates", not screen pixels
Default transform-origin in SVG = 0,0 (top-left of viewBox)
NOT relative to the element being transformed!
.lid {
transform: rotate(120deg);
transform-origin: 18px 8px; /* Exact viewBox coordinates */
}
Must calculate exact coordinates where you want rotation center
Pain to maintain if elements move
.element {
transform-origin: center;
transform-box: fill-box; /* Makes origin relative to element */
}
Options:
view-box (default) -> relative to viewBox
fill-box -> relative to element's filled area
stroke-box -> includes stroke in calculation
Limitations:
Poor browser support (Safari issues, Firefox stroke-box)
Doesn't work with groups -> only individual shapes
.element {
transition: transform 0.2s;
}
.element:hover {
transform: translateY(-5px);
}
.element {
transition: transform 0.2s, fill 0.3s;
}
✅ Presentation attributes:
stroke, fill, opacity, stroke-width✅ Some geometry properties:
x, y, cx, cy, r (limited browser support)❌ Line-specific attributes:
x1, y1, x2, y2 -> NOT valid CSS propertiesUse transforms instead of direct coordinate changes:
/* Instead of changing y1, y2 coordinates */
.line {
transform: translateY(-2px);
}
Hard: Structuring SVG for animation (grouping, coordinates)
Easy: Adding the actual animation (transitions)
Annoying: Transform origins and coordinate calculations
Group related elements with <g>
Use transforms over direct coordinate changes
Plan transform origins early
Test across browsers (transform-box issues)
<!-- Zoom in: smaller viewBox area -->
<svg viewBox="25 25 50 50">
<!-- Pan right: move starting coordinates -->
<svg viewBox="50 0 100 100">
<!-- Wide shot: larger viewBox area -->
<svg viewBox="0 0 200 100">
Zoom effects by changing viewBox dimensions
Pan by changing x,y coordinates
Cinematic effects with smooth transitions
Perfect for React/TS library development
Transform origin confusion: Default is viewBox (0,0), not element center
CSS units required: Must use px even for SVG coordinates
Browser support: transform-box has issues
Groups vs individuals: Some properties only work on individual shapes
Coordinate calculation: Manual math required for complex origins