Unit 6 Teaching Demo

SVG Animation with CSS

This demo shows two simple ideas: a heart that draws itself and a sun that rotates around the correct center.

Try the Demo

Heart is drawing. Sun is rotating.

Plays once

Heart Drawing Itself

forwards
Animated heart A heart outline draws itself once.

Think of the heart as one long piece of string. CSS moves the string out of view, then slides it back into place.

stroke-dashoffset: var(--path-length);

@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}

Repeats continuously

Sun Rotating Around Its Center

infinite
Animated sun The rays rotate around the center of the sun.

Turn off “Rotate around the sun.” The rays will use the center of the entire SVG instead of the center of the sun.

Green: sun center Red: whole SVG center

transform-box: fill-box;
transform-origin: center;

How the Heart Draws Itself

  1. JavaScript measures the line.
  2. CSS turns the line into one long dash.
  3. The dash is moved out of view.
  4. The dash moves back and reveals the heart.

Why the Sun Needs a Fix

SVG elements do not always rotate around their own center. transform-box: fill-box tells the browser to use the sun rays themselves when finding the center.

Reduced Motion

Some users prefer less movement. When reduced motion is on, the sun stops and the heart stays visible.

@media (prefers-reduced-motion: reduce) {
  .draw-path,
  .sun-rays {
    animation: none;
  }

  .draw-path {
    stroke-dashoffset: 0;
  }
}