Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <style>
    canvas {
      border: 1px solid #ddd;
    }
  </style>
  <title>Audio Dithering Widget</title>
</head>
<body>
  <div>
    <label for="amplitude">Amplitude:</label>
    <input type="range" id="amplitude" min="0" max="1" step="0.01" value="1">
  </div>
  <div>
    <label>
      <input type="checkbox" id="show-sine" checked> Sine-Wave (rounded)
    </label>
    <label>
      <input type="checkbox" id="show-dither"> Dither Added
    </label>
    <label>
      <input type="checkbox" id="show-dither-rounded"> Dither + Rounded
    </label>
  </div>
  <canvas id="myChart" width="400" height="200"></canvas>

  <script>
    // Initialize Chart.js
    var ctx = document.getElementById('myChart').getContext('2d');
    var chart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: Array.from({ length: 100 }, (_, i) => i + 1),
        datasets: [
          {
            label: 'Sine-Wave (rounded)',
            borderColor: 'rgba(75, 192, 192, 1)',
            data: generateSineWave(1),
            hidden: false
          },
          {
            label: 'Dither Added',
            borderColor: 'rgba(255, 99, 132, 1)',
            data: generateDitheredWave(1),
            hidden: true
          },
          {
            label: 'Dither + Rounded',
            borderColor: 'rgba(255, 255, 0, 1)',
            data: generateDitherAndRoundedWave(1),
            hidden: true
          }
        ]
      },
      options: {
        scales: {
          y: {
            min: -1,
            max: 1
          }
        }
      }
    });

    // Event listener for amplitude change
    document.getElementById('amplitude').addEventListener('input', function () {
      var amplitude = parseFloat(this.value);
      chart.data.datasets[0].data = generateSineWave(amplitude);
      chart.data.datasets[1].data = generateDitheredWave(amplitude);
      chart.data.datasets[2].data = generateDitherAndRoundedWave(amplitude);
      chart.update();
    });

    // Event listener for checkbox changes
    ['show-sine', 'show-dither', 'show-dither-rounded'].forEach(function (id) {
      document.getElementById(id).addEventListener('change', function () {
        var index = id === 'show-sine' ? 0 : id === 'show-dither' ? 1 : 2;
        chart.getDatasetMeta(index).hidden = !this.checked;
        chart.update();
      });
    });

    // Helper function to generate sine wave data
    function generateSineWave(amplitude) {
      return Array.from({ length: 100 }, (_, i) => Math.sin((i + 1) * (Math.PI / 50)) * amplitude);
    }

    // Helper function to generate dithered wave data
    function generateDitheredWave(amplitude) {
      return generateSineWave(amplitude).map(value => value + (Math.random() - 0.5) * 0.2);
    }

    // Helper function to generate dithered and rounded wave data
    function generateDitherAndRoundedWave(amplitude) {
      return generateDitheredWave(amplitude).map(value => Math.round(value * 10) / 10);
    }
  </script>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console