/slidecrafting:animejs-revealjsfrom slidecrafting
Patterns for syncing anime.js animations with Quarto reveal.js fragment and slide navigation. Covers coordinate-space conversion, fragment lifecycle gotchas, self-cancelling loops, and cleanup on stop.
Ships in slidecrafting. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
References:
Notes distilled from building a collaborative-editing cursor animation for a Quarto reveal.js talk. This skill is about wiring an external animation library (anime.js) to reveal.js's fragment and slide lifecycle — not about CSS-only fragments (see the quarto-revealjs-fragment skill for those).
Load it directly in an include-after-body file:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/anime.min.js"></script>
Reveal.js scales the entire slide with a CSS transform to fit the viewport. Consequences:
getBoundingClientRect() returns screen-space values.slideCoord = screenPixels * (1280 / section.getBoundingClientRect().width).translateX/translateY on child elements are already in the slide's internal coordinate space (e.g. 1280×720) — that's what you want to animate to.getBoundingClientRect() is a snapshot. If layout changes afterward — a fragment fires, a CSS transition runs, an element resizes — cached positions are wrong. Remeasure after the change settles, not before.
For fragment-triggered layout shifts, wait for the CSS transition to finish before remeasuring: setTimeout(runAnim, 700) after a 0.6s CSS transition, for example.
Two animation systems on the same property is undefined behavior.
transition own layout properties (padding-left, width) used for structural shifts.transform and opacity on the animated elements.fragmentshown fires at the moment of trigger, not after reveal.js's own fragment animation completes. If you need to measure something that changes when the fragment appears, add a small delay before measuring.
fragmenthidden is the mirror event for back-navigation. Easy to forget — if you only handle shown, state gets out of sync when the user steps backward.
anime.js timelines use absolute time offsets. If layout shifts while a timeline is mid-play, the remaining keyframes target positions that no longer exist. There's no clean "pause here, resume after layout settles" — stop completely and restart fresh with new measurements.
Design animation loops so all state (translateY, width, text content) returns to its original value after one full cycle:
Track state variables (ty, imgCSS, header text) as the timeline is built. Each move snapshots values before mutating state, so targets are computed correctly even though the timeline plays later.
anime.js timelines are built synchronously then played asynchronously. If target values are computed lazily (inside callbacks), they reflect state at play time, not build time — wrong if earlier moves already ran.
Snapshot any values needed before calling state-mutating functions:
const snapHandleY = hy(blockIdx); // snapshot before ty changes
tl.add({ targets: el, translateY: snapHandleY + dragDY, ... });
ty[blockIdx] += dragDY; // mutate after
anime.js can animate properties on plain JS objects, not just DOM elements — useful for typewriter effects:
const state = { n: 0 };
tl.add({
targets: state,
n: text.length,
duration: text.length * 65,
easing: 'linear',
update: () => { el.textContent = text.slice(0, Math.round(state.n)) + '|'; },
});
Pass fromText/toText explicitly when building the timeline — don't read from the DOM inside the timeline build loop, since the DOM hasn't changed yet (the animation hasn't played).
If fragments should control distinct phases (e.g. "cursors appear" vs. "loop starts"), split into two functions:
appearAnim() — creates elements, runs the entry animation, sets currentTl.runAnim() — pauses currentTl, snaps elements to idle, starts the loop immediately.runAnim must pause any in-progress currentTl before starting. If appearAnim's entry timeline is still playing when runAnim fires (user advanced the fragment before it finished), the two animations fight over positions, corrupting any layout measurements a later step relies on.
function runAnim() {
if (running) return;
running = true;
if (currentTl) { currentTl.pause(); currentTl = null; } // kill appearAnim if still running
// snap elements to idle, then doLoop()
}
After a layout shift (fade out, then call runAnim), the loop restarts without an entry animation. Two things must happen at the top of runAnim:
anime.set(alice, { opacity: 1, translateX: aliceSnap.x, translateY: aliceSnap.y });
An effect that mutates DOM state incrementally (e.g. a typewriter appending | to textContent) will leave that state behind if the animation is paused mid-way (fragment back-navigation, layout shift, slide change) — the complete callback that would normally clean up never fires.
Fix: a dedicated cleanup helper, called from every stop path:
function cleanHeader() {
const h = document.getElementById('cblock-header');
if (h && h.textContent.endsWith('|')) h.textContent = h.textContent.slice(0, -1);
}
Gate distinct animation phases behind separate reveal.js fragments using invisible fragment spans:
<span class="fragment" id="appear-fragment" style="position:absolute;pointer-events:none"></span>
<span class="fragment" id="start-fragment" style="position:absolute;pointer-events:none"></span>
Wire each phase in fragmentshown/fragmenthidden:
Reveal.on('fragmentshown', ({ fragment }) => {
if (fragment.id === 'appear-fragment') appearAnim();
if (fragment.id === 'start-fragment') runAnim();
});
Reveal.on('fragmenthidden', ({ fragment }) => {
if (fragment.id === 'start-fragment') stopLoop(); // keep elements, stop loop
if (fragment.id === 'appear-fragment') stopAnim(true); // fly elements out
});
Back-navigation also fires slidechanged. On arrival at the slide, check which fragments are already visible to restore the right state:
Reveal.on('slidechanged', ({ currentSlide }) => {
if (currentSlide?.id === 'my-slide') {
const af = document.getElementById('appear-fragment');
const sf = document.getElementById('start-fragment');
if (sf?.classList.contains('visible')) runAnim();
else if (af?.classList.contains('visible')) appearAnim();
} else {
stopAnim();
}
});
Avoid putting ids on slides for JS targeting. Drop an empty marker element inside the slide instead:
::: {.my-slide}
:::
Detect it in slidechanged via currentSlide.querySelector('.my-slide'), and find the section from any DOM query via .closest('section'):
const section = document.querySelector('.my-slide')?.closest('section');
For CSS, use :has() to style the section:
section:has(.my-slide) {
overflow: hidden;
}
This keeps slide ids out of JS and CSS, and works naturally with Quarto's ::: div syntax.
Trap: checking whether a section itself carries the marker class with section.querySelector('.my-slide') returns null even when the class is on the section — querySelector only walks descendants. Use section.classList.contains('my-slide') or section.matches('.my-slide') instead.
When multiple slides need an independently-running instance of the same behavior, wrap the state and loop in a factory instead of top-level variables:
function makeFloatBehavior(defs) {
let running = false;
function floatOne(el) {
if (!running) return;
anime({ targets: el, translateX: x, translateY: y, ..., complete: () => floatOne(el) });
}
return {
start(section) {
if (running) return;
running = true;
ensureElements(section, defs);
defs.forEach(({ id }) => { ... floatOne(el); });
},
stop() {
running = false;
anime.remove(...); anime({ targets, opacity: 0, ... });
},
};
}
const floatBehavior = makeFloatBehavior(FLOAT_DEFS);
// floatBehavior.start(currentSlide) / floatBehavior.stop()
Each factory call gets its own running flag, so state doesn't leak between slides.
Extract the "create if missing, append to section" pattern too, since it recurs wherever elements might not exist yet (entry animation, loop restart after a layout shift):
function ensureElements(section, defs) {
defs.forEach(({ id, ... }) => {
if (!document.getElementById(id)) section.appendChild(makeElement(id, ...));
});
}
Define element lists as constants and pass them to ensureElements, the factory, and any stop/fly-out logic, to avoid the same array hardcoded in three places.
For floating elements that should avoid the slide center (e.g. to leave room for text), sample from one of four edge strips instead of the full slide area:
function edgePos() {
const region = Math.floor(Math.random() * 4);
switch (region) {
case 0: return { x: Math.random() * 250 + 30, y: Math.random() * 660 + 30 }; // left
case 1: return { x: Math.random() * 250 + 1000, y: Math.random() * 660 + 30 }; // right
case 2: return { x: Math.random() * 1220 + 30, y: Math.random() * 130 + 30 }; // top
default: return { x: Math.random() * 1220 + 30, y: Math.random() * 100 + 590 }; // bottom
}
}
Tune the strip widths (250, 130, 100) to give the center content more or less breathing room.
Control how much animations overlap by what time value a builder function returns. Returning earlier than the true end time lets the next move start while the current one is still finishing:
return startAt + 2100; // next move starts here even though the element settles at 2830
Tune up (less overlap) or down (more overlap) without touching any individual animation.
Reveal.on('ready', cb) only fires for future ready events. If the listener is registered after Reveal has finished initializing — common when scripts are injected via include-after-body — the callback never runs.
Fix: register slidechanged/fragmentshown/fragmenthidden listeners unconditionally as soon as Reveal exists. Those handlers fire on future user navigation regardless of whether ready already passed, so they don't need ready-gating.
function onReveal(cb) {
function go() {
if (typeof Reveal === 'undefined' || !Reveal.on) {
setTimeout(go, 50);
return;
}
cb(); // register listeners now; do not wait for 'ready'
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', go);
} else {
go();
}
}
Only the boot-time "did we land directly on slide X?" check actually needs to know Reveal is ready (so Reveal.getCurrentSlide() returns a real value). For that, poll briefly or accept that direct-deep-link entry won't auto-trigger an ambient animation until the user navigates once.
If N slide modules all call into one shared helper (a registerSlide-style function, an element factory, etc.), a bug in the helper silently breaks every dependent feature at once. Cheap surface checks — HTTP 200, syntax passes, classes present in rendered HTML — won't catch behavioral bugs in the helper. Only running the deck in a real browser does.
When the unit of failure is the helper, the smallest viable test is "does anything trigger at all?" — there's no narrower test that's useful. Run that before assuming the foundation is sound; skipping it to save time costs more time when every module has to be re-investigated to localize the bug.
fragmenthidden, not just fragmentshown.ready.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
5 skills in this plugin
/auto-animate-elementsHow to spruce up Quarto reveal.js slides with auto-animate by carrying a small cast of persistent elements (shapes) across slides that grow, shrink, and rearrange. Use this skill whenever the user wants elements that persist/animate/reshape between reveal.js slides, a set of shapes that move or morph across a deck, an "auto-animate" motif, or a bento-style animated presentation in Quarto. Covers the data-id contract, the SCSS-plus-inline split, content and figures inside elements, rotation/3D/filter tweening via the autoanimate-extra extension, and the "enough, not all" pacing rule./fragment-stop-and-retargetRetrofit a custom reveal.js fragment/slide animation (or any JS-driven exit/entrance effect) so that interrupting it mid-flight — navigating the opposite direction before it finishes — stops and redirects from the live state instead of finishing the old animation or snapping. Use when a fragment's custom JS animation doesn't reverse cleanly if triggered before the forward (or reverse) pass completes, or when a staggered/choreographed multi-element effect only picks up a direction change after a long delay./quarto-revealjsBuild, theme, and present reveal.js slide decks authored in Quarto. Use this skill whenever the user is working on Quarto reveal.js slides (a `.qmd` with `format: revealjs`) - starting a new deck, restyling it (fonts, colors, sizes, SCSS themes), positioning content (columns, absolute positioning, background images, overlays, cards), showing code or plots well, adding fragment or auto-animate motion, or presenting and exporting (speaker notes, speaker view, PDF, chalkboard, multiplex). Routes to focused reference files under references/.npx claudepluginhub emilhvitfeldt/slidecrafting-book.com --plugin slidecraftingOther plugins' skills for the same job