Animated icons JS behaviour

Draw-in entrance

A set of Lucide-style line icons that ink themselves stroke by stroke as they enter view — the pen hops from path to path with a stepped cadence, hovering replays a single icon, and one switch re-inks the whole set.

Documentation

Real scenarios

The component as it is used in a product — not a specimen grid.

Icons that ink themselves in

Twelve Lucide-style line icons — a uniform 2px stroke with round joins — enter blank and draw themselves stroke by stroke as the grid reaches view; the pen hops from path to path on a stepped cadence. Hover any tile to replay just that icon.

  • Heart
  • Bell
  • Star
  • Sun
  • Moon
  • Search
  • Globe
  • Zap
  • Shield
  • Message
  • Lock
  • Sparkles

stroke-dasharray: 1 · pathLength normalized per stroke

Important props

Important props
Prop Type Default What it does
pathLength technique '1' Every path is normalised to length 1 so stroke-dasharray: 1 draws exactly that stroke — no measuring real lengths.
cadence ms '130' The stepped gap between an icon’s strokes; tiles themselves stagger 60ms apart so a draw wave sweeps the grid.
draw duration '550ms' Each stroke’s inking time on a soft ease; erasing is instant so replays start clean.
trigger behaviour 'in-view · once' An IntersectionObserver at a 20% threshold fires once and disconnects itself; cleaned up on destroy.
reduced-motion media 'static' Under prefers-reduced-motion every path sits fully drawn and static from the start.

Snippet

<div class="imd" x-data="{
    redraw() {
        window.dispatchEvent(new CustomEvent('imd-erase'));
        requestAnimationFrame(() => requestAnimationFrame(
            () => window.dispatchEvent(new CustomEvent('imd-draw'))));
    },
}" x-init="
    const io = new IntersectionObserver((entries) => entries.forEach((entry) => {
        if (entry.isIntersecting) {
            window.dispatchEvent(new CustomEvent('imd-draw'));
            io.disconnect();
        }
    }), { threshold: 0.2 });
    io.observe($refs.grid);
    $cleanup(() => io.disconnect());
">
    <ul class="imd-grid" x-ref="grid">
        @foreach ($icons as $i => $icon)
            <li class="imd-tile"
                x-data="{ d: false, replay() {
                    if (!this.d) return;
                    this.d = false;
                    requestAnimationFrame(() => requestAnimationFrame(() => (this.d = true)));
                } }"
                x-bind:data-drawn="d"
                @imd-draw.window="d = true"
                @imd-erase.window="d = false"
                @pointerenter="replay()">
                <svg viewBox="0 0 24 24" aria-hidden="true">
                    @foreach ($icon['paths'] as $p => $d)
                        <path pathLength="1" d="{{ $d }}" style="--d: {{ $i * 60 + $p * 130 }}ms" />
                    @endforeach
                </svg>
                <span>{{ $icon['en'] }}</span>
            </li>
        @endforeach
    </ul>

    <button type="button" class="imd-again" x-on:click="redraw()">
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/></svg>
        Draw them all again
    </button>
</div>

<style>
.imd-grid { display: grid; gap: .75rem; margin: 0; padding: 0; list-style: none;
            grid-template-columns: repeat(auto-fill, minmax(7.25rem, 1fr)); }
.imd-tile { display: grid; gap: .55rem; justify-items: center; padding: 1rem .5rem;
            border: 1px solid #e7e5e4; border-radius: .9rem; background: #fff; color: #1c1917; }
.imd-tile svg { inline-size: 2.4rem; }
.imd-tile path { fill: none; stroke: currentColor; stroke-width: 2;
                 stroke-linecap: round; stroke-linejoin: round;
                 stroke-dasharray: 1; stroke-dashoffset: 1;
                 transition: stroke-dashoffset .55s ease var(--d, 0s); }
.imd-tile[data-drawn='true'] path { stroke-dashoffset: 0; }
.imd-tile:not([data-drawn='true']) path { transition: none; }
@media (prefers-reduced-motion: reduce) {
    .imd-tile path { transition: none; stroke-dashoffset: 0; }
}
</style>
// app/Pages/IconMotion.jsx — Inertia (React) page; renders the React
// snippet below unchanged, with Inertia's Link wired into the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import DrawEntrance from '@/components/DrawEntrance';

export default function IconMotion() {
  return (
    <NabuXUIProvider locale="en" linkComponent={Link}>
      <main className="nx-page">
        <DrawEntrance />
      </main>
    </NabuXUIProvider>
  );
}
// DrawEntrance.jsx — Lucide-style icons inking themselves on entry
import { useEffect, useRef, useState } from 'react';
import '@nabuxai/ui-core/css';

const ICONS = [
  { id: 'heart', name: 'Heart', paths: ['M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z'] },
  { id: 'bell', name: 'Bell', paths: ['M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9', 'M10.3 21a1.94 1.94 0 0 0 3.4 0'] },
  { id: 'globe', name: 'Globe', paths: ['M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z', 'M3 12h18', 'M12 3a13.5 13.5 0 0 1 3.5 9A13.5 13.5 0 0 1 12 21a13.5 13.5 0 0 1-3.5-9A13.5 13.5 0 0 1 12 3Z'] },
  { id: 'lock', name: 'Lock', paths: ['M5 11a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2Z', 'M8 11V7a4 4 0 0 1 8 0v4'] },
  // …the rest of the set follows the same shape.
];

export default function DrawEntrance() {
  const gridRef = useRef(null);
  const [drawn, setDrawn] = useState(() => Object.fromEntries(ICONS.map((i) => [i.id, false])));

  const setAll = (value) => setDrawn(Object.fromEntries(ICONS.map((i) => [i.id, value])));
  // A double rAF lets the erase state paint before the draw transition restarts.
  const replay = (id) => {
    if (!drawn[id]) return;
    setDrawn((d) => ({ ...d, [id]: false }));
    requestAnimationFrame(() => requestAnimationFrame(() =>
      setDrawn((d) => ({ ...d, [id]: true }))));
  };
  const redrawAll = () => {
    setAll(false);
    requestAnimationFrame(() => requestAnimationFrame(() => setAll(true)));
  };

  useEffect(() => {
    const io = new IntersectionObserver((entries) => entries.forEach((entry) => {
      if (entry.isIntersecting) { setAll(true); io.disconnect(); }
    }), { threshold: 0.2 });
    io.observe(gridRef.current);
    return () => io.disconnect();
  }, []);

  return (
    <>
      <style>{`
        .imd-grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(7.25rem, 1fr)); }
        .imd-tile { display: grid; gap: .55rem; justify-items: center; padding: 1rem .5rem;
                    border: 1px solid #e7e5e4; border-radius: .9rem; background: #fff; color: #1c1917;
                    cursor: default; }
        .imd-tile svg { inline-size: 2.4rem; }
        .imd-tile path { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round;
                         stroke-linejoin: round; stroke-dasharray: 1; stroke-dashoffset: 1;
                         transition: stroke-dashoffset .55s ease var(--d, 0s); }
        .imd-tile[data-drawn='true'] path { stroke-dashoffset: 0; }
        .imd-tile:not([data-drawn='true']) path { transition: none; }
        .imd-again { display: inline-flex; align-items: center; gap: .45rem; margin-block-start: 1rem;
                     padding: .5rem 1rem; border: 1px solid #e7e5e4; border-radius: 999px; background: #fff;
                     font: 600 .8rem/1 'Inter', sans-serif; cursor: pointer; }
        @media (prefers-reduced-motion: reduce) { .imd-tile path { transition: none; stroke-dashoffset: 0; } }
      `}</style>
      <ul className="imd-grid" ref={gridRef}>
        {ICONS.map((icon, i) => (
          <li key={icon.id} className="imd-tile" data-drawn={!!drawn[icon.id]}
              onPointerEnter={() => replay(icon.id)}>
            <svg viewBox="0 0 24 24" aria-hidden="true">
              {icon.paths.map((d, p) => (
                <path key={p} pathLength="1" d={d} style={{ '--d': `${i * 60 + p * 130}ms` }} />
              ))}
            </svg>
            <span>{icon.name}</span>
          </li>
        ))}
      </ul>
      <button type="button" className="imd-again" onClick={redrawAll}>
        <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 3-6.7L3 8" /><path d="M3 3v5h5" /></svg>
        Draw them all again
      </button>
    </>
  );
}
<!-- DrawEntrance.vue — Lucide-style icons inking themselves on entry -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, reactive } from 'vue';
import '@nabuxai/ui-core/css';

const icons = [
  { id: 'heart', name: 'Heart', paths: ['M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z'] },
  { id: 'bell', name: 'Bell', paths: ['M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9', 'M10.3 21a1.94 1.94 0 0 0 3.4 0'] },
  { id: 'globe', name: 'Globe', paths: ['M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z', 'M3 12h18', 'M12 3a13.5 13.5 0 0 1 3.5 9A13.5 13.5 0 0 1 12 21a13.5 13.5 0 0 1-3.5-9A13.5 13.5 0 0 1 12 3Z'] },
  { id: 'lock', name: 'Lock', paths: ['M5 11a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2Z', 'M8 11V7a4 4 0 0 1 8 0v4'] },
  // …the rest of the set follows the same shape.
];
const drawn = reactive(Object.fromEntries(icons.map((i) => [i.id, false])));
let grid = $ref<HTMLElement | null>(null);
let io: IntersectionObserver | null = null;

const setAll = (value: boolean) => icons.forEach((i) => { drawn[i.id] = value; });
// A double rAF lets the erase state paint before the draw transition restarts.
const replay = (id: string) => {
  if (!drawn[id]) return;
  drawn[id] = false;
  requestAnimationFrame(() => requestAnimationFrame(() => { drawn[id] = true; }));
};
const redrawAll = () => {
  setAll(false);
  requestAnimationFrame(() => requestAnimationFrame(() => setAll(true)));
};

onMounted(() => {
  io = new IntersectionObserver((entries) => entries.forEach((entry) => {
    if (entry.isIntersecting) { setAll(true); io!.disconnect(); }
  }), { threshold: 0.2 });
  io.observe(grid!);
});
onBeforeUnmount(() => io?.disconnect());
</script>

<template>
  <ul class="imd-grid" :ref="(el) => (grid = el as HTMLElement)">
    <li v-for="(icon, i) in icons" :key="icon.id" class="imd-tile" :data-drawn="drawn[icon.id]"
        @pointerenter="replay(icon.id)">
      <svg viewBox="0 0 24 24" aria-hidden="true">
        <path v-for="(d, p) in icon.paths" :key="p" pathLength="1" :d="d" :style="{ '--d': `${i * 60 + p * 130}ms` }" />
      </svg>
      <span>{{ icon.name }}</span>
    </li>
  </ul>
  <button type="button" class="imd-again" @click="redrawAll">
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 3-6.7L3 8" /><path d="M3 3v5h5" /></svg>
    Draw them all again
  </button>
</template>

<style scoped>
.imd-grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(7.25rem, 1fr)); }
.imd-tile { display: grid; gap: .55rem; justify-items: center; padding: 1rem .5rem;
            border: 1px solid #e7e5e4; border-radius: .9rem; background: #fff; color: #1c1917; }
.imd-tile svg { inline-size: 2.4rem; }
.imd-tile path { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round;
                 stroke-linejoin: round; stroke-dasharray: 1; stroke-dashoffset: 1;
                 transition: stroke-dashoffset .55s ease var(--d, 0s); }
.imd-tile[data-drawn='true'] path { stroke-dashoffset: 0; }
.imd-tile:not([data-drawn='true']) path { transition: none; }
.imd-again { display: inline-flex; align-items: center; gap: .45rem; margin-block-start: 1rem;
             padding: .5rem 1rem; border: 1px solid #e7e5e4; border-radius: 999px; background: #fff;
             font: 600 .8rem/1 'Inter', sans-serif; cursor: pointer; }
@media (prefers-reduced-motion: reduce) { .imd-tile path { transition: none; stroke-dashoffset: 0; } }
</style>
<!-- DrawEntrance.svelte — Lucide-style icons inking themselves on entry -->
<script lang="ts">
  import { onMount } from 'svelte';
  import '@nabuxai/ui-core/css';

  const icons = [
    { id: 'heart', name: 'Heart', paths: ['M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z'] },
    { id: 'bell', name: 'Bell', paths: ['M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9', 'M10.3 21a1.94 1.94 0 0 0 3.4 0'] },
    { id: 'globe', name: 'Globe', paths: ['M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z', 'M3 12h18', 'M12 3a13.5 13.5 0 0 1 3.5 9A13.5 13.5 0 0 1 12 21a13.5 13.5 0 0 1-3.5-9A13.5 13.5 0 0 1 12 3Z'] },
    { id: 'lock', name: 'Lock', paths: ['M5 11a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2Z', 'M8 11V7a4 4 0 0 1 8 0v4'] },
    // …the rest of the set follows the same shape.
  ];
  let drawn = $state(Object.fromEntries(icons.map((i) => [i.id, false])));
  let grid: HTMLElement;

  const setAll = (value: boolean) => { drawn = Object.fromEntries(icons.map((i) => [i.id, value])); };
  // A double rAF lets the erase state paint before the draw transition restarts.
  const replay = (id: string) => {
    if (!drawn[id]) return;
    drawn = { ...drawn, [id]: false };
    requestAnimationFrame(() => requestAnimationFrame(() => (drawn = { ...drawn, [id]: true })));
  };
  const redrawAll = () => {
    setAll(false);
    requestAnimationFrame(() => requestAnimationFrame(() => setAll(true)));
  };

  onMount(() => {
    const io = new IntersectionObserver((entries) => entries.forEach((entry) => {
      if (entry.isIntersecting) { setAll(true); io.disconnect(); }
    }), { threshold: 0.2 });
    io.observe(grid);
    return () => io.disconnect();
  });
</script>

<ul class="imd-grid" bind:this={grid}>
  {#each icons as icon, i (icon.id)}
    <li class="imd-tile" data-drawn={drawn[icon.id]} onpointerenter={() => replay(icon.id)}>
      <svg viewBox="0 0 24 24" aria-hidden="true">
        {#each icon.paths as d, p}
          <path pathLength="1" {d} style="--d: {i * 60 + p * 130}ms" />
        {/each}
      </svg>
      <span>{icon.name}</span>
    </li>
  {/each}
</ul>
<button type="button" class="imd-again" onclick={redrawAll}>
  <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 3-6.7L3 8" /><path d="M3 3v5h5" /></svg>
  Draw them all again
</button>

<style>
.imd-grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(7.25rem, 1fr)); }
.imd-tile { display: grid; gap: .55rem; justify-items: center; padding: 1rem .5rem;
            border: 1px solid #e7e5e4; border-radius: .9rem; background: #fff; color: #1c1917; }
.imd-tile svg { inline-size: 2.4rem; }
.imd-tile path { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round;
                 stroke-linejoin: round; stroke-dasharray: 1; stroke-dashoffset: 1;
                 transition: stroke-dashoffset .55s ease var(--d, 0s); }
.imd-tile[data-drawn='true'] path { stroke-dashoffset: 0; }
.imd-tile:not([data-drawn='true']) path { transition: none; }
.imd-again { display: inline-flex; align-items: center; gap: .45rem; margin-block-start: 1rem;
             padding: .5rem 1rem; border: 1px solid #e7e5e4; border-radius: 999px; background: #fff;
             font: 600 .8rem/1 'Inter', sans-serif; cursor: pointer; }
@media (prefers-reduced-motion: reduce) { .imd-tile path { transition: none; stroke-dashoffset: 0; } }
</style>