Timeline
A six second keyframe timeline you can play, scrub and step through frame by frame.
What you are seeing
A light scene shows a blue ball, a charcoal square and a blue bar. Under it a six second timeline has three rows of diamond keyframes and a magenta playhead with a round handle. A readout gives the current time out of six seconds.
How it works
The timeline stores keyframes as pairs of time and value for each track. To draw a frame, the script finds the two keyframes around the current time and eases between their values.
The scene above only reads that one time value. A ball slides, a square turns and a bar grows, so scrubbing and playing are the same operation with a different source of time.
The playhead is a slider. You can drag it, click anywhere on the track, press the arrow keys for 0.1 second steps, Shift for one second, and Page Up or Page Down to jump to keyframes.
Source
/lab/timeline/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Timeline</title>
<meta name="description" content="A keyframe timeline with play, pause, scrubbing and keyboard stepping, driving three SVG shapes.">
<link rel="stylesheet" href="../lab.css">
<style>
.ball{fill:#1f3fe0}.box{fill:#24272c}.meter{fill:#1f3fe0;fill-opacity:.8}
.lane{stroke:#c9ccd3;stroke-width:1}.tick{stroke:#c9ccd3;stroke-width:1}
.lbl{font-size:10px;fill:#555b66}.key{fill:#fff;stroke:#24272c;stroke-width:1.5}
.head{stroke:#d4006a;stroke-width:2}.knob{fill:#d4006a;stroke:#fff;stroke-width:2}
#track{fill:#fff;stroke:#c9ccd3}
</style>
</head>
<body>
<main class="demo">
<div class="stage">
<svg id="svg" class="drag-surface" viewBox="0 0 400 300" role="group" aria-labelledby="t d">
<title id="t">Timeline</title>
<desc id="d">The upper part shows a blue ball sliding sideways, a charcoal square turning and a blue bar growing. The lower part is a six second timeline with three tracks of diamond keyframes. A magenta playhead marks the current time and can be dragged or moved with the arrow keys.</desc>
<line x1="20" y1="64" x2="380" y2="64" class="lane"/>
<circle id="ball" class="ball" r="14" cy="64" cx="30"/>
<rect id="box" class="box" x="-18" y="-18" width="36" height="36" rx="4"/>
<rect id="meter" class="meter" x="30" y="136" height="10" rx="5" width="10"/>
<rect id="track" x="8" y="172" width="384" height="120" rx="8"/>
<g id="ticks"></g>
<g id="rows"></g>
<g id="playhead">
<line class="head" x1="0" x2="0" y1="176" y2="290"/>
<circle id="knob" class="knob grab" cx="0" cy="182" r="9" tabindex="0" role="slider" aria-label="Playhead time" aria-valuemin="0" aria-valuemax="6" aria-valuenow="0"/>
</g>
</svg>
</div>
<div class="bar">
<button id="play" type="button">Pause</button>
<button id="back" type="button" aria-label="Step back 0.1 seconds">◀ Step</button>
<button id="fwd" type="button" aria-label="Step forward 0.1 seconds">Step ▶</button>
<label><input id="loop" type="checkbox" checked> <span>Loop</span></label>
<output id="time" aria-live="off">0.00 / 6.00 s</output>
</div>
<p class="sr-only" id="live" aria-live="polite"></p>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/timeline/demo.js
(function () {
'use strict';
var $ = Lab.$, svg = $('#svg'), DUR = 6, X0 = 78, X1 = 380;
var tracks = [
{ name: 'Ball x', y: 208, keys: [[0, 0], [1.5, 1], [3, 0.25], [4.5, 1], [6, 0]] },
{ name: 'Turn', y: 238, keys: [[0, 0], [2, 90], [4, 90], [6, 360]] },
{ name: 'Bar', y: 268, keys: [[0, 10], [3, 330], [6, 10]] }
];
function value(tr, t) {
var k = tr.keys;
for (var i = 0; i < k.length - 1; i++) if (t <= k[i + 1][0]) {
var u = (t - k[i][0]) / (k[i + 1][0] - k[i][0]);
return Lab.lerp(k[i][1], k[i + 1][1], Lab.ease(Lab.clamp(u, 0, 1)));
}
return k[k.length - 1][1];
}
var tx = function (t) { return X0 + (t / DUR) * (X1 - X0); };
var ticks = $('#ticks'), rows = $('#rows');
for (var s = 0; s <= DUR; s++) {
Lab.make('line', { class: 'tick', x1: tx(s), x2: tx(s), y1: 190, y2: 284 }, ticks);
Lab.make('text', { class: 'lbl', x: tx(s), y: 187, 'text-anchor': 'middle' }, ticks).textContent = s + 's';
}
tracks.forEach(function (tr) {
Lab.make('text', { class: 'lbl', x: 16, y: tr.y + 4 }, rows).textContent = tr.name;
Lab.make('line', { class: 'lane', x1: X0, x2: X1, y1: tr.y, y2: tr.y }, rows);
tr.keys.forEach(function (k) { Lab.make('rect', { class: 'key', x: -5, y: -5, width: 10, height: 10, transform: 'translate(' + tx(k[0]) + ' ' + tr.y + ') rotate(45)' }, rows); });
});
var t = 0, playing = false, ball = $('#ball'), box = $('#box'), meter = $('#meter'), play = $('#play'), knob = $('#knob');
function render() {
ball.setAttribute('cx', (30 + value(tracks[0], t) * 340).toFixed(1));
box.setAttribute('transform', 'translate(200 104) rotate(' + value(tracks[1], t).toFixed(1) + ')');
meter.setAttribute('width', value(tracks[2], t).toFixed(1));
$('#playhead').setAttribute('transform', 'translate(' + tx(t).toFixed(1) + ' 0)');
$('#time').textContent = t.toFixed(2) + ' / ' + DUR.toFixed(2) + ' s';
knob.setAttribute('aria-valuenow', t.toFixed(2)); knob.setAttribute('aria-valuetext', t.toFixed(1) + ' seconds');
}
function seek(v) { t = Lab.clamp(v, 0, DUR); render(); }
function setPlay(on) { playing = on; play.textContent = on ? 'Pause' : 'Play'; if (on) { if (t >= DUR) t = 0; loop.start(); } else loop.stop(); }
var loop = Lab.loop(function (dt) {
t += dt;
if (t >= DUR) { if ($('#loop').checked) t -= DUR; else { t = DUR; setPlay(false); } }
render();
});
play.addEventListener('click', function () { setPlay(!playing); });
$('#back').addEventListener('click', function () { setPlay(false); seek(Math.round((t - 0.1) * 10) / 10); });
$('#fwd').addEventListener('click', function () { setPlay(false); seek(Math.round((t + 0.1) * 10) / 10); });
function fromPoint(p) { return (p.x - X0) / (X1 - X0) * DUR; }
Lab.drag(knob, { space: svg, down: function () { setPlay(false); }, move: function (p) { seek(fromPoint(p)); } });
var track = $('#track'); track.classList.add('drag-surface');
Lab.drag(track, { space: svg, down: function (p) { setPlay(false); seek(fromPoint(p)); }, move: function (p) { seek(fromPoint(p)); } });
knob.addEventListener('keydown', function (e) {
var k = e.key, s = e.shiftKey ? 1 : 0.1;
if (k === 'ArrowRight' || k === 'ArrowUp') seek(Math.round((t + s) * 10) / 10);
else if (k === 'ArrowLeft' || k === 'ArrowDown') seek(Math.round((t - s) * 10) / 10);
else if (k === 'Home') seek(0); else if (k === 'End') seek(DUR);
else if (k === ' ' || k === 'Enter') { setPlay(!playing); }
else if (k === 'PageDown' || k === 'PageUp') {
var all = []; tracks.forEach(function (tr) { tr.keys.forEach(function (q) { if (all.indexOf(q[0]) < 0) all.push(q[0]); }); }); all.sort(function (a, b) { return a - b; });
var next = k === 'PageUp' ? all.filter(function (v) { return v > t + 0.001; })[0] : all.filter(function (v) { return v < t - 0.001; }).pop();
if (next !== undefined) seek(next);
} else return;
if (k !== ' ' && k !== 'Enter') setPlay(false);
e.preventDefault();
});
render();
if (Lab.reduced) { seek(1.5); setPlay(false); } else setPlay(true);
})();
/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}}