Canvas Waveform generator for SoundSky https://soundskycloud.github.io/canwave/
  • JavaScript 59.1%
  • HTML 40.9%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-05-19 20:47:39 +02:00
.github/workflows Static Pages 2025-05-19 20:38:19 +02:00
lib Update and rename waveform.ts to waveform.js 2025-05-19 18:41:12 +02:00
example.html Create example.html 2025-05-19 20:45:10 +02:00
index.html Example 2025-05-19 20:34:49 +02:00
LICENSE Create LICENSE 2025-05-19 20:37:08 +02:00
README.md Update README.md 2025-05-19 20:47:39 +02:00

image

Canvas Waveform Library

A lightweight, customizable library for generating audio waveform visualizations using HTML Canvas

Features

  • Customizable colors, bar width, and spacing
  • Vanilla JS functions for non-React projects

Usage Example

 // Example 1: Basic waveform in container
    const basicWaveform = SoundSkyWaveform.createWaveformInContainer({
      container: '#basic-waveform',
      backgroundColor: '#0f172a',
      waveColor: 'rgba(255, 255, 255, 0.7)'
    });
    
    document.getElementById('regenerate-basic').addEventListener('click', function() {
      basicWaveform.regenerate();
    });
    
    // Example 2: Customizable waveform
    const customWaveform = SoundSkyWaveform.createWaveformInContainer({
      container: '#custom-waveform',
      backgroundColor: '#0f172a',
      waveColor: 'rgba(255, 255, 255, 0.7)',
      barWidth: 2,
      gap: 3
    });
    
    document.getElementById('regenerate-custom').addEventListener('click', function() {
      customWaveform.regenerate();
    });
    
    document.getElementById('apply-colors').addEventListener('click', function() {
      const bgColor = document.getElementById('bg-color').value;
      const waveColor = document.getElementById('wave-color').value;
      
      customWaveform.updateOptions({
        backgroundColor: bgColor,
        waveColor: waveColor
      });
    });
    
    // Example 3: Using existing canvas
    const existingCanvas = document.getElementById('existing-canvas');
    const existingWaveform = SoundSkyWaveform.createWaveform({
      canvas: existingCanvas,
      backgroundColor: '#1e293b',
      waveColor: 'rgba(59, 130, 246, 0.7)',
      barWidth: 4,
      gap: 2
    });
    
    document.getElementById('regenerate-existing').addEventListener('click', function() {
      existingWaveform.regenerate();
    });
    
    // Example 4: Single waveform example
    console.log("SoundSkyWaveform loaded:", SoundSkyWaveform !== undefined);
    
    const waveform = SoundSkyWaveform.createWaveformInContainer({
      container: '#waveform',
      backgroundColor: '#0f172a',
      waveColor: 'rgba(255, 255, 255, 0.7)',
      barWidth: 3,
      gap: 4
    });
    
    document.getElementById('regenerate').addEventListener('click', function() {
      waveform.regenerate();
    });

API Reference

WaveformOptions

Option Type Default Description
backgroundColor string "#0f172a" Background color of the canvas
waveColor string "rgba(255, 255, 255, 0.7)" Color of the waveform bars
barWidth number 3 Width of each bar in pixels
gap number 4 Gap between bars in pixels
minHeight number 5 Minimum height of bars (percentage)
maxHeight number 70 Maximum height of bars (percentage)
mirror boolean true Whether to mirror the waveform vertically
randomize boolean true Whether to randomize bar heights
customHeights number[] [] Custom array of heights to use instead of random generation