Published
Edited
Feb 17, 2021
2 forks
14 stars
Insert cell
Insert cell
Insert cell
animation({
duration: 4000,
size: 600,
steps: 15,
dashes,
strokeWidth: 12,
background: 'black',
color: 'white',
})
Insert cell
function* animation(options = {}) {
const {
size = 600,
radius = 1,
background = 'white',
color = 'black',
steps = 10,
strokeWidth = 10,
dashes = 10,
duration = 5000,
} = options;
const ease = t => Math.cos(t*Math.PI*2) * .5 + .5,
circles = Array.from(
{length: steps},
(_, i) => svg`<circle r=${size/2*radius*i/steps}>`
),
view = svg`
<svg viewBox="${-size/2} ${-size/2} ${size} ${size}"
width=${size} preserveAspectRatio="xMidYMid meet" style="max-width:100%">
<rect x=${-size/2} y=${-size/2} width=${size} height=${size} fill=${background} />
<g transform="rotate(90)" fill=none stroke=${color} stroke-linecap=round>
${circles}`;
const update = tGlobal => {
for(const [i, c] of circles.entries()) {
// Offset avoids flickering.
const t = -.001 + ease(i/circles.length + tGlobal),
segment = c.getTotalLength() / dashes,
dash = segment * (1-t),
gap = segment * t;
c.setAttribute('stroke-dasharray', `${dash},${gap}`);
c.setAttribute('stroke-dashoffset', -gap / 2);
c.setAttribute('stroke-width', 2 + t * strokeWidth);
}
};
// To call .getTotalLength(), the element has to attached to the DOM.
yield view;
while(true) {
update(Date.now() / duration % 1);
yield view;
}
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more