framework / astroAstro 4+ · zero hydration
Astro scroll animations. No island required.
Astro’s whole premise is shipping no JavaScript unless you ask for it, so wrapping content in a hydrated component just to animate it defeats the point. Here the markup stays a server component: mark elements with a data attribute, pass options as JSON, and call one init function from a single client script. One small script for the whole page, not an island per animation.
- astro entry
- 12.3 KB
- Every API
- 11.9 KB
- GSAP stack
- 47.5 KB
- Version
- 2.31.0
Quick start
Import from svg-scroll-draw/astro — every sample below is written against the wrappers this package actually exports.
sample 01Hero.astro
Mark the element, init once
Hero.astro--- // No client directive — this stays a server component. --- <div data-scroll-draw data-scroll-draw-options='{"easing":"ease-out","fade":true}'> <svg viewBox="0 0 200 100"> <path d="M10 50 Q 100 10 190 50" stroke="#111" fill="none" /> </svg> </div> <script> import { initScrollDraw } from 'svg-scroll-draw/astro'; initScrollDraw(); </script>sample 02Layout.astro
Every API in one call
Layout.astro<script> // initAll wires up draw, animate, counter and text in one pass — // put it in your layout and every page gets it. import { initAll } from 'svg-scroll-draw/astro'; initAll(); </script>sample 03Features.astro
The other attributes
Features.astro<h2 data-scroll-text data-scroll-text-options='{"split":"words","stagger":0.05}'> Every API, one package. </h2> <span data-scroll-counter data-scroll-counter-options='{"to":48000}'>0</span> <div data-scroll-animate data-scroll-animate-options='{"props":{"opacity":[0,1],"transform":["translateY(24px)","translateY(0px)"]}}'> Fades up on scroll. </div>
Options are read from the JSON attribute, so they must be valid JSON — double-quoted keys, no trailing commas, no JS expressions. Single-quoting the attribute in your markup is the easiest way to keep the inner quotes valid.
Init on astro:page-load with View Transitions
Astro bundles a component's <script> and runs it once per full page load. With <ClientRouter /> in the layout (<ViewTransitions /> before Astro 5), a link click swaps the page without reloading it, so an init at the top of a script covers the page it first runs on and none after it. Without the router, the top-level call in the quick start is all you need.
With the router, run initAll() from an astro:page-load listener, which fires on the first load and after every navigation. The engines on the outgoing page are not torn down when it is swapped out. Until destroy() runs, each stays in the library's instance registry, which keeps the swapped-out nodes in memory, and one running in JavaScript rather than native CSS keeps its window resize listeners too. Keep what initAll() returned and destroy it on astro:before-swap.
A second init on the same element is safe. Each element keeps one engine per kind, so when the quick start's initScrollDraw() and the layout listener both reach an element on the first load, the second call keeps the engine the first one started. Before 2.19.0 it put a second engine on the element.
---
import { ClientRouter } from 'astro:transitions';
---
<html lang="en">
<head>
<ClientRouter />
</head>
<body>
<slot />
<script>
import { initAll, type ScrollDrawInstance } from 'svg-scroll-draw/astro';
let running: ScrollDrawInstance[] = [];
document.addEventListener('astro:page-load', () => {
running = Object.values(initAll()).flat();
});
document.addEventListener('astro:before-swap', () => {
running.forEach((i) => i.destroy());
running = [];
});
</script>
</body>
</html>Edit the attribute to change options
The init functions watch each element's options attribute with a MutationObserver. Set a new value on data-scroll-draw-options (or the animate, counter or text attribute) and that element's engine is destroyed and rebuilt from the new JSON, while other elements keep running. The instances init returned follow the rebuild, so pause(), seek() and destroy() reach the current engine without a second init.
The check compares the attribute text, not the parsed options. The same string written back keeps the engine. The same options with the keys in another order, or with different spacing, rebuild it. Writing JSON.stringify of an object with a fixed shape keeps the text stable.
Text that is not valid JSON does not throw: the element is rebuilt as if the attribute were empty. After destroy(), edits to the attribute are ignored until an init runs again.
Each element has one attribute watched. On an element that carries two kinds, say data-scroll-animate and data-scroll-counter, it is the attribute of the kind whose init ran last (initAll() runs draw, animate, counter, then text), and an edit to the other is not seen until its init runs again. Give a kind whose options change an element of its own.
<div id="signature" data-scroll-draw data-scroll-draw-options='{"speed":1}'>
<svg viewBox="0 0 200 100">
<path d="M10 60 C 60 10, 140 110, 190 40" stroke="currentColor" fill="none" />
</svg>
</div>
<button id="faster" type="button">Draw faster</button>
<script>
import { initScrollDraw } from 'svg-scroll-draw/astro';
initScrollDraw();
document.getElementById('faster')?.addEventListener('click', () => {
const el = document.getElementById('signature');
// The observer sees the edit and rebuilds this element's engine only.
if (el) el.dataset.scrollDrawOptions = JSON.stringify({ speed: 2 });
});
</script>Options in frontmatter, functions in a script
Frontmatter runs on the server, so it can build options but cannot start engines. An init called there has no document to scan: it returns an empty array (initAll() an object of four empty arrays) and nothing animates, with no error to point at the cause. Build the object in frontmatter and write it into the attribute with JSON.stringify. Astro escapes the quotes in an attribute expression, so values from Astro.props or a content collection need no hand-quoting.
JSON.stringify leaves out functions without a word, so format, a custom easing function and callbacks such as onComplete never reach the browser. Named easings such as "spring" are strings and do. On a counter, "decimals" sets fixed decimal places. A format that arrives as a string is removed and the counter falls back to decimals, or to whole numbers.
When an element needs a function, start it from the script with the core API. scrollCounter from the main entry takes the same options as the attribute, and there format can be a function. Leave decimals out of that call: when both are set, decimals wins and format is never called.
---
// Server side: build the options here, do not init here.
const stats = [
{ label: 'Teams', to: 4200 },
{ label: 'Uptime', to: 99.9, decimals: 1 },
];
---
{stats.map(({ label, ...options }) => (
<p>
<span data-scroll-counter data-scroll-counter-options={JSON.stringify(options)}>0</span>
{label}
</p>
))}
<p>Revenue <span id="revenue">$0</span></p>
<script>
import { initScrollCounter } from 'svg-scroll-draw/astro';
import { scrollCounter } from 'svg-scroll-draw';
initScrollCounter();
// format is a function, so it starts here rather than from the attribute.
scrollCounter('#revenue', {
to: 1250000,
format: (n) => '$' + Math.round(n).toLocaleString('en-US'),
});
</script>Size
svg-scroll-draw/astro is 12.3 KB gzipped, the engines it wraps included.
Measured 2026-08-14 at gzip level 9 against gsap 3.15.0 and svg-scroll-draw 2.31.0. Full comparison →
Astro questions
How do I add scroll animation to Astro without hydration?
Add data-scroll-draw to any element, pass options via data-scroll-draw-options as JSON, and call initScrollDraw() from a plain <script> tag. Astro bundles that script but the component itself is never hydrated, so no framework runtime ships.
Do I need a client:load directive?
No. A client directive hydrates a framework component; this uses a plain script tag instead, so there is no island and no framework runtime in the bundle.
Can I initialise every animation type at once?
Yes. initAll() wires up draw, animate, counter and text in a single pass. Put it in your layout and every page is covered by one small script.