Marble Zigzag Ramps Roll And Drop

Small marbles roll down three opposing ramps, dropping off each end onto the next one below until they fall out of the frame.

Motion specs

Duration
Continuous
Easing
linear
Technique
Canvas 2D 3D projection
Library
Canvas 2D
Category
Canvas & Particles
License
Free for personal and commercial use
AI prompt
Create a professional web motion called "Marble Zigzag Ramps Roll And Drop" (كرات تتدحرج على منحدرات متعرجة).
• Target: عنصر الكانفاس (canvas)
• Duration: Continuous
• Easing: linear
• Colors: dark charcoal background #0d0e11, surface #1e2129, white text #f3f4f6, and one fiery orange accent #ff5e1a
• Direction: كرات صغيرة تتدحرج على ثلاثة منحدرات متعاكسة وتسقط من كل طرف إلى المنحدر التالي
• Library / technique: Canvas 2D 3D projection
• Output format: a fully self-contained HTML5 / CSS3 / vanilla JS file
• Behavior: Small marbles roll down three opposing ramps, dropping off each end onto the next one below until they fall out of the frame. The animation loops automatically with a short pause between cycles.
• Performance: prefer transform and opacity, keep 60fps, no external libraries.
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>Marble Zigzag Ramps Roll And Drop</title>
  <style>
    html,body{margin:0;height:100%;background:#0d0e11;overflow:hidden}
    #stage{position:relative;width:100%;height:100vh;min-height:220px;overflow:hidden;background:#090a0d}
  </style>
</head>
<body>
  <div id="stage"></div>
  <script>
const T=[];

const S=document.getElementById('stage');
const B=document.createElement('div');
B.style.cssText='position:absolute;left:50%;top:50%;width:400px;height:220px;margin:-110px 0 0 -200px;overflow:hidden;background:#0d0e11;direction:'+(document.documentElement.lang==='ar'?'rtl':'ltr')+';';
S.appendChild(B);
function fit(){const s=Math.min(innerWidth/400,innerHeight/220);B.style.transform='scale('+s+')';}
fit();addEventListener('resize',fit);
const AC='#ff5e1a',W='#f3f4f6',MU='#a1a8b3',SF='#1e2129',DK='#0d0e11';
const AR=document.documentElement.lang==='ar';
let TM=[],GEN=0;
function clr(){TM.forEach(clearTimeout);TM=[];GEN++;B.innerHTML='';}
let FB=1.3;
function el(t,css,txt,p){const e=document.createElement(t);css=(css||'').replace(/font:(\d+) (\d+(?:\.\d+)?)px/,(m,w,s)=>{s=+s;s=s<=22?s*FB:s<=34?s*(1+(FB-1)*.4):s;return 'font:'+w+' '+s.toFixed(1)+'px';});if(AR)css=css.replace('system-ui,sans-serif',"'Cairo',system-ui,sans-serif");e.style.cssText=css;if(txt!=null)e.textContent=txt;(p||B).appendChild(e);return e;}
function an(e,k,o){return e.animate(k,Object.assign({fill:'both',easing:'cubic-bezier(.16,1,.3,1)'},o||{}));}
function at(ms,f){const g=GEN;TM.push(setTimeout(()=>{if(g===GEN)f();},ms));}
function tw(ms,f,delay){const g=GEN;const st=performance.now()+(delay||0);(function s(n){if(g!==GEN)return;const p=Math.max(0,Math.min(1,(n-st)/ms));f(p,p*ms);if(n-st<ms)requestAnimationFrame(s);})(performance.now());}
function U(s){return AR?s.split(' '):s.split('');}
function cv(){const c=el('canvas','position:absolute;left:0;top:0');c.width=400;c.height=220;return c.getContext('2d');}
function run(b){clr();const d=b();if(typeof d==='number')TM.push(setTimeout(()=>run(b),d+1200));}
const ez=p=>p<.5?2*p*p:1-Math.pow(-2*p+2,2)/2;
const NS='http://www.w3.org/2000/svg';
function sv(t,a,p){const e=document.createElementNS(NS,t);for(const k in a)e.setAttribute(k,a[k]);(p||B).appendChild(e);return e;}
function svg(){const s=sv('svg',{viewBox:'0 0 400 220',width:400,height:220});s.style.cssText='position:absolute;left:0;top:0';return s;}

function rowEl(top,fe,fa,ge,ga){return el('div','position:absolute;left:0;right:0;top:'+top+'px;display:flex;justify-content:center;align-items:baseline;gap:'+(AR?ga:ge)+'px;font:900 '+(AR?fa:fe)+'px system-ui,sans-serif;color:'+W+';white-space:pre');}
function scn(){const c='position:absolute;left:0;top:0;width:400px;height:220px;display:flex;align-items:center;justify-content:center;font:900 44px system-ui,sans-serif;';const a=el('div',c+'background:'+SF+';color:'+W,T[0]);const b=el('div',c+'background:'+AC+';color:'+DK,T[1]);return [a,b];}
function dr(p,dl,du,e){const L=p.getTotalLength();p.style.strokeDasharray=L;p.style.strokeDashoffset=L;return an(p,[{strokeDashoffset:L},{strokeDashoffset:0}],{delay:dl,duration:du,easing:e||'cubic-bezier(.65,0,.35,1)'});}
function ST(c,w){return {fill:'none',stroke:c,'stroke-width':w||4,'stroke-linecap':'round','stroke-linejoin':'round'};}
function cur(path,dl,du,e){const c=el('div','position:absolute;left:0;top:0;width:18px;height:24px;background:#f3f4f6;clip-path:polygon(0 0,0 100%,32% 76%,58% 100%,76% 90%,52% 66%,100% 60%);z-index:20;filter:drop-shadow(0 2px 3px #000)');an(c,path.map(q=>({transform:'translate('+q[0]+'px,'+q[1]+'px)'})),{delay:dl,duration:du,easing:e||'ease-in-out'});return c;}
function units(r,u){return u.map(x=>el('span','display:inline-block',x,r));}


run(()=>{const FN=AR?"Cairo,system-ui,sans-serif":"system-ui,sans-serif";const OR='#ff5e1a',WH='#f3f4f6',GR='#6b7280';const BG=(x)=>{x.fillStyle='#0d0e11';x.fillRect(0,0,400,220);};const rnd=(i)=>Math.abs(Math.sin(i*12.9898+1.7)*43758.5453)%1;const RT=(p,ax,ay)=>{let [x,y,z]=p;let c=Math.cos(ay),s=Math.sin(ay);[x,z]=[x*c+z*s,-x*s+z*c];c=Math.cos(ax);s=Math.sin(ax);[y,z]=[y*c-z*s,y*s+z*c];return [x,y,z];};const PJ=(p,cx,cy,f)=>{const k=f/(f+p[2]);return [cx+p[0]*k,cy+p[1]*k,k];};const DT=(x,X,Y,r,c,a)=>{x.globalAlpha=a==null?1:a;x.fillStyle=c;x.beginPath();x.arc(X,Y,Math.max(.3,r),0,7);x.fill();x.globalAlpha=1;};const SAMP=(txt,fs,step,w,h)=>{const o=document.createElement('canvas');o.width=w;o.height=h;const q=o.getContext('2d');q.font='900 '+fs+'px '+FN;const mw=q.measureText(txt).width;if(mw>w-12){fs=Math.floor(fs*(w-12)/mw);}q.fillStyle='#fff';q.font='900 '+fs+'px '+FN;q.textAlign='center';q.textBaseline='middle';q.fillText(txt,w/2,h/2);const d=q.getImageData(0,0,w,h).data;const out=[];for(let y=0;y<h;y+=step)for(let X=0;X<w;X+=step)if(d[(y*w+X)*4+3]>128)out.push([X,y]);return out;};const x=cv();const MB=s=>()=>{s=s+0x6D2B79F5|0;let u=Math.imul(s^s>>>15,1|s);u=u+Math.imul(u^u>>>7,61|u)^u;return((u^u>>>14)>>>0)/4294967296;};const SG=[[60,48,250,82],[340,112,150,146],[60,176,250,206]];let M,lc=0,ns=0;const init=()=>{M=[];ns=0;};init();tw(1e9,(p,t)=>{BG(x);const c=t%7600;let dt=c-lc;if(dt<0){init();dt=0;}dt=Math.min(dt,33);lc=c;const rg=MB(3+Math.floor(t/7600));while(ns<12&&c>=ns*450){M.push({x:64+rg()*6,y:22,vx:.04,vy:0,k:ns%3});ns++;}const sub=4,h=dt/sub;for(let s=0;s<sub;s++)M.forEach(m=>{m.vy+=.0013*h;m.x+=m.vx*h;m.y+=m.vy*h;SG.forEach(q=>{const ex=q[2]-q[0],ey=q[3]-q[1],L2=ex*ex+ey*ey;let u=((m.x-q[0])*ex+(m.y-q[1])*ey)/L2;u=Math.max(0,Math.min(1,u));const px=q[0]+ex*u,py=q[1]+ey*u;let dx=m.x-px,dy=m.y-py;const d=Math.hypot(dx,dy);if(d<5&&d>0){dx/=d;dy/=d;m.x=px+dx*5;m.y=py+dy*5;const vn=m.vx*dx+m.vy*dy;if(vn<0){m.vx-=(1.25)*vn*dx;m.vy-=(1.25)*vn*dy;}m.vx*=.9995;}});});SG.forEach(q=>{const n=Math.floor(Math.hypot(q[2]-q[0],q[3]-q[1])/6);for(let i=0;i<=n;i++)DT(x,q[0]+(q[2]-q[0])*i/n,q[1]+(q[3]-q[1])*i/n+6,1.3,'#6b7280',.8);});const CL=[OR,'#f3f4f6','#9ca3af'];M.forEach(m=>{if(m.y<236)DT(x,m.x,m.y,4.6,CL[m.k],1);});});});
  </script>
</body>
</html>

Related templates