SMIL vs CSS vs JS

The same eased back and forth motion built with SMIL, CSS keyframes and requestAnimationFrame, side by side.

What you are seeing

Three horizontal rails each carry a round dot that slides left and right. The top lane is labelled SMIL, the middle CSS and the bottom JavaScript. A small number beside each label shows the dot position. The dots stay in step with each other.

How it works

Three dots travel the same distance in the same time with the same easing curve. The top one uses an animate element with keySplines, the middle one uses CSS keyframes and a cubic-bezier timing function, and the bottom one is moved by script.

SMIL keeps its animation inside the SVG file, so it works in an img element. CSS is the simplest for hover and state changes. Script gives full control, but you must write the timing and easing yourself, which is why the bottom dot uses a small bezier solver.

Pause, restart and speed changes show how each method is controlled: pauseAnimations and setCurrentTime for SMIL, animation-play-state for CSS, and a flag in the loop for script.

Source

/lab/smil-vs-css/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>SMIL vs CSS vs JS</title>
<meta name="description" content="The same eased motion written three ways: SMIL, CSS keyframes and requestAnimationFrame.">
<link rel="stylesheet" href="../lab.css">
<style>
:root{--dur:1.5s}
.rail{stroke:#c9ccd3;stroke-width:3;stroke-linecap:round}
.lab{font-size:12px;font-weight:600;fill:#24272c}.sub{font-size:10px;fill:#555b66}
.dot{fill:#1f3fe0}.dot.css{fill:#24272c;animation:slide var(--dur) cubic-bezier(.65,0,.35,1) infinite alternate}
.dot.js{fill:#1f3fe0;fill-opacity:.55}
.paused .dot.css{animation-play-state:paused}
@keyframes slide{from{transform:translateX(0)}to{transform:translateX(280px)}}
</style>
</head>
<body>
<main class="demo">
  <div class="stage">
    <svg id="svg" viewBox="0 0 400 300" role="img" aria-labelledby="t d">
      <title id="t">SMIL, CSS and JavaScript animation compared</title>
      <desc id="d">Three horizontal rails each carry a dot that slides back and forth with the same easing and timing. The top dot is animated by SMIL, the middle by CSS keyframes and the bottom by JavaScript. A small readout beside each label shows its current position.</desc>
      <g>
        <text class="lab" x="20" y="38">SMIL <tspan class="sub">animate element</tspan></text><text id="r0" class="sub" x="380" y="38" text-anchor="end"></text>
        <line class="rail" x1="70" x2="350" y1="70" y2="70"/>
        <circle class="dot" id="smil" cx="70" cy="70" r="14">
          <animate id="smilAnim" attributeName="cx" values="70;350;70" keyTimes="0;0.5;1" calcMode="spline" keySplines="0.65 0 0.35 1;0.65 0 0.35 1" dur="3s" repeatCount="indefinite"/>
        </circle>
      </g>
      <g>
        <text class="lab" x="20" y="130">CSS <tspan class="sub">@keyframes</tspan></text><text id="r1" class="sub" x="380" y="130" text-anchor="end"></text>
        <line class="rail" x1="70" x2="350" y1="162" y2="162"/>
        <circle class="dot css" id="css" cx="70" cy="162" r="14"/>
      </g>
      <g>
        <text class="lab" x="20" y="222">JavaScript <tspan class="sub">requestAnimationFrame</tspan></text><text id="r2" class="sub" x="380" y="222" text-anchor="end"></text>
        <line class="rail" x1="70" x2="350" y1="254" y2="254"/>
        <circle class="dot js" id="js" cx="70" cy="254" r="14"/>
      </g>
    </svg>
  </div>
  <div class="bar">
    <button id="play" type="button">Pause</button>
    <button id="restart" type="button">Restart all</button>
    <label><span>Speed</span><select id="speed"><option value="0.5">0.5x</option><option value="1" selected>1x</option><option value="2">2x</option></select></label>
  </div>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/smil-vs-css/demo.js
(function () {
  'use strict';
  var $ = Lab.$, svg = $('#svg'), css = $('#css'), js = $('#js'), smil = $('#smil'), anim = $('#smilAnim'), play = $('#play'), speed = $('#speed');
  // Cubic bezier easing, same curve as the CSS and SMIL versions: cubic-bezier(.65, 0, .35, 1)
  function bezier(x1, y1, x2, y2) {
    function c(a, b, t) { var m = 1 - t; return 3 * m * m * t * a + 3 * m * t * t * b + t * t * t; }
    return function (x) {
      var lo = 0, hi = 1, t = x;
      for (var i = 0; i < 24; i++) { t = (lo + hi) / 2; var cx = c(x1, x2, t); if (cx < x) lo = t; else hi = t; }
      return c(y1, y2, t);
    };
  }
  var ease = bezier(0.65, 0, 0.35, 1), t = 0, playing = false;
  function leg() { return 1.5 / +speed.value; }
  function jsPos() {
    var p = (t % (2 * leg())) / leg(); if (p > 1) p = 2 - p;
    return 70 + 280 * ease(p);
  }
  function readouts() {
    var a = smil.cx.animVal.value;
    var m = getComputedStyle(css).transform, b = 70;
    if (m && m !== 'none') { var mm = m.match(/matrix\(([^)]+)\)/); if (mm) b = 70 + parseFloat(mm[1].split(',')[4]); }
    js.setAttribute('cx', jsPos().toFixed(1));
    $('#r0').textContent = 'x ' + a.toFixed(0); $('#r1').textContent = 'x ' + b.toFixed(0); $('#r2').textContent = 'x ' + jsPos().toFixed(0);
  }
  var loop = Lab.loop(function (dt) { t += dt; readouts(); });
  function setSpeed() {
    anim.setAttribute('dur', (3 / +speed.value) + 's');
    document.documentElement.style.setProperty('--dur', (1.5 / +speed.value) + 's');
    restart();
  }
  function restart() {
    t = 0; svg.setCurrentTime(0);
    css.style.animation = 'none'; void css.getBoundingClientRect(); css.style.animation = '';
    if (!playing) css.style.animationPlayState = 'paused';
    readouts();
  }
  function setPlay(on) {
    playing = on; play.textContent = on ? 'Pause' : 'Play';
    document.body.classList.toggle('paused', !on);
    css.style.animationPlayState = on ? 'running' : 'paused';
    if (on) { svg.unpauseAnimations(); loop.start(); } else { svg.pauseAnimations(); loop.stop(); readouts(); }
  }
  document.addEventListener('visibilitychange', function () { if (playing) { if (document.hidden) svg.pauseAnimations(); else svg.unpauseAnimations(); } });
  play.addEventListener('click', function () { setPlay(!playing); });
  $('#restart').addEventListener('click', restart);
  speed.addEventListener('change', setSpeed);
  if (Lab.reduced) { setPlay(false); restart(); } else setPlay(true);
  readouts();
})();
/lab/lab.js
/* Small shared helpers for the SVG Wow! lab demos. No dependencies. */
(function () {
  'use strict';
  var NS = 'http://www.w3.org/2000/svg';
  var mq = window.matchMedia('(prefers-reduced-motion: reduce)');
  var Lab = (window.Lab = { NS: NS });
  Object.defineProperty(Lab, 'reduced', { get: function () { return mq.matches; } });
  Lab.$ = function (s, r) { return (r || document).querySelector(s); };
  Lab.$$ = function (s, r) { return Array.prototype.slice.call((r || document).querySelectorAll(s)); };
  Lab.clamp = function (v, a, b) { return Math.max(a, Math.min(b, v)); };
  Lab.lerp = function (a, b, t) { return a + (b - a) * t; };
  Lab.ease = function (t) { return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; };
  Lab.make = function (tag, attrs, parent) {
    var e = document.createElementNS(NS, tag);
    if (attrs) for (var k in attrs) e.setAttribute(k, attrs[k]);
    if (parent) parent.appendChild(e);
    return e;
  };
  /* Client point to the user space of `el` (any SVG graphics element). */
  Lab.pt = function (el, evt) {
    var m = el.getScreenCTM();
    if (!m) return { x: 0, y: 0 };
    var p = new DOMPoint(evt.clientX, evt.clientY).matrixTransform(m.inverse());
    return { x: p.x, y: p.y };
  };
  /* requestAnimationFrame loop that pauses while the document is hidden. */
  Lab.loop = function (fn) {
    var raf = 0, last = 0, running = false, wanted = false, self = {};
    function tick(t) {
      if (!running) return;
      raf = requestAnimationFrame(tick);
      var dt = last ? Math.min((t - last) / 1000, 0.1) : 0;
      last = t;
      fn(dt, t / 1000);
    }
    function go() { if (running || document.hidden || !wanted) return; running = true; last = 0; raf = requestAnimationFrame(tick); }
    function halt() { running = false; cancelAnimationFrame(raf); }
    self.start = function () { wanted = true; go(); };
    self.stop = function () { wanted = false; halt(); };
    self.running = function () { return wanted; };
    document.addEventListener('visibilitychange', function () { if (document.hidden) halt(); else go(); });
    return self;
  };
  /* Pointer drag with capture. space: element whose user space the points use. */
  Lab.drag = function (el, o) {
    var active = false, space = o.space || el.ownerSVGElement || el;
    el.addEventListener('pointerdown', function (e) {
      if (e.button > 0) return;
      active = true;
      try { el.setPointerCapture(e.pointerId); } catch (_) {}
      if (o.down) o.down(Lab.pt(space, e), e);
      e.preventDefault();
    });
    el.addEventListener('pointermove', function (e) { if (active) o.move(Lab.pt(space, e), e); });
    function end(e) {
      if (!active) return;
      active = false;
      try { el.releasePointerCapture(e.pointerId); } catch (_) {}
      if (o.up) o.up(e);
    }
    el.addEventListener('pointerup', end);
    el.addEventListener('pointercancel', end);
  };
  Lab.say = function (msg) {
    var l = document.getElementById('live');
    if (l) l.textContent = msg;
  };
})();
/lab/lab.css
:root{--bg:#f1f2f4;--ink:#24272c;--blue:#1f3fe0;--mag:#d4006a;--line:#c9ccd3;--panel:#fff;--muted:#555b66;font-family:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:var(--bg);color:var(--ink);overflow-x:hidden}
body{font-size:13px;line-height:1.3}
.demo{display:flex;flex-direction:column;height:100%;min-height:0}
.stage{flex:1 1 auto;min-height:120px;position:relative;overflow:hidden;background:var(--bg)}
.stage>svg{position:absolute;left:0;top:0;width:100%;height:100%;display:block}
.bar{flex:0 0 auto;display:flex;flex-wrap:wrap;gap:6px 14px;align-items:center;padding:6px 10px;background:var(--panel);border-top:1px solid var(--line);max-height:46%;overflow:auto}
.bar .grp{display:flex;flex-wrap:wrap;align-items:center;gap:6px 10px}
label{display:inline-flex;align-items:center;gap:6px;white-space:nowrap}
label>span,.dim{color:var(--muted)}
output{font-variant-numeric:tabular-nums;min-width:3.2ch;display:inline-block}
button,select,input[type=number],input[type=text]{font:inherit;color:var(--ink);background:#fff;border:1px solid #8a909b;border-radius:6px;padding:3px 8px;min-height:28px}
button{border-color:var(--ink);cursor:pointer}
button:hover{background:#e7e9ef}
button[aria-pressed=true]{background:var(--ink);color:#fff}
input[type=range]{width:110px;accent-color:var(--blue);margin:0}
input[type=color]{width:34px;height:28px;padding:1px;border:1px solid #8a909b;border-radius:6px;background:#fff}
input[type=checkbox]{accent-color:var(--blue);width:16px;height:16px}
:focus-visible{outline:3px solid var(--blue);outline-offset:2px}
.grab{cursor:grab;touch-action:none}
.grab:active{cursor:grabbing}
.grab:focus-visible{outline:none;stroke:var(--blue);stroke-width:4px}
.drag-surface{touch-action:none}
.ro{font-size:11px;color:var(--muted)}
.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
svg text{font-family:inherit}
pre,textarea{font:11px/1.35 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
@media (pointer:coarse){button,select,input[type=number]{min-height:38px}input[type=range]{height:30px}}
@media (max-width:420px){.bar{gap:6px 10px;padding:6px 8px}input[type=range]{width:96px}}
@media (min-width:700px){.demo.split{flex-direction:row}.demo.split .stage{flex:1 1 auto}.demo.split .bar{width:320px;max-height:none;flex:0 0 320px;border-top:0;border-left:1px solid var(--line);align-content:flex-start}}