Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                <div id='animation'></div>

<div style='padding : 30px; -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; 
-khtml-border-radius: 10px;  background-color:rgba(0, 0, 0, 0.5); position : absolute; top : 20px; left : 20px;'>
  
	
  
	<button style='cursor : pointer; font-size : 20px; -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-khtml-border-radius: 10px;  border : 0px solid #F687B3; color : #fff; background-color: #F687B3; padding : 10px;' onClick='sketch.playRandom()'>Play Music</button>
  
	<h2 style='color : #fff'>Use sliders to remix the visual </h2>
  
	<div style='margin-bottom : 25px; width : 200px;' id='slider1'></div>
  
	<div style='margin-bottom : 25px; width : 200px;' id='slider2'></div>
  
	<div style='margin-bottom : 10px; width : 200px;' id='slider3'></div>
  
</div>

<div id='song'>
  
	<audio crossOrigin='anonymous' src='https://storage.googleapis.com/musicmation-83a98.appspot.com/changesTrimmed.mp3' id='songPlayer' loop></audio>
  
</div>
              
            
!

CSS

              
                body { margin : 0px !important; font-family : 'IBMPlexSans'}

@font-face { font-family: 'IBMPlexSans'; src: url('https://storage.googleapis.com/vizibop-fonts/IBMPlexSans-Regular.ttf')}

.noUi-base { background-color : #7F7F7F; }
              
            
!

JS

              
                var sketch

const s = p => {
    let x = 100;
    let y = 100;

    var onsetLow, onsetLowMid, onsetMid

    store = []
    store.slider1Value = 50;
    store.slider2Value = 50;
    store.slider3Value = 50;
    store.slider4Value = 50;
    store.backgroundColor = "#48639B";
    store.fillColor = "#F687B3";
    store.fontColor = "#7F7F7F";
    store.strokeColor = "#ffffff";
    var numOfSides = 6
    var ratio = 1.78125;
    var canvas;
    store.audioStarted = false
    store.beatAlgorithm = "1"


    store.slider1 = document.getElementById('slider1');
    noUiSlider.create(slider1, {
        start: 50,
        range: {
            'min': 0,
            'max': 100
        }
    });

    store.slider1.noUiSlider.on('update', function(value) {

        store.slider1Value = value




    })

    store.slider2 = document.getElementById('slider2');
    noUiSlider.create(slider2, {
        start: 50,
        range: {
            'min': 0,
            'max': 100
        }
    });

    store.slider2.noUiSlider.on('update', function(value) {

        store.slider2Value = value


    })


    store.slider3 = document.getElementById('slider3');
    noUiSlider.create(slider3, {
        start: 50,
        range: {
            'min': 0,
            'max': 100
        }
    });

    store.slider3.noUiSlider.on('update', function(value) {

        store.slider3Value = value


    })



    p.playRandom = function() {
      
      if ( store.audioStarted === false ) {

        store.audioContext = sketch.getAudioContext()
        store.source = store.audioContext.createMediaElementSource(document.getElementById('songPlayer'));
        
      }

        if (store.audioContext.state === 'suspended') {

            store.audioContext.resume()

        }

        store.amplitude = new p5.Amplitude();
        store.amplitude.smooth(0.9);
        store.amplitude.setInput(store.source)

        store.FFT = new p5.FFT();
        store.FFT.setInput(store.source)


        store.source.connect(store.audioContext.destination);




        store.audioElement = document.getElementById('songPlayer');

        store.audioElement.play();
      store.audioElement.loop = false;
        store.audioStarted = true
        //store.setupAudio(store.sketch)




    }


    p.isABeat = function(onsetLow, onsetLowMid, onsetMid) {


            store.spectrum = store.FFT.analyze();

            store.bassEnergy = store.FFT.getEnergy("bass")
            store.lowMidEnergy = store.FFT.getEnergy("lowMid")
            store.midEnergy = store.FFT.getEnergy("mid")
            store.highMidEnergy = store.FFT.getEnergy("highMid")
            store.trebleEnergy = store.FFT.getEnergy("treble")


            var isBeat = false


            onsetLow.update(store.FFT);
            onsetLowMid.update(store.FFT);
            onsetMid.update(store.FFT);


            switch (store.beatAlgorithm) {
                case "1":
                    if (store.lowCount === 1) {
                        isBeat = true
                    }
                    // code block
                    break;
                case "2":
                    if (store.lowMidCount === 1) {
                        isBeat = true
                    }
                    break;
                case "3":
                    if (store.midCount === 1) {
                        isBeat = true
                    }
                    // code block
                    break;
                case "4":
                    if (store.lowCount + store.lowMidCount === 2) {
                        isBeat = true
                    }
                    break;
                case "5":
                    if (store.lowCount + store.midCount === 2) {
                        isBeat = true
                    }
                    break;
                case "6":
                    if (store.lowMidCount + store.midCount === 2) {
                        isBeat = true
                    }
                    break;
                default:
                    // code block
            }


            return isBeat




        },


        p.setup = function() {



            p.rectMode(p.CORNER)



            p.background('#3BDBFF');


            onsetLow = new OnsetDetect(40, 120, "low", 0.025);
            onsetLowMid = new OnsetDetect(140, 400, "lowMid", 0.025);
            onsetMid = new OnsetDetect(400, 2600, "mid", 0.025);


            store.evenColor = store.backgroundColor



            //var canvas = p.createCanvas(250, 250);
            canvas = p.createCanvas(window.innerWidth, window.innerHeight);
            canvas.parent('animation');



            p.noStroke();



        }

    p.draw = function() {

        store.lowCount = 0
        store.lowMidCount = 0
        store.midCount = 0




        p.background(store.evenColor);



        if (store.audioStarted) {

            if (store.beatAlgorithm !== "0") {

                try {

                    if (p.isABeat(onsetLow, onsetLowMid, onsetMid)) {

                        if (store.evenBeat === true) {

                            store.evenBeat = false
                            store.evenColor = store.backgroundColor
                            store.oddColor = store.fillColor


                        } else {

                            store.evenBeat = true
                            store.evenColor = store.fillColor
                            store.oddColor = store.backgroundColor

                            //p.fill(store.fillColor)


                        }



                    }

                } catch (err) {


                    console.log(err)


                }

            }

        }

        var hexSize = p.map(store.slider1Value, 0, 100, 2, 300)
        var ratio2 = p.map(store.slider2Value, 0, 100, 5, .01)
        numOfSides = p.map(store.slider3Value, 0, 100, 2, 12)

        var size = hexSize;
        //var size = 64;
        //var offset = size * ratio;
        var offset = size * ratio2;

        for (var x = 0; x <= p.width + size; x += size * 2) {
            for (var y = 0; y <= p.height + offset; y += offset) {
                var x0 = 0;
                if (y % (offset * 2) == 0) {
                    p.fill(store.fontColor);
                    var x0 = size;
                } else {
                    p.fill(store.strokeColor);
                    var xo = 0;
                }

                p.makePoly(x + x0, y, size);
            }
        }



    }


    p.makePoly = function(a, b, size) {

        var diff =
            p.sin(p.radians(p.dist(a, b, p.width / 2, p.height / 2) - p.frameCount)) * size / ratio;
        p.beginShape();
        //for (var i = 0; i < 6; i++) {
        for (var i = 0; i < numOfSides; i++) {
            //var angle = p.PI * i / 3;
            var angle = p.PI * i / (numOfSides / 2);
            p.vertex(
                a + p.sin(angle) * (size / ratio - diff),
                b + p.cos(angle) * (size / ratio - diff)
            );
        }
        p.endShape(p.CLOSE);
    }


}



sketch = new p5(s);



function OnsetDetect(f1, f2, str, thresh) {
    this.isDetected = false;
    this.f1 = f1;
    this.f2 = f2;
    this.str = str;
    this.treshold = thresh;
    this.energy = 0;
    this.penergy = 0;
    this.siz = 10;
    this.sensitivity = 500;
}

OnsetDetect.prototype.display = function(x, y) {

    if (this.isDetected == true) {
        this.siz = store.p5.lerp(this.siz, 40, 0.99);
    } else if (this.isDetected == false) {
        this.siz = store.p5.lerp(this.siz, 15, 0.99);
    }
    store.p5.fill(255, 0, 0);
    store.p5.ellipse(x, y, this.siz, this.siz);
    store.p5.fill(0);
    //store.p5.text(this.str,x,y);
    //store.p5.text("( "+this.f1+" - "+this.f2+"Hz )",x,y+10);
}

OnsetDetect.prototype.update = function(fftObject) {
    this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;

    if (this.isDetected == false) {
        if (this.energy - this.penergy > this.treshold) {
            this.isDetected = true;



            if (this.str === 'low') {
                store.lowCount = 1
            }
            if (this.str === 'lowMid') {
                store.lowMidCount = 1
            }
            if (this.str === 'mid') {
                store.midCount = 1
            }

            var self = this;
            setTimeout(function() {
                self.isDetected = false;
            }, this.sensitivity);
        }
    }

    this.penergy = this.energy;

}


function BeatDetect(f1, f2, str, thresh) {
    this.isDetected = false;
    this.f1 = f1;
    this.f2 = f2;
    this.str = str;
    this.treshold = thresh;
    this.energy = 0;

    this.siz = 10;
    this.sensitivity = 500;
}

BeatDetect.prototype.display = function(x, y) {

    if (this.isDetected == true) {
        this.siz = store.p5.lerp(this.siz, 40, 0.99);
    } else if (this.isDetected == false) {
        this.siz = store.p5.lerp(this.siz, 15, 0.99);
    }
    store.p5.fill(255, 0, 0);
    store.p5.ellipse(x, y, this.siz, this.siz);
    store.p5.fill(0);
    //store.p5.text(this.str,x,y);
    //store.p5.text("( "+this.f1+" - "+this.f2+"Hz )",x,y+10);
}

BeatDetect.prototype.update = function(fftObject) {
    this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;

    if (this.isDetected == false) {
        if (this.energy > this.treshold) {
            this.isDetected = true;
            var self = this;
            setTimeout(function() {
                self.isDetected = false;
            }, this.sensitivity);
        }
    }
}
              
            
!
999px

Console