Raka's logo is going to appear as if drawn by hand: the t-shirt line draws itself, the coffee bean on its chest pops in, then the word RAKA fades in. All of it in SVG (Scalable Vector Graphics): a picture written as elements inside HTML, like "a line from here to there" or "a circle at this point".
Picture the cut file of a sticker-cutting machine: it only holds the knife's route, so it can be cut as small as a label or as big as a banner without breaking up. SVG is the same. We build the logo first, then animate it.
SVG, viewBox and path
<svg viewBox="0 0 120 140" width="240" height="280" role="img" aria-label="Raka's logo: a t-shirt with a coffee bean">
<rect width="120" height="140" rx="12" fill="#1c1917" />
<path class="shirt" d="M45 18 L25 24 L8 42 L20 54 L30 46 L30 102 L90 102 L90 46 L100 54 L112 42 L95 24 L75 18 L60 28 Z"
fill="none" stroke="#f5e6c8" stroke-width="4" stroke-linejoin="round" />
<g class="bean">
<ellipse cx="76" cy="62" rx="7" ry="10" fill="#c47a3d" />
<path d="M76 54 Q72 62 76 70" fill="none" stroke="#1c1917" stroke-width="2" />
</g>
<text class="label" x="60" y="128" text-anchor="middle" font-size="18" font-weight="bold" fill="#f5e6c8">RAKA</text>
</svg>
viewBox="0 0 120 140"is the coordinate system inside the picture: x 0 to 120, y 0 to 140, (0, 0) at the top left like a canvas.width="240"shows it twice as big: 1 unit = 2 px, and it stays sharp at any size because the browser redraws the lines instead of stretching pixels.- The
dattribute of a<path>is a list of commands:M x ymoves the pen (moveTo),L x ydraws a line to that point (lineTo),H xandV ydraw flat and upright lines,QandCdraw curves,Zcloses back to the start (closePath). Capital letters use absolute coordinates, lowercase ones are relative to the pen. - Every shape is an element in the DOM: it can get a
class, be styled with CSS, and get its ownaddEventListener.
Animating with CSS, and the pivot-point trap
The coffee bean appears by growing from 0 while turning:
.bean {
transform-box: fill-box; /* reference box = the bean's own box */
transform-origin: center;
animation: appear 0.5s 2s ease-out both;
}
@keyframes appear {
from { transform: scale(0) rotate(-90deg); }
}
On SVG elements, transform-origin defaults to 0 0, the viewBox's top-left corner, and transform-box defaults to view-box. So transform-origin: center alone means the middle of the whole logo (60, 70), not the middle of the bean. Measured with the bean halfway, scale(0.5) rotate(-45deg) (Chrome, Firefox and Safari agree):
CSS on .bean |
Pivot point | Bean's centre when halfway |
|---|---|---|
no transform-origin |
(0, 0), the top-left corner | around (49, -5): outside the logo |
transform-origin: center |
(60, 70), the logo's middle | around (63, 62): drifting in from the middle |
transform-box: fill-box + center |
(76, 62), the bean's middle | (76, 62): stays in place |
both in animation holds the from state (size 0) during the 2 second delay, so the bean doesn't show before its time.
A line that draws itself
stroke-dasharray turns a line into dashes. One number means a dash that long, then a gap that long too. stroke-dashoffset slides that pattern along the line. The trick: make the dash and the gap as long as the line. An offset as long as the line means the gap covers it (invisible); an offset of 0 means the dash does (whole).
Measure the length with JavaScript:
const shirt = document.querySelector('.shirt');
console.log(shirt.getTotalLength()); // 358.8880615234375
Chrome, Firefox and Safari give the same number. Round it up (Math.ceil) to 359:
.shirt {
stroke-dasharray: 359;
stroke-dashoffset: 0; /* base state: the whole line */
animation: draw 2s ease-out both;
}
@keyframes draw {
from { stroke-dashoffset: 359; } /* start: hidden */
}
.label { animation: fade 0.6s 2.3s both; }
@keyframes fade { from { opacity: 0; } }
The offset partway = length × (1 - percent / 100):
| Progress | stroke-dashoffset |
What shows |
|---|---|---|
| 0% | 359 | nothing yet |
| 25% | 269.25 | the first 89.75 units |
| 50% | 179.5 | half the line |
| 100% | 0 | the whole line |
What if you don't measure the length? Guess first: stroke-dasharray gets a guessed 300, and stroke-dashoffset is animated from 300 to 0. At the end, is the line whole, or is there a part that never gets drawn?
With the guess of 300, the dash is 300 and the gap is 300. At the end, units 0 to 300 are under the dash, but 300 to 358.9 fall into the gap: the last 58.9 units (the V-neck and the right shoulder) never get drawn. Stranger still, at the start of the animation that same piece is already showing. Alternatively, pathLength="100" on the <path> makes the line count as 100 long, so stroke-dasharray: 100 always fits.
SMIL: animation inside the SVG
SVG has animation elements of its own, placed inside the element they animate:
<path class="shirt" d="..." stroke-dasharray="359" stroke-dashoffset="359">
<animate attributeName="stroke-dashoffset" from="359" to="0" dur="2s" fill="freeze" />
</path>
attributeNameis the attribute that changes,from/toits values,durits duration, andrepeatCount="indefinite"makes it repeat forever.fill="freeze"here means hold the end value (likeforwardsin CSS), not the fill colour.- SMIL works in current Chrome, Firefox and Safari. Chrome planned to remove it in 2015, then called that off.
CSS is still usually preferred: it's the same knowledge as other CSS animation, @media (prefers-reduced-motion: reduce) can switch it off, and JavaScript can control it through element.getAnimations() (pause, play, jump to a given second). CSS media queries don't touch SMIL; to stop it you need svg.pauseAnimations().
@media (prefers-reduced-motion: reduce) {
.shirt, .bean, .label { animation: none; } /* straight to the end state */
}
This works because the base state is the end state (offset 0, normal size, opacity 1); the start state only lives in from.
Other options for animating SVG:
| Way | When to pick it |
|---|---|
| CSS (used in this lesson) | animations that are fixed in advance, like Raka's logo that appears once. Easy to switch off with prefers-reduced-motion. Pick this if unsure |
Web Animations API: shirt.animate([{ strokeDashoffset: 359 }, { strokeDashoffset: 0 }], 2000) |
the numbers are known only when the code runs (for example from getTotalLength()), or the animation starts on a click. CSS media queries don't stop it, so check matchMedia('(prefers-reduced-motion: reduce)').matches yourself |
| GSAP (a library, free for all users) | many animations that must run in order (a timeline), animations that follow scrolling, or one shape that turns into another (the MorphSVG plugin) |
| Lottie | complex animations a designer made in After Effects, exported as a JSON file and played with the lottie-web library |
SVG or canvas?
| SVG | Canvas | |
|---|---|---|
| What it holds | elements in the DOM | pixels |
| Enlarged | stays sharp | has to be redrawn with the DPR |
| Clicking one shape | addEventListener on its element |
your own hit testing |
| Many things | tens to hundreds | thousands of particles every frame |
| Good for | logos, icons, illustrations, charts | particles, games, per-pixel effects |
Raka's logo suits SVG; steam with dozens of particles a second suits a canvas.
⚠️ What throws the logo animation off
transform-origin: centerwithouttransform-box: fill-box. In SVG, center means the middle of the viewBox, so the bean drifts in from the middle of the logo.- A
stroke-dasharrayshorter than the line. The end of the line never gets drawn. Measure withgetTotalLength()and round up. - Putting the start state in the base style. If
.shirthasstroke-dashoffset: 359as its base, switching animations off for reduced motion makes the logo disappear. Put the start state infrom, and useboth. - Working out the progress backwards. An offset of 0 means finished, not not-started: offset = length × (1 - percent / 100).
TL;DR: SVG is a vector picture as DOM elements: viewBox sets its coordinates, a path's d holds the commands M, L, H, V, Q, C, Z. Animate it with CSS: for scale or rotate on an SVG element, set transform-box: fill-box plus transform-origin: center, because the default is the viewBox's corner. The line-drawing effect: stroke-dasharray and stroke-dashoffset as long as the line (measured with getTotalLength(), Raka's logo is 358.9, so 359), then animate the offset from the length to 0; partway, offset = length × (1 - percent / 100). SMIL (<animate>, repeatCount, fill="freeze") works in every current browser, but CSS is easier to control and to switch off for prefers-reduced-motion. SVG for logos and icons, canvas for thousands of particles.
⚡ A picture made of elements
Write:
<path class="shirt" d="M45 18 L25 24 L8 42 L20 54 L30 46 L30 102 L90 102 L90 46 L100 54 L112 42 L95 24 L75 18 L60 28 Z"
fill="none" stroke="#f5e6c8" stroke-width="4" stroke-linejoin="round" />
Meaning: M moves the pen, L draws a line, Z closes back to the start. Every SVG shape is an element in the DOM, so it can get a class, be styled with CSS, and get its own addEventListener. That's what a canvas doesn't have.
⚡ The bean's pivot point
Write:
.bean {
transform-box: fill-box;
transform-origin: center;
}
Meaning: In SVG, transform-origin: center alone means the middle of the whole logo. With transform-box: fill-box, center becomes the middle of the bean itself, so the bean grows in place.
⚡ A line that draws itself
Write:
.shirt {
stroke-dasharray: 359;
stroke-dashoffset: 0;
animation: draw 2s ease-out both;
}
@keyframes draw {
from { stroke-dashoffset: 359; }
}
Guess first: with a guessed dasharray of 300, is the 358.9 line whole at the end?
No. The last 58.9 units fall into the gap and never get drawn. Measure first with getTotalLength(), then round up.
Meaning: The dash and the gap are as long as the line. An offset of 359 means hidden, 0 means whole, and partway the offset = length × (1 - percent / 100).