🗓️ 05082025 0103
📎
svg_animate_transform
🧩 SMIL Basics
- SMIL allows declarative animation inside SVG without JavaScript.
- Animations are defined directly in the SVG using elements like
<animate>
and<animateTransform>
.
🛠️ animateTransform
- Used to animate transform properties (e.g., translate, scale, rotate).
- Attributes:
attributeName="transform"
targets the transform.type
specifies the transform type.to
sets the target value.dur
sets the duration.fill="freeze"
keeps the end state.
⏱️ Timing Control
begin
controls when the animation starts:- Accepts time values (
0.1s
) or event triggers (click
,endEvent
, etc.).
- Accepts time values (
endEvent
can be used to chain animations (e.g., animation B begins when animation A ends).
🖥️ Browser Compatibility
- SMIL is not supported in Safari.
- Workarounds (e.g., JavaScript) may be needed for broader compatibility.
🎨 SMIL vs CSS
Feature | SMIL | CSS |
---|---|---|
Declarative SVG animation | ✅ | ❌ |
Native timing/sequencing | ✅ | ⚠️ Needs JS |
Broad browser support | ❌ | ✅ |
JS-free triggering | ✅ | ❌ |
🔑 Key Concepts: Triggering SMIL with JavaScript & React
🧩 Manual Control of SMIL
-
Set
begin="indefinite"
to defer animation start. -
Use
.beginElement()
in JavaScript to manually trigger the animation.
⚙️ JavaScript Integration
js
CopyEdit
animateElement.beginElement();
- Use DOM methods to trigger SMIL from custom events (e.g., button clicks, state changes).
⚛️ React Integration
-
Use
useRef
to reference the<animate>
element. -
Use
useEffect
to call.beginElement()
when a prop/state changes. -
Track the previous value using a custom
usePrevious
hook to animate from old ➝ new.
js
CopyEdit
const previous = usePrevious(current);
🧠 Animation Gotcha
-
If
to
and the current value are the same, no animation will occur. -
Always animate from previous ➝ new to avoid this.
🧠 Summary
Technique | Purpose |
---|---|
begin="indefinite" | Disable auto-start, allow manual trigger |
.beginElement() | Manually start SMIL animation |
useRef + useEffect in React | Control animation lifecycle declaratively |
usePrevious() | Track previous prop/state to enable transitions |
from + to attributes | Define animation range explicitly |
🆚 <animate>
vs. <animateTransform>
Feature | <animate> | <animateTransform> |
---|---|---|
Purpose | Animates standard SVG attributes (e.g., fill , opacity , points ) | Specifically animates transform properties (e.g., translate, scale) |
Target attribute | Any animatable attribute (except transform ) | Always attributeName="transform" |
Usage | General-purpose animation tool | Specialized for transforms like translation, scaling, rotation |
Type required? | ❌ No type needed | ✅ Must specify type (e.g., translate , scale , rotate ) |
Example | <animate attributeName="opacity" to="1" dur="0.5s" /> | <animateTransform type="translate" to="10,0" dur="0.5s" /> |
🧠 When to Use Which
-
✅ Use
<animate>
when animating things like:fill
,stroke
,r
,x
,y
,width
,height
,opacity
, etc.
-
✅ Use
<animateTransform>
when animating:translate
,scale
,rotate
,skewX
,skewY