Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <audio id="soundElement"></audio>
<br>
<label for="audioInput" id="audioInputLabel">Choose a sound file. </label>
<input type="file" id="audioInput"/>
<p id="loadInput" onclick="loadDefaultSoundFile();">Play &nbsp;  LAST TRAIL &nbsp;  by Stan Williams.</p>
  
  

            
          
!
            
              html, body {
  overflow: hidden;
}

canvas {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  border: none;
  margin: 0;
  z-index: -1;
}

#audioInputLabel:hover {
  cursor: pointer;
  color: #aaa;
}

#audioInputLabel {
  font-family: Tahoma, Geneva, sans-serif;
  font-size: 15px;
  color: #fff;
  padding-left: 20px;
  text-decoration: underline;
}

#audioInput {
	width: 0.1px;
	height: 0.1px;
	opacity: 0;
	overflow: hidden;
	position: absolute;
	z-index: -1;
}

#loadInput {
  font-family: Tahoma, Geneva, sans-serif;
  font-size: 15px;
  color: #fff;
  padding-left: 20px;
  text-decoration: underline;
}

#loadInput:hover {
  cursor: pointer;
  color: #aaa;
}



            
          
!
            
              let presets = {
  "preset": "Do It",
  "remembered": {
    "DIY": {
      "0": {
        "smoothing": 0.8,
        "minDecibels": -100,
        "maxDecibels": -24.023333077627456,
        "numberOfBars": 256,
        "visualiserWidthRatio": 1,
        "barGapRatio": 1,
        "yOffsetRatio": 0,
        "barMaxHeightRatio": 0.7166855772025342,
        "barHue": 50,
        "barSat": 100,
        "barBrightness": 0,
        "barOpacity": 0.34516080916772385,
        "backHue": 50,
        "backSat": 100,
        "backBrightness": 100,
        "backOpacity": 0.1891139827381498,
        "textHue": 0,
        "textSat": 0,
        "textBrightness": 0,
        "textOpacity": 0.12223677141118951,
        "partHue": 0,
        "partSat": 0,
        "partBrightness": 0,
        "partOpacity": 0.935166610512731,
        "textIncluded": true,
        "text": "Make your own! 😃",
        "textXOffsetRatio": 0.11697700706805958,
        "textYOffsetRatio": 0.6670320495022679,
        "textSize": 128.06081538085886,
        "numParticles": 207.7370962505678,
        "baseSpeed": 2.5045767977087308,
        "bassBoost": 5.516615283155818,
        "angleDirection": 5.579915202085805,
        "angleVarience": 0.15018001192291322,
        "particleSize": 1.2944557043534721,
        "particleVarience": 4.9248189844192485
      }
    },
    "Do It": {
      "0": {
        "smoothing": 0.8,
        "minDecibels": -100,
        "maxDecibels": -25,
        "numberOfBars": 64,
        "visualiserWidthRatio": 0.85,
        "barGapRatio": 0.75,
        "yOffsetRatio": 0.5,
        "barMaxHeightRatio": 0.4,
        "barHue": 320,
        "barSat": 100,
        "barBrightness": 100,
        "barOpacity": 1,
        "backHue": 0,
        "backSat": 0,
        "backBrightness": 6,
        "backOpacity": 1,
        "textHue": 0,
        "textSat": 0,
        "textBrightness": 100,
        "textOpacity": 1,
        "partHue": 0,
        "partSat": 0,
        "partBrightness": 100,
        "partOpacity": 1,
        "textIncluded": true,
        "text": "Music Visualiser",
        "textXOffsetRatio": 0.0678,
        "textYOffsetRatio": 0.3,
        "textSize": 136,
        "numParticles": 250,
        "baseSpeed": 1,
        "bassBoost": 3.5,
        "angleDirection": 0,
        "angleVarience": 0.25,
        "particleSize": 2,
        "particleVarience": 4
      }
    },
    "Toothless Hawkins": {
      "0": {
        "smoothing": 0.701502814566924,
        "minDecibels": -100,
        "maxDecibels": 0,
        "numberOfBars": 200,
        "visualiserWidthRatio": 0.85,
        "barGapRatio": 0.270992418949638,
        "yOffsetRatio": 0.5,
        "barMaxHeightRatio": 0.3590012257391113,
        "barHue": 135,
        "barSat": 100,
        "barBrightness": 100,
        "barOpacity": 1,
        "backHue": 0,
        "backSat": 0,
        "backBrightness": 2,
        "backOpacity": 1,
        "textHue": 0,
        "textSat": 0,
        "textBrightness": 100,
        "textOpacity": 1,
        "partHue": 0,
        "partSat": 0,
        "partBrightness": 100,
        "partOpacity": 1,
        "textIncluded": true,
        "text": "TOOTHLESS HAWKINS",
        "textXOffsetRatio": 0.07,
        "textYOffsetRatio": 0.33699902404174287,
        "textSize": 119,
        "numParticles": 0,
        "baseSpeed": 1,
        "bassBoost": 1,
        "angleDirection": 0,
        "angleVarience": 0.5,
        "particleSize": 2,
        "particleVarience": 4
      }
    },
    "Renegade": {
      "0": {
        "smoothing": 0.8,
        "minDecibels": -100,
        "maxDecibels": -29.849718543307603,
        "numberOfBars": 66.93344783860665,
        "visualiserWidthRatio": -1,
        "barGapRatio": 0.9,
        "yOffsetRatio": 0.8775204281458707,
        "barMaxHeightRatio": -0.8,
        "barHue": 100,
        "barSat": 100,
        "barBrightness": 100,
        "barOpacity": 1,
        "backHue": 0,
        "backSat": 0,
        "backBrightness": 6,
        "backOpacity": 0.3287930534041678,
        "textHue": 100,
        "textSat": 0,
        "textBrightness": 6.008620205119812,
        "textOpacity": 1,
        "partHue": 100,
        "partSat": 80,
        "partBrightness": 100,
        "partOpacity": 1.523792862918443,
        "textIncluded": true,
        "text": "RENEGADE - NOISESTORM",
        "textXOffsetRatio": 0.0125,
        "textYOffsetRatio": 0.725,
        "textSize": 113,
        "numParticles": 160,
        "baseSpeed": 2.0645327637613637,
        "bassBoost": 5.034829992906089,
        "angleDirection": 4.819959155458702,
        "angleVarience": 0.2682852645075171,
        "particleSize": 10,
        "particleVarience": 4
      }
    },
    "Too Simple": {
      "0": {
        "smoothing": 0.8,
        "minDecibels": -100,
        "maxDecibels": -25.44927820383394,
        "numberOfBars": 64,
        "visualiserWidthRatio": 0.85,
        "barGapRatio": 0.8,
        "yOffsetRatio": 0.2,
        "barMaxHeightRatio": 0.6,
        "barHue": 300,
        "barSat": 100,
        "barBrightness": 100,
        "barOpacity": 1,
        "backHue": 299.74262388875496,
        "backSat": 100,
        "backBrightness": 50,
        "backOpacity": 0.38477364743603654,
        "textHue": 300,
        "textSat": 100,
        "textBrightness": 50,
        "textOpacity": 1,
        "partHue": 300,
        "partSat": 70,
        "partBrightness": 100,
        "partOpacity": 2.6322086247494427,
        "textIncluded": true,
        "text": "Too Simple",
        "textXOffsetRatio": 0.08,
        "textYOffsetRatio": 0.21,
        "textSize": 108,
        "numParticles": 207.7370962505678,
        "baseSpeed": 1,
        "bassBoost": 5,
        "angleDirection": 6.28,
        "angleVarience": 0.16465489451291224,
        "particleSize": 1.4044667128403137,
        "particleVarience": 6.134940077774507
      }
    }
  },
  "closed": false,
  "folders": {
    "Processing": {
      "preset": "Default",
      "closed": true,
      "folders": {}
    },
    "Bars": {
      "preset": "Default",
      "closed": true,
      "folders": {}
    },
    "Colours": {
      "preset": "Default",
      "closed": true,
      "folders": {
        "Bar Colour": {
          "preset": "Default",
          "closed": true,
          "folders": {}
        },
        "Background Colour": {
          "preset": "Default",
          "closed": true,
          "folders": {}
        },
        "Text Colour": {
          "preset": "Default",
          "closed": true,
          "folders": {}
        },
        "Particle Colour": {
          "preset": "Default",
          "closed": true,
          "folders": {}
        }
      }
    },
    "Text": {
      "preset": "Default",
      "closed": true,
      "folders": {}
    },
    "Particles": {
      "preset": "Default",
      "closed": true,
      "folders": {}
    }
  }
}

let visualiser = {
  numberOfBars: 64,
  smoothing: 0.8,
  minDecibels: -100,
  maxDecibels: 0,
  
  visualiserWidthRatio: 0.85,
  barGapRatio: 0.75,
  yOffsetRatio: 0.5,
  barMaxHeightRatio: 0.4,
  
  barHue: 320,
  barSat: 100,
  barBrightness: 100,
  barOpacity: 1,
  backHue: 0,
  backSat: 0,
  backBrightness: 6,
  backOpacity: 1,
  textHue: 0,
  textSat: 0,
  textBrightness: 100,
  textOpacity: 1,
  partHue: 0,
  partSat: 0,
  partBrightness: 100,
  partOpacity: 1,
  
  textIncluded: true,
  text: "DO IT - ROOTKIT",
  textXOffsetRatio: 0.0678,
  textYOffsetRatio: 0.3,
  textSize: 163,
  
  numParticles: 250,
  baseSpeed: 1,
  bassBoost: 1,
  angleDirection: 0,
  angleVarience: 0.5,
  particleSize: 2,
  particleVarience: 4,
}

let gui = new dat.GUI({
  load: presets,
  preset: 'Do It'
});
gui.remember(visualiser);

let f1 = gui.addFolder('Processing');
let f2 = gui.addFolder('Bars');
let f3 = gui.addFolder('Colours');
let f4 = gui.addFolder('Text');
let f5 = gui.addFolder('Particles');

let smoothingController = f1.add(visualiser,'smoothing',0,1).name("Smoothing");
let minDecibelsController = f1.add(visualiser,'minDecibels',-100,0).name("minDecibels");
let maxDecibelsController = f1.add(visualiser,'maxDecibels',-100,0).name("maxDecibels");

let numberOfBarsController = f2.add(visualiser,'numberOfBars',0,256).name("Number of Bars");
f2.add(visualiser,'visualiserWidthRatio',-1,1).name("Visualiser Width");
f2.add(visualiser,'barGapRatio',-1,1).name("Gap Width");
f2.add(visualiser,'yOffsetRatio',0,1).name("Y Axis Position");
f2.add(visualiser,'barMaxHeightRatio',-1,1).name("Height of Bars");

let f31 = f3.addFolder('Bar Colour');
f31.add(visualiser, 'barHue',0,360).name("Hue");
f31.add(visualiser, 'barSat', 0, 100).name("Saturation");
f31.add(visualiser, 'barBrightness', 0, 100).name("Brightness");
f31.add(visualiser, 'barOpacity', 0, 1).name("Opacity");

let f32 = f3.addFolder('Background Colour');
f32.add(visualiser, 'backHue',0,360).name("Hue");
f32.add(visualiser, 'backSat', 0, 100).name("Saturation");
f32.add(visualiser, 'backBrightness', 0, 100).name("Brightness");
f32.add(visualiser, 'backOpacity', 0, 1).name("Opacity");

let f33 = f3.addFolder('Text Colour');
f33.add(visualiser, 'textHue', 0, 360).name("Hue");
f33.add(visualiser, 'textSat', 0, 100).name("Saturation");
f33.add(visualiser, 'textBrightness', 0, 100).name("Brightness");
f33.add(visualiser, 'textOpacity', 0, 1).name("Opacity");

let f34 = f3.addFolder('Particle Colour');
f34.add(visualiser, 'partHue', 0, 360).name("Hue");
f34.add(visualiser, 'partSat', 0, 100).name("Saturation");
f34.add(visualiser, 'partBrightness', 0, 100).name("Brightness");
f34.add(visualiser, 'partOpacity', 0, 3).name("Opacity");

f4.add(visualiser,'textIncluded').name("Include Text");
f4.add(visualiser,'text').name("Text");
f4.add(visualiser, 'textXOffsetRatio', -1, 1).name("X Axis Position");
f4.add(visualiser, 'textYOffsetRatio', -1, 1).name("Y Axis Position");
f4.add(visualiser, 'textSize', 1, 600).name("Text Size");

let numOfParticlesController = f5.add(visualiser,'numParticles',0,500).name("Number");
f5.add(visualiser,'baseSpeed',0,10).name("Base Speed");
f5.add(visualiser,'bassBoost',0,10).name("Treble Boost");
let angleDirectionController = f5.add(visualiser,'angleDirection',0,6.28).name("Direction");
let angleVarienceController = f5.add(visualiser,'angleVarience',0,3.14).name("Varience");
let particleSizeController = f5.add(visualiser,'particleSize',0,10).name("Size");
let particleSizeVarienceController = f5.add(visualiser,'particleVarience',0,10).name("Varience");

gui.close();


let mV = new MusicVisualiser(visualiser.numberOfBars,"soundElement");

//Declaring the controllers which trigger reset of the visualisers arrays.
numberOfBarsController.onChange(function(val) {
  mV.setNumberOfBars(val);
});
smoothingController.onChange(function(val) {
  mV.setSmoothingTimeConstant(val);
});
minDecibelsController.onChange(function(val) {
  mV.analyser.minDecibels = val;
});
maxDecibelsController.onChange(function(val) {
  mV.analyser.maxDecibels = val;
});

numOfParticlesController.onChange(function(val) {
  particles = []
  for (let i = 0; i < visualiser.numParticles; i++){
    particles.push(new Particle());
  }
});

angleDirectionController.onChange(function(val) {
  for (let i = 0; i < visualiser.numParticles; i++){
    particles[i].updateDirection();
  }
});

angleVarienceController.onChange(function(val) {
  for (let i = 0; i < visualiser.numParticles; i++){
    particles[i].updateDirection();
  }
});

particleSizeController.onChange(function(val) {
  for (let i = 0; i < visualiser.numParticles; i++){
    particles[i].updateSize();
  }
});

particleSizeVarienceController.onChange(function(val) {
  for (let i = 0; i < visualiser.numParticles; i++){
    particles[i].updateSize();
  }
});

//Playing user selected audio files.
mV.setAudioFileInputElementID("audioInput");
audioInput.onchange = function() { 
  mV.loadUserSelectedSoundFile();
};

//Playing the default sound file.
function loadDefaultSoundFile() {
  mV.setAudioFileURL("https://archive.org/download/StanWilliamsLastTrail/StanWilliamsLastTrail.mp3");
  mV.loadDefaultSoundFile();
}

/* --- Particles --- */

function Particle() {
  //Pick co-ordinates limited to a box within the window (makes it look neater)
  this.y = random(windowHeight);
  this.x = random(windowWidth);
  
  this.size = visualiser.particleSize + random(visualiser.particleVarience);
  this.force = visualiser.baseSpeed;
  this.angle = map(random(1),0,1, visualiser.angleDirection - visualiser.angleVarience, visualiser.angleDirection + visualiser.angleVarience);
  this.opacity = 0.34 + random(0.66);
  
  this.updateDirection = function(){
    this.angle = map(random(1),0,1, visualiser.angleDirection - visualiser.angleVarience, visualiser.angleDirection + visualiser.angleVarience);
  }
  
  this.updateSize = function() {
    this.size = visualiser.particleSize + random(visualiser.particleVarience);
  }
  
  this.move = function() {
    
    this.force = visualiser.baseSpeed + mV.getTreble()*visualiser.bassBoost;
    
    this.x += this.force * cos(this.angle);
    this.y += this.force * sin(this.angle);
    
    if (this.x > windowWidth + 10){ 
      this.x -= (windowWidth+20);
    }
    if (this.x < -10){ 
      this.x += windowWidth+20;
    }
    if (this.y > windowHeight + 10){ 
      this.y -= (windowHeight+20);
    }
    if (this.y < -10){ 
      this.y += windowHeight + 20;
    }
  }
  
  this.display = function(){
    fill(visualiser.partHue,visualiser.partSat,visualiser.partBrightness,visualiser.partOpacity * this.opacity);
    ellipse(this.x, this.y, this.size);
  }
}

/* --- ANIMATION --- */

let particles = [];

function setup(){
  createCanvas(windowWidth, windowHeight);
  rectMode(CORNERS);
  colorMode(HSB, 360, 100, 100, 1);
  //fill(visualiser.barColour[0], visualiser.barColour[1], visualiser.barColour[2],visualiser.barColour[3]);
  for (let i = 0; i < visualiser.numParticles; i++){
    particles.push(new Particle());
  }
}

let beatFadeFactor = 0;

function draw(){
  mV.updateFrequencyData();
  
  drawBackground();
  drawBars();
  if (visualiser.textIncluded){
    drawText();    
  }
}

function drawBackground(){

  background(visualiser.backHue,visualiser.backSat,visualiser.backBrightness,visualiser.backOpacity);
    
  fill(visualiser.partHue,visualiser.partSat,visualiser.partBrightness,visualiser.partOpacity);
  
  for (let i = 0; i < visualiser.numParticles; i++){
    particles[i].move();
    particles[i].display();
  }
  /*
  if (mV.beat){
    beatFadeFactor = 100;
  } else {
    if (beatFadeFactor > 0)
      beatFadeFactor -= 2;
  }
  */
}

function drawBars(){
  noStroke();
  fill(visualiser.barHue,visualiser.barSat,visualiser.barBrightness,visualiser.barOpacity);
  
  let visualiserWidth = windowWidth * visualiser.visualiserWidthRatio;
  let barPlusGapWidth = visualiserWidth / mV.numberOfBars;
  let barWidth = barPlusGapWidth * visualiser.barGapRatio;
  
  let xStartingPoint = (windowWidth/2) - (visualiserWidth/2);
  let additiveXOffset = xStartingPoint;
  
  let barMaxHeight = windowHeight * visualiser.barMaxHeightRatio;
  let yOffset = windowHeight * (1-visualiser.yOffsetRatio) - barMaxHeight;
  
  for (let i = 0; i < mV.numberOfBars; i++){
    rect(
      additiveXOffset,                                              //bottom left corner x coordinate.
      yOffset + barMaxHeight,                                       //bottom left corner y coordinate.
      additiveXOffset + barWidth,                                   //top right corner x coordinate.
      yOffset + barMaxHeight - mV.barHeights[i] * barMaxHeight      //top right corner y coordinate.
    );    
    additiveXOffset += barPlusGapWidth;
  }
  
}

function drawText(){
  let textX = windowWidth * visualiser.textXOffsetRatio;
  let textY = windowHeight * (1-visualiser.textYOffsetRatio)
  textFont('Helvetica');
  textSize(visualiser.textSize);
  fill(visualiser.textHue, visualiser.textSat, visualiser.textBrightness, visualiser.textOpacity);
  textStyle(BOLD);
  text(visualiser.text, textX, textY);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console