Bubble Sort Bars

Random bars sorted by swapping neighbors before your eyes, highlighting the two bars being compared.

Motion specs

Duration
4.5s
Easing
cubic-bezier(.16,1,.3,1)
Technique
JS algorithm + WAAPI
Library
JS algorithm + WAAPI
Category
Animated Charts
License
Free for personal and commercial use
AI prompt
Create a web motion called "Bubble Sort Bars" (ترتيب الأعمدة خطوة بخطوة).
• Description: Random bars sorted by swapping neighbors before your eyes, highlighting the two bars being compared.
• Duration: 4.5s, loops automatically with a 1.5 s pause between cycles.
• Colors: background #0d0e11, text #f3f4f6, one accent color #ff5e1a.
• Technique: JS algorithm + WAAPI
• Output: a standalone HTML/CSS/JS file, animate transform and opacity only, runs only while visible (IntersectionObserver) and respects prefers-reduced-motion.
Full code (HTML + CSS + JS)
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Bubble Sort Bars</title>
  <style>
    body { margin: 0; background: #0d0e11; color: #f3f4f6; font-family: system-ui, sans-serif; }
    .fxn{position:relative;width:100%;height:220px;overflow:hidden;display:grid;place-items:center;font-family:system-ui,sans-serif;color:#f3f4f6}
    .fxn .zsr{display:flex;align-items:flex-end;gap:5px;height:150px;direction:ltr}.fxn .zsr i{display:block;width:14px;border-radius:3px 3px 0 0;background:#3a3f4b;transition:background .12s}.fxn .zsr i.c{background:#ff5e1a}.fxn .zsr i.d{background:#ffd2b8}
  </style>
</head>
<body>
  <div id="stage"><div class="fxn"><div class="zsr"></div></div></div>
  <script>
    function play(root){
    const q=s=>root.querySelector(s),qa=s=>[...root.querySelectorAll(s)],T=[],A=[],F=[];
    const R=matchMedia('(prefers-reduced-motion: reduce)').matches;
    const at=(ms,f)=>{T.push(setTimeout(f,R?0:ms))};
    const an=(e,k,o={})=>{const a=e.animate(k,{duration:600,fill:'both',easing:'cubic-bezier(.16,1,.3,1)',...o});if(R){try{a.finish()}catch(_){}}A.push(a);return a};
    const raf=f=>{let id,on=true;const l=t=>{if(!on)return;f(t);if(!R)id=requestAnimationFrame(l)};id=requestAnimationFrame(l);F.push(()=>{on=false;cancelAnimationFrame(id)})};
    const tween=(dur,delay,fn)=>{at(delay,()=>{const t0=performance.now();let on=true,id;const l=now=>{if(!on)return;const p=R?1:Math.min(1,(now-t0)/dur);fn(1-Math.pow(1-p,3));if(p<1)id=requestAnimationFrame(l)};id=requestAnimationFrame(l);F.push(()=>{on=false;cancelAnimationFrame(id)})})};
    const box=q('.zsr');const v=[8,3,10,5,12,2,9,6,11,4,7,1];v.forEach(n=>{const b=document.createElement('i');b.style.height=n*11+'px';box.appendChild(b)});const bs=qa('.zsr i'),ar=v.slice();const ops=[];(()=>{const a=ar.slice();for(let i=0;i<a.length;i++)for(let j=0;j<a.length-1-i;j++){ops.push([j,a[j]>a[j+1]]);if(a[j]>a[j+1]){[a[j],a[j+1]]=[a[j+1],a[j]]}}})();let k=0;const setH=()=>{bs.forEach((b,i)=>{b.style.height=ar[i]*11+'px';b.className=''})};if(R){ar.sort((a,b)=>a-b);setH();bs.forEach(b=>b.className='d')}else{const id=setInterval(()=>{if(k>=ops.length){clearInterval(id);bs.forEach(b=>b.className='d');return}const [j,sw]=ops[k++];bs.forEach(b=>b.className='');if(sw){[ar[j],ar[j+1]]=[ar[j+1],ar[j]]}setH();bs[j].className='c';bs[j+1].className='c'},k<30?70:55);F.push(()=>clearInterval(id))}
    return()=>{T.forEach(clearTimeout);A.forEach(a=>a.cancel());F.forEach(f=>f())};
    }
    const MS = 5400, PAUSE = 1500;
    const host = document.getElementById('stage');
    const init = host.innerHTML;
    const still = matchMedia('(prefers-reduced-motion: reduce)').matches;
    let stop = null, timer = null, shown = false;
    function run() { if (stop) stop(); host.innerHTML = init; stop = play(host.firstElementChild); }
    function loop() { run(); if (!still) timer = setTimeout(loop, MS + PAUSE); }
    new IntersectionObserver(([e]) => {
      if (e.isIntersecting && !shown) { shown = true; loop(); }
      else if (!e.isIntersecting && shown) { shown = false; clearTimeout(timer); if (stop) stop(); }
    }, { threshold: 0.1 }).observe(host);
  </script>
</body>
</html>

Related templates