<!--
Motion Blur — After Effects-style shutter-based motion blur.
Usage: paste this snippet into your composition, then call
attachMotionBlur() with any element animated by your GSAP timeline.
How it works — the same model After Effects uses for the layer Motion Blur
switch (temporal supersampling), synthesised inside the page because the
renderer captures one instant per frame with no shutter:
1. After every timeline update (each rendered frame), the snippet seeks
the timeline to `samplesPerFrame` sub-frame times spread evenly across
the shutter window, reads each target's GSAP x/y at every sample, then
restores the frame time. Seeks run with events suppressed, so no user
callbacks fire and the DOM ends up exactly where the frame left it.
2. An SVG filter stacks one copy of the element per sample (feOffset to
the sampled position relative to the current one) and adds them at
equal weight (feComposite arithmetic, 1/N each, sRGB space). Result:
a box smear along the actual trajectory whose length is speed ×
shutter time, with no decaying one-sided trail. The tails — where
fewer copies overlap — step down evenly at 1/N per copy; wherever the
element travels less than its own size the copies pile up past 1 and
clamp, so the middle of the smear is solid rather than a plateau.
3. The element itself is then composited over that smear, sharp and at
full opacity. No single duplicate ever exceeds 1/N, so a moving edge
resolves as a staircase of ghosts behind a solid frame-time instance.
4. When the sampled positions collapse to the current one (element at
rest), the filter is removed so the element renders sharp.
Shutter window (AE semantics, per frame at time t, frame interval 1/fps):
shutterTime = shutterAngle / 360 / fps
windowStart = t + shutterPhase / 360 / fps
copy k = windowStart + k / N × shutterTime, k = 0..N (N+1 copies)
N counts sub-intervals, so both ends of the window carry a copy.
Defaults (shutterAngle 720, shutterPhase -360) integrate [t - 1/fps, t + 1/fps]:
a solid moving at 2000 px/s at 30 fps smears 133 px, centred on its position.
These defaults are measured, not assumed. On a 1920×1080 / 30 fps After Effects
export of translating text, the outermost duplicate on the trailing side sits
exactly at the previous frame's position and the outermost leading duplicate
exactly at the next frame's, with 8 evenly spaced duplicates in between on each
side (window = 2 frames = 720°, phase -360°, 16 sub-intervals). Reading the ghost
staircase across a stroke gives steps of 0.063 ± 0.002 of the sharp text
intensity — a flat 1/16 per duplicate, with no taper toward the window edges
(triangle weighting scores 1.6 dB worse against the same export). The sharp
instance on top is what keeps total ink above the unblurred frame's.
Requirements:
- Elements must be animated via GSAP x/y (transform), not left/top. The
element's own scale/rotation is compensated; skew, 3D rotation and
transformed ancestors are not.
- The host must render the timeline for the blur to update — HyperFrames
seeks every frame. A fresh paused timeline that is never seeked shows no
blur on its first frame.
- Call attachMotionBlur() AFTER defining all tweens (the timeline's final
duration must be known), before window.__timelines registration.
- GSAP must be loaded before this snippet executes.
API:
attachMotionBlur(selector, timeline, options?)
Options:
shutterAngle — degrees of the frame interval the shutter is open
(default 720 = two frames, as measured off the AE
reference; 360 = one frame, 0 disables)
shutterPhase — degrees offset of the window start from the frame
time (default -360 = centred on the frame, one frame
back, like the AE reference)
samplesPerFrame — sub-intervals of the shutter window, so N+1 duplicates
each at 1/N opacity (default 16, max 64)
fps — composition frame rate (default: the root's data-fps,
else 30). Pass it explicitly when rendering with an
fps override (`hyperframes render --fps`).
axis — "x" | "y" | "both" (default "both")
-->
<script>
(function () {
if (!window._hfMbUid) window._hfMbUid = 0;
// Shutter length is measured in frames, so the snippet must know the
// composition frame rate: explicit option, else the root's data-fps, else 30.
function resolveFps(optionFps) {
var explicit = Number(optionFps);
if (explicit > 0) return explicit;
var root = document.querySelector("[data-composition-id][data-fps]");
var rootFps = root ? Number(root.getAttribute("data-fps")) : 0;
if (rootFps > 0) return rootFps;
return 30;
}
function numOption(value, fallback) {
return value !== undefined ? Number(value) : fallback;
}
// objectBoundingBox percentage for a px offset plus padding.
function regionPercent(offset, size, padPercent) {
return ((offset / size) * 100 + padPercent).toFixed(2) + "%";
}
window.attachMotionBlur = function (selector, tl, opts) {
opts = opts || {};
var shutterAngle = numOption(opts.shutterAngle, 720);
var shutterPhase = numOption(opts.shutterPhase, -360);
var requestedSamples = numOption(opts.samplesPerFrame, 16);
var samples = Number.isFinite(requestedSamples)
? Math.max(2, Math.min(64, Math.round(requestedSamples)))
: 16;
var axis = opts.axis || "both";
var fps = resolveFps(opts.fps);
var useX = axis !== "y";
var useY = axis !== "x";
// Untransformed box size; SVG elements have no offsetWidth, so use the
// same bbox GSAP uses to resolve their percent transforms.
function boxSize(el, horizontal) {
var box = typeof el.offsetWidth === "number" ? el : el.getBBox ? el.getBBox() : null;
var size = box
? horizontal
? box.offsetWidth || box.width
: box.offsetHeight || box.height
: 0;
return size || el.getBoundingClientRect()[horizontal ? "width" : "height"] || 1;
}
// GSAP x/y may carry a % unit and xPercent/yPercent is a separate term;
// resolve both against the element's own box so samples are in px.
function readAxis(el, prop, percentProp, size) {
var px = parseFloat(gsap.getProperty(el, prop, "px")) || 0;
var percent = parseFloat(gsap.getProperty(el, percentProp)) || 0;
return px + (percent * size) / 100;
}
function readPosition(el) {
return {
x: useX ? readAxis(el, "x", "xPercent", boxSize(el, true)) : 0,
y: useY ? readAxis(el, "y", "yPercent", boxSize(el, false)) : 0,
};
}
// CSS filters run in the element's pre-transform space while GSAP x/y are
// post-transform, so a displacement is mapped through the inverse of the
// element's own scale/rotation. Ancestor transforms are not compensated.
function toFilterSpace(el, dx, dy) {
var sx = parseFloat(gsap.getProperty(el, "scaleX")) || 1;
var sy = parseFloat(gsap.getProperty(el, "scaleY")) || 1;
var theta = ((parseFloat(gsap.getProperty(el, "rotation")) || 0) * Math.PI) / 180;
var cos = Math.cos(theta);
var sin = Math.sin(theta);
return { dx: (dx * cos + dy * sin) / sx, dy: (-dx * sin + dy * cos) / sy };
}
var items = Array.isArray(selector) ? selector : [selector];
var targets = items.reduce(function (acc, s) {
if (typeof s === "string") {
document.querySelectorAll(s).forEach(function (el) {
acc.push(el);
});
} else if (s instanceof Element) {
acc.push(s);
}
return acc;
}, []);
var ns = "http://www.w3.org/2000/svg";
// `samples` counts sub-intervals of the shutter window, so there is one copy at
// every interval boundary — both ends of the window included — and each carries
// 1/samples of the source. That is what the reference export shows: the outermost
// copy sits exactly one frame away from the frame time, not half a step short.
var copies = samples + 1;
var weight = String(1 / samples);
// One SVG filter per target: N+1 offset copies of SourceGraphic added at 1/N
// each, then the element itself over the top. Accumulating in sRGB matches
// AE's default (non-linear) 8-bpc working space.
var state = targets.map(function (el) {
var uid = "hf-mb-" + window._hfMbUid++;
var svg = document.createElementNS(ns, "svg");
svg.setAttribute("style", "position:absolute;width:0;height:0;overflow:hidden;");
var filter = document.createElementNS(ns, "filter");
filter.id = uid;
filter.setAttribute("color-interpolation-filters", "sRGB");
var offsets = [];
for (var k = 0; k < copies; k++) {
var feOff = document.createElementNS(ns, "feOffset");
feOff.setAttribute("in", "SourceGraphic");
feOff.setAttribute("dx", "0");
feOff.setAttribute("dy", "0");
feOff.setAttribute("result", "s" + k);
filter.appendChild(feOff);
offsets.push(feOff);
}
// Pairwise accumulation: the first composite scales both inputs by 1/N, every
// later one adds the next copy at 1/N to the running sum. The N+1 weights total
// (N+1)/N, so an element wider than its own smear saturates through the middle
// and the filter clamps there. That is the saturation the reference shows, and
// the sharp instance covers the same region regardless.
var accumulated = "s0";
for (var c = 1; c < copies; c++) {
var feComp = document.createElementNS(ns, "feComposite");
feComp.setAttribute("operator", "arithmetic");
feComp.setAttribute("in", accumulated);
feComp.setAttribute("in2", "s" + c);
feComp.setAttribute("k1", "0");
feComp.setAttribute("k2", c === 1 ? weight : "1");
feComp.setAttribute("k3", weight);
feComp.setAttribute("k4", "0");
accumulated = "a" + c;
feComp.setAttribute("result", accumulated);
filter.appendChild(feComp);
}
// The frame-time instance is drawn sharp, at full opacity, over the smear.
// Every blurred copy stays at 1/N, so nothing but the instance the frame is
// actually at reads as solid. Unnamed, so it is the filter output.
var feOver = document.createElementNS(ns, "feComposite");
feOver.setAttribute("operator", "over");
feOver.setAttribute("in", "SourceGraphic");
feOver.setAttribute("in2", accumulated);
filter.appendChild(feOver);
svg.appendChild(filter);
document.body.appendChild(svg);
return { el: el, offsets: offsets, filter: filter, filterId: uid, xs: [], ys: [] };
});
// The element may carry its own inline filter (authored or tweened by
// GSAP); the blur is appended to that chain and removed from it, never
// written over it.
var OWN_FILTER = /\s*url\(["']?#hf-mb-\d+["']?\)/g;
function baseFilter(el) {
return el.style.filter.replace(OWN_FILTER, "").trim();
}
function clearBlur(s) {
s.el.style.filter = baseFilter(s.el);
}
var scheduled = false;
function applyShutter() {
var shutterTime = shutterAngle / 360 / fps;
if (!(shutterTime > 0)) {
state.forEach(clearBlur);
return;
}
var t0 = tl.time();
var duration = tl.duration();
var windowStart = t0 + shutterPhase / 360 / fps;
// Sample the real trajectory: seek to each sub-frame time with events
// suppressed (so no tween callbacks, including this tracker's, fire),
// read positions, then restore the frame time.
try {
for (var k = 0; k < copies; k++) {
var tk = windowStart + (k / samples) * shutterTime;
tl.time(Math.min(duration, Math.max(0, tk)), true);
state.forEach(function (s) {
var pos = readPosition(s.el);
s.xs[k] = pos.x;
s.ys[k] = pos.y;
});
}
} finally {
tl.time(t0, true);
}
state.forEach(function (s) {
var current = readPosition(s.el);
var minDx = Infinity;
var maxDx = -Infinity;
var minDy = Infinity;
var maxDy = -Infinity;
for (var i = 0; i < copies; i++) {
var d = toFilterSpace(s.el, s.xs[i] - current.x, s.ys[i] - current.y);
var dx = d.dx;
var dy = d.dy;
s.offsets[i].setAttribute("dx", dx.toFixed(3));
s.offsets[i].setAttribute("dy", dy.toFixed(3));
if (dx < minDx) minDx = dx;
if (dx > maxDx) maxDx = dx;
if (dy < minDy) minDy = dy;
if (dy > maxDy) maxDy = dy;
}
// Below half a pixel of smear the average is indistinguishable from
// the source — render sharp instead of paying for the filter.
if (maxDx - minDx < 0.5 && maxDy - minDy < 0.5) {
clearBlur(s);
return;
}
// Grow the filter region to cover every copy AND the sharp instance the
// final composite draws at offset 0 (objectBoundingBox units, 10% padding
// so anti-aliased edges are not clipped). Clamping the bounds through 0
// matters whenever the window sits entirely on one side of the frame time
// — any shutterPhase above 0 or below -shutterAngle — because then no copy
// is at 0 and a region derived from the copies alone cuts the instance off.
var x0 = Math.min(0, minDx);
var x1 = Math.max(0, maxDx);
var y0 = Math.min(0, minDy);
var y1 = Math.max(0, maxDy);
var w = boxSize(s.el, true);
var h = boxSize(s.el, false);
s.filter.setAttribute("x", regionPercent(x0, w, -10));
s.filter.setAttribute("width", regionPercent(x1 - x0, w, 120));
s.filter.setAttribute("y", regionPercent(y0, h, -10));
s.filter.setAttribute("height", regionPercent(y1 - y0, h, 120));
var base = baseFilter(s.el);
s.el.style.filter = (base ? base + " " : "") + "url(#" + s.filterId + ")";
});
}
// tl.eventCallback("onUpdate") is unavailable under the HyperFrames runtime proxy, so a
// tracker tween's onUpdate fires on every seek. Sampling is deferred to a microtask:
// seeking GSAP from inside its own render reads stale tween state, while the microtask
// runs after the seek returns and before the frame is captured or painted.
var _proxy = { t: 0 };
tl.to(
_proxy,
{
t: 1,
duration: Math.max(tl.duration(), 0.1),
ease: "none",
onUpdate: function () {
if (scheduled) return;
scheduled = true;
Promise.resolve().then(function () {
scheduled = false;
applyShutter();
});
},
},
0,
);
};
})();
</script>
<!--
Timeline integration example:
const tl = gsap.timeline({ paused: true });
tl.fromTo("#my-box", { x: -100 }, { x: 1700, duration: 1.2, ease: "power3.inOut" }, 0.5);
// Extend to data-duration so seeks past the last tween reach the blur callback.
tl.set(document.body, {}, DATA_DURATION);
// Call AFTER tweens, BEFORE window.__timelines registration.
// attachMotionBlur adds a tracking tween with onUpdate — must be called after
// tl.set()/tl.to() have established the final timeline duration.
attachMotionBlur("#my-box", tl, { shutterAngle: 720, samplesPerFrame: 16 });
window.__timelines = window.__timelines || {};
window.__timelines["my-composition"] = tl;
-->