مذبذب الطيف الصوتي

محاكاة حية لأعمدة التردد الصوتي بتنعيم حركي وتلوين قمم الذروة.

مواصفات الحركة

المدة
مستمر (60fps)
منحنى التسارع
Lerp Smoothing
التقنية
Canvas Spectrum
المكتبة
Canvas 2D Harmonic Interpolation
التصنيف
خلفيات Canvas والجسيمات
الرخصة
مجاني للاستخدام الشخصي والتجاري
أمر الذكاء الاصطناعي (AI Prompt)
أنشئ حركة ويب احترافية باسم "مذبذب الطيف الصوتي" (Procedural Audio Spectrum Bars).
• العنصر المستهدف (Target): أعمدة الطيف (.spec-canvas)
• المدة (Duration): مستمر (60fps)
• منحنى التسارع (Easing): Lerp Smoothing
• الألوان (Colors): خلفية فحمية داكنة #0d0e11، سطح #16181e، نص أبيض #f3f4f6، ولون تمييز برتقالي ناري واحد #ff5e1a
• الاتجاه (Direction): نبض عمودي متناظر من المنتصف
• المكتبة/التقنية (Library): Canvas 2D Harmonic Interpolation
• صيغة الإخراج (Output Format): ملف HTML5/CSS3/Vanilla JS مستقل بالكامل (Self-Contained)
• قيود الأداء (Performance): تحريك خصائص GPU الآمنة فقط (transform, opacity, clip-path, filter)، سرعة 60fps، استخدام IntersectionObserver للتشغيل عند الظهور، ودعم @media (prefers-reduced-motion: reduce).
الكود الكامل (HTML + CSS + JS)
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>مذبذب الطيف الصوتي — Procedural Audio Spectrum Bars</title>
  <style>
    body { margin: 0; background: #0d0e11; color: #f3f4f6; font-family: system-ui, sans-serif; }
    :root { --font-mono: ui-monospace, Menlo, Consolas, monospace; }
    #stage { position: relative; width: 100%; height: 220px; box-sizing: border-box; padding: 16px; display: flex; align-items: center; justify-content: center; overflow: hidden; background: #090a0d; }
  </style>
</head>
<body>
  <div id="stage"></div>
  <script>
    function play(stage) {
      const fx = { id: 60, mode: "v_spec" };
      const timers = [], rafs = [], anims = [], listeners = [];
      const regTimer = (t) => { timers.push(t); return t; };
      const regRaf = (r) => { rafs.push(r); return r; };
      const regAnim = (a) => { anims.push(a); return a; };
      const regListen = (el, ev, fn) => { el.addEventListener(ev, fn); listeners.push([el, ev, fn]); };
      const accent = '#ff5e1a', white = '#f3f4f6', muted = '#a1a8b3', surface = '#1e2129';
      const svgEl = (tag, attrs = {}) => {
        const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
        Object.entries(attrs).forEach(([k, v]) => el.setAttribute(k, v));
        return el;
      };
      switch (fx.mode) {
        case 'v_const':
        case 'v_star':
        case 'v_matrix':
        case 'v_vortex':
        case 'v_ember':
        case 'v_repel':
        case 'v_ribbon':
        case 'v_squares':
        case 'v_sonar':
    case 'v_spec': {
              const canvas = document.createElement('canvas');
              const W = 320, H = 170;
              canvas.width = W; canvas.height = H;
              canvas.style.cssText = 'width:100%;height:100%;display:block;';
              stage.appendChild(canvas);
              const ctx = canvas.getContext('2d');
              let mx = -999, my = -999;
              regListen(canvas, 'pointermove', (e) => {
                const r = canvas.getBoundingClientRect();
                mx = ((e.clientX - r.left) / r.width) * W;
                my = ((e.clientY - r.top) / r.height) * H;
              });
              regListen(canvas, 'pointerleave', () => { mx = -999; my = -999; });
    
              if (fx.mode === 'v_const') {
                const pts = Array.from({ length: 24 }, () => ({ x: Math.random() * W, y: Math.random() * H, vx: (Math.random() - 0.5) * 1.1, vy: (Math.random() - 0.5) * 1.1 }));
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  pts.forEach((p, i) => {
                    p.x = (p.x + p.vx + W) % W; p.y = (p.y + p.vy + H) % H;
                    for (let j = i + 1; j < pts.length; j++) {
                      const q = pts[j], d = Math.hypot(p.x - q.x, p.y - q.y);
                      if (d < 68) {
                        ctx.strokeStyle = `rgba(255,94,26,${(1 - d / 68) * 0.65})`;
                        ctx.beginPath(); ctx.moveTo(p.x, p.y); ctx.lineTo(q.x, q.y); ctx.stroke();
                      }
                    }
                    ctx.fillStyle = white; ctx.beginPath(); ctx.arc(p.x, p.y, 2, 0, Math.PI * 2); ctx.fill();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_star') {
                const stars = Array.from({ length: 48 }, () => ({ x: (Math.random() - 0.5) * W, y: (Math.random() - 0.5) * H, z: Math.random() * W }));
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  stars.forEach(s => {
                    s.z -= 3.2; if (s.z <= 1) s.z = W;
                    const sx = (s.x / s.z) * 140 + W / 2, sy = (s.y / s.z) * 140 + H / 2;
                    const r = Math.max(0.6, (1 - s.z / W) * 2.8);
                    ctx.fillStyle = s.z < W * 0.35 ? accent : white;
                    ctx.beginPath(); ctx.arc(sx, sy, r, 0, Math.PI * 2); ctx.fill();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_matrix') {
                const t0 = performance.now();
                const draw = (now) => {
                  ctx.clearRect(0, 0, W, H);
                  const t = (now - t0) / 360;
                  for (let x = 22; x < W; x += 22) {
                    for (let y = 20; y < H; y += 22) {
                      const s = Math.sin(t + x * 0.03 + y * 0.03);
                      const r = 1.5 + (s + 1) * 1.4;
                      ctx.fillStyle = s > 0.45 ? accent : '#4b5563';
                      ctx.beginPath(); ctx.arc(x, y + s * 4, r, 0, Math.PI * 2); ctx.fill();
                    }
                  }
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_vortex') {
                const orb = Array.from({ length: 28 }, (_, i) => ({ a: i * 0.45, r: 16 + (i % 7) * 9, s: 0.018 + (7 - (i % 7)) * 0.004 }));
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  ctx.fillStyle = accent; ctx.beginPath(); ctx.arc(W / 2, H / 2, 5, 0, Math.PI * 2); ctx.fill();
                  orb.forEach(o => {
                    o.a += o.s;
                    const x = W / 2 + Math.cos(o.a) * o.r * 1.35, y = H / 2 + Math.sin(o.a) * o.r * 0.75;
                    ctx.fillStyle = o.r < 42 ? accent : white;
                    ctx.beginPath(); ctx.arc(x, y, 2.1, 0, Math.PI * 2); ctx.fill();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_ember') {
                const sp = Array.from({ length: 26 }, () => ({ x: Math.random() * W, y: Math.random() * H, vy: 0.6 + Math.random() * 1.4, r: 1.5 + Math.random() * 2 }));
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  sp.forEach(p => {
                    p.y -= p.vy; p.x += Math.sin(p.y * 0.05) * 0.5;
                    if (p.y < 0) { p.y = H; p.x = Math.random() * W; }
                    ctx.fillStyle = `rgba(255,94,26,${(p.y / H).toFixed(2)})`;
                    ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.fill();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_repel') {
                const dots = [];
                for (let x = 24; x < W; x += 24) for (let y = 20; y < H; y += 22) dots.push({ ox: x, oy: y, x, y });
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  dots.forEach(d => {
                    const dx = d.ox - mx, dy = d.oy - my, dist = Math.hypot(dx, dy);
                    if (dist < 56 && dist > 0.1) {
                      const f = (1 - dist / 56) * 20;
                      d.x += (d.ox + (dx / dist) * f - d.x) * 0.25;
                      d.y += (d.oy + (dy / dist) * f - d.y) * 0.25;
                    } else {
                      d.x += (d.ox - d.x) * 0.14; d.y += (d.oy - d.y) * 0.14;
                    }
                    ctx.fillStyle = dist < 56 ? accent : '#6b7280';
                    ctx.beginPath(); ctx.arc(d.x, d.y, 2.2, 0, Math.PI * 2); ctx.fill();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_ribbon') {
                const t0 = performance.now();
                const draw = (now) => {
                  ctx.clearRect(0, 0, W, H);
                  const t = (now - t0) / 500;
                  [0, 1, 2].forEach(l => {
                    ctx.strokeStyle = l === 1 ? accent : 'rgba(243,244,246,0.28)';
                    ctx.lineWidth = l === 1 ? 2.2 : 1.2;
                    ctx.beginPath();
                    for (let x = 0; x <= W; x += 8) {
                      const y = H / 2 + Math.sin(x * 0.025 + t + l * 1.1) * (22 + l * 8);
                      if (x === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
                    }
                    ctx.stroke();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_squares') {
                const sq = Array.from({ length: 14 }, (_, i) => ({ x: Math.random() * W, y: Math.random() * H, s: 10 + (i % 3) * 6, a: Math.random() * 6, vx: 0.45 + (i % 3) * 0.2 }));
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  sq.forEach((q, idx) => {
                    q.x = (q.x + q.vx) % (W + 20); q.a += 0.02;
                    ctx.save(); ctx.translate(q.x, q.y); ctx.rotate(q.a);
                    ctx.strokeStyle = idx % 3 === 0 ? accent : '#4b5563'; ctx.lineWidth = 1.6;
                    ctx.strokeRect(-q.s / 2, -q.s / 2, q.s, q.s);
                    ctx.restore();
                  });
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else if (fx.mode === 'v_sonar') {
                let ang = 0;
                const draw = () => {
                  ctx.clearRect(0, 0, W, H);
                  const cx = W / 2, cy = H / 2;
                  [22, 44, 66].forEach(r => {
                    ctx.strokeStyle = '#252934'; ctx.beginPath(); ctx.arc(cx, cy, r, 0, Math.PI * 2); ctx.stroke();
                  });
                  ang = (ang + 0.04) % (Math.PI * 2);
                  ctx.strokeStyle = accent; ctx.lineWidth = 2;
                  ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(cx + Math.cos(ang) * 66, cy + Math.sin(ang) * 66); ctx.stroke();
                  ctx.fillStyle = accent; ctx.beginPath(); ctx.arc(cx + 32, cy - 18, 3.5, 0, Math.PI * 2); ctx.fill();
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              } else { // v_spec
                const t0 = performance.now();
                const draw = (now) => {
                  ctx.clearRect(0, 0, W, H);
                  const t = (now - t0) / 180;
                  for (let i = 0; i < 18; i++) {
                    const h = 16 + Math.abs(Math.sin(t + i * 0.55)) * 96;
                    ctx.fillStyle = h > 84 ? accent : white;
                    ctx.fillRect(28 + i * 15, H / 2 - h / 2, 8, h);
                  }
                  regRaf(requestAnimationFrame(draw));
                };
                regRaf(requestAnimationFrame(draw));
              }
              break;
            }
      }
      return () => {
        timers.forEach(clearTimeout);
        rafs.forEach(cancelAnimationFrame);
        anims.forEach(a => { try { a.cancel(); } catch (_) {} });
        listeners.forEach(([el, ev, fn]) => el.removeEventListener(ev, fn));
      };
    }
    
    const MS = 0, PAUSE = 1500; // MS = 0: the effect already runs continuously
    const host = document.getElementById('stage');
    const still = matchMedia('(prefers-reduced-motion: reduce)').matches;
    let stop = null, timer = null, shown = false;
    function run() { if (stop) stop(); host.innerHTML = ""; stop = play(host); }
    function loop() { run(); if (!still && MS) 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>

قوالب مشابهة