Skip to contentCREATIVE·CODE·ART
1
12
26
Canvas JS

Wave Choir

Sine waves in layers, each one humming over the last. Plain Canvas JS, no library — the live example for the Canvas guide.

Licence: CC BY-NC-SA 4.0: remix with credit, non-commercial, share alike

View the code
canvas.width = 600;
canvas.height = 600;
let t = 0;
function frame() {
  ctx.fillStyle = "rgba(8, 10, 18, 0.22)";
  ctx.fillRect(0, 0, 600, 600);
  const lines = Math.floor(params.lines);
  for (let i = 0; i < lines; i++) {
    const f = lines === 1 ? 0.5 : i / (lines - 1);
    const baseY = 70 + f * 460;
    const hue = (185 + f * 130 + t * 12) % 360;
    ctx.beginPath();
    ctx.moveTo(0, 600);
    for (let x = 0; x <= 600; x += 6) {
      const wave =
        Math.sin(x * 0.012 + t * (1 + f) + i * 0.7) * params.height * 0.6 +
        Math.sin(x * 0.03 - t * 1.7 + i * 1.9) * params.height * 0.4;
      ctx.lineTo(x, baseY + wave);
    }
    ctx.lineTo(600, 600);
    ctx.closePath();
    ctx.fillStyle = "hsla(" + hue + ", 85%, 60%, 0.10)";
    ctx.fill();
    ctx.strokeStyle = "hsla(" + hue + ", 90%, 70%, 0.75)";
    ctx.lineWidth = 1.6;
    ctx.stroke();
  }
  t += 0.016 * params.speed;
  requestAnimationFrame(frame);
}
frame();