API reference

Everything you need, nothing you don’t.

Cheat sheet

Quick formulas that map to a visual mood.

EffectMath logicVisual feeling
Breathesin(t) * 0.5 + 0.5Smooth pulsing (0 to 1)
Glitchsin(t) + sin(t * 10) * 0.1Electric vibration
Orbitx: sin(t), y: cos(t)Perfect circular motion
Chaossin(t + sin(t))Organic drift

API reference

Minimal surface area, designed to stay predictable.

SineWaveGenerator Options

OptionTypeDefaultDescription
elstring | HTMLCanvasElementrequiredCanvas element or CSS selector
wavesWave[][]Array of wave configuration objects
pixelRationumberdevicePixelRatioDevice pixel ratio override
maxPixelRationumber2Maximum pixel ratio cap for memory/perf
autoResizebooleantrueAutomatically resize canvas on window resize

Wave Options

OptionTypeDefaultDescription
amplitudenumber10Wave height in pixels
wavelengthnumber100Distance between wave peaks in pixels
segmentLengthnumber10Rendering segment length (lower = smoother)
phasenumberrandomInitial phase offset (radians)
speednumberrandom 0.5-1.0Animation speed multiplier
easingfunctionEase.sineInOutEasing function for wave shape
rotatenumber0Rotation angle in degrees
strokeStylestring | nullnullCustom stroke color (CSS color or CanvasGradient)

Methods

MethodReturnsDescription
start()thisStart the animation loop (uses requestAnimationFrame)
stop()thisStop the animation loop and unbind events
resize()thisManually trigger canvas resize and gradient rebuild
addWave(config)thisAdd a wave dynamically at runtime
removeWave(index)thisRemove wave by index
setWaves(waves)thisReplace all waves in a single update
setQualityPreset(preset)thisApply a memory/performance preset: quality, balanced, battery
destroy()thisStop animation, unbind events, clear waves

Easing Functions

FunctionDescription
Ease.sineInOutSmooth sine curve (default)
Ease.sineInAccelerating sine curve
Ease.sineOutDecelerating sine curve
Ease.linearLinear (no easing)

Browser support

Works everywhere the Canvas API is available.

BrowserVersionStatus
Chrome / Edge60+Full support
Firefox55+Full support
Safari11+Full support
iOS Safari11+Full support
Samsung Internet8+Full support

Requires HTMLCanvasElement.getContext("2d") and requestAnimationFrame. No WebGL needed.

Framework guides

Use sine-wave-generator with any UI framework.

React

import { useEffect, useRef } from "react";

function Wave() {
  const ref = useRef(null);
  useEffect(() => {
    const gen = new SineWaveGenerator({
      el: ref.current,
      waves: [{ amplitude: 30, wavelength: 140 }],
    });
    gen.start();
    return () => gen.stop();
  }, []);
  return <canvas ref={ref} />;
}

Vue 3

<template>
  <canvas ref="canvas" />
</template>
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
const canvas = ref(null);
let gen;
onMounted(() => {
  gen = new SineWaveGenerator({
    el: canvas.value,
    waves: [{ amplitude: 30, wavelength: 140 }],
  });
  gen.start();
});
onUnmounted(() => gen?.stop());
</script>

Angular

@Component({
  template: `<canvas #wave></canvas>`
})
export class WaveComponent implements AfterViewInit, OnDestroy {
  @ViewChild("wave") canvas!: ElementRef;
  private gen?: SineWaveGenerator;

  ngAfterViewInit() {
    this.gen = new SineWaveGenerator({
      el: this.canvas.nativeElement,
      waves: [{ amplitude: 30, wavelength: 140 }],
    });
    this.gen.start();
  }
  ngOnDestroy() { this.gen?.stop(); }
}

Performance

Tune defaults for smoothness, with knobs for heavy scenes.

requestAnimationFrame

All rendering uses requestAnimationFrame for battery-efficient 60fps. The loop auto-pauses when the tab is hidden.

IntersectionObserver

Demos on this page use IntersectionObserver to start animations only when visible, saving CPU and battery.

prefers-reduced-motion

When prefers-reduced-motion: reduce is active, animations slow down to respect motion preferences.

Cap pixel ratio

Use maxPixelRatio to avoid oversized buffers on high-DPI screens.

Increase segment length

Larger segments reduce per-frame points while preserving visual flow.

Reduce wave count

More waves increases draw cost; balance layering with frame budget.

Important: If you set a high maxPixelRatio, memory use grows quickly on large canvases.

Memory grows with (width × height × maxPixelRatio²). For quick tuning, try setQualityPreset("quality"), setQualityPreset("balanced"), or setQualityPreset("battery").

Troubleshooting

Common questions and solutions.

Why is my canvas blurry on Retina screens?

The library handles DPI scaling automatically. If you see blur, ensure you are not setting width/height attributes on the canvas manually. Let the library manage sizing via resize(). You can also lower maxPixelRatio to trade sharpness for performance.

Why isn't the wave moving?

Make sure you call generator.start() after setup. Also check that speed is a positive number. If prefers-reduced-motion is enabled in your OS, animations will slow down to respect the setting.

How do I resize the canvas when the window changes?

Set autoResize: true (the default). For containers that resize independently of the window, call generator.resize() manually inside a ResizeObserver callback.

Can I use this with server-side rendering (SSR)?

The library requires the DOM and Canvas API. Guard your import with a typeof window !== "undefined" check or use dynamic imports in Next.js/Nuxt.

Does this library support WebGL?

No. The library uses the 2D Canvas API exclusively. This keeps the bundle small and avoids GPU driver issues. For most wave visualizations, 2D Canvas provides sufficient performance.