Pen Settings



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


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


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.


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.


                <div id="container"><canvas id="canvas"></canvas></div>


                body {
  margin: 0;
  overflow: hidden;

canvas {
  display: block;
  width: 100%;

#container {
  height: calc( 100vh );


                // most relevant part
const canvas = document.getElementById('canvas'),
      ctx = canvas.getContext('2d'),
      container = document.getElementById('container'); 

const settings = {
  sampleRate: 44100,
  length: 3,
  generateData: generateData,
  download: doIt,
  fileName: 'generatedFile'
const wavToExport = new wavefile.WaveFile(),
      array = [],
      gui = new dat.GUI();
gui.add(settings, 'length', 0, 10).name('Audio data length (seconds)');
gui.add(settings, 'sampleRate', 8000, 96000, 1).name('Sample rate');
gui.add(settings, 'generateData').name('Generate results');
gui.add(settings, 'fileName').name('File name');
gui.add(settings, 'download').name('Export to file');

function resizeCanvas() {
  const scale = devicePixelRatio;
  canvas.width = container.clientWidth*scale;
  canvas.height = container.clientHeight*scale;
addEventListener('resize', resizeCanvas);

function exportToAudioFile(data, fileName) {
  const a = document.createElement('a'),
        blob = new Blob([data], {type: 'audio/wav'}),
        tempURL = URL.createObjectURL(blob);
  a.setAttribute('href', tempURL);
  a.setAttribute('download', `${fileName}.wav`);;

function generateData() {
  array.length = 0;
  const length = settings.length * settings.sampleRate;
  if (length >= 2 ** 31)
    throw 'The length is too large';
  for (let i = 0; i < length; i++) {
    const result = Math.cos(2*Math.PI*i/settings.sampleRate*1000); // placeholder
    array[i] = result;

function doIt() {
  if (array.length > 0) {
    // create an audio file (64-bit double-precision floating point is used to losslessly save array (not TypedArray) of numbers as an audio file, however, a 64-bit version of foobar2000 is required for playback for such files without precision loss)
    wavToExport.fromScratch(1, settings.sampleRate, '64', array);
    exportToAudioFile(wavToExport.toBuffer(), settings.fileName);

function redraw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for (let i = 0; i < array.length; i++) {
    ctx.lineTo(i*canvas.width/array.length, canvas.height/2-array[i]*canvas.height/2);