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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

              
                <canvas id="c"></canvas>
<script id="shader-fs" type="x-shader/x-fragment">
				#ifdef GL_ES
				  precision highp float;
				  #endif

            //attribute vec2 a_position;
            varying   vec4 vColor;

    		void main(void) {
    		 gl_FragColor = vec4(vColor);
    		}

    </script>

    <script id="shader-vs" type="x-shader/x-vertex">
    //attribute vec3 vertexPosition;
    attribute vec2 a_position;
    attribute vec4 a_color;

    varying   vec4 vColor;

    uniform vec2 u_resolution;

   	void main(void) {
   	    vColor = a_color;
   	    vec2 curPosition = a_position + u_resolution/2.0;

  		vec2 zeroToOne = curPosition / u_resolution;
  		vec2 zeroToTwo = zeroToOne * 2.0;
  		vec2 clipSpace = zeroToTwo - 1.0;
  		gl_Position = vec4(clipSpace, 0, 1);
  	}

    </script>
              
            
!

CSS

              
                @import "compass/css3";

@import "compass/reset";
@import "compass/css3";

body{
  overflow: hidden;
}

canvas{
  width: 100%;
  height: 100%;
}
              
            
!

JS

              
                window.onload = loadScene;

var canvas, gl,
    ratio,
    vertices,
    velocities,
    freqArr,
    cw,
    ch,
    colorLoc,
    thetaArr,
    velThetaArr,
    velRadArr0,
    velRadArr1,
    boldRateArr,
    drawType,
    numLines = 20000;

var colorArr = [];
var colorVbo;

var target = [];
var type = [];

var randomTargetXArr = [], randomTargetYArr = [];
drawType = 2;


/**
 * Initialises WebGL and creates the 3D scene.
 */
function loadScene() {
    //    Get the canvas element
    canvas = document.getElementById("c");
    //    Get the WebGL context
    gl = canvas.getContext("experimental-webgl");
    //    Check whether the WebGL context is available or not
    //    if it's not available exit
    if (!gl) {
        alert("There's no WebGL context available.");
        return;
    }
    //    Set the viewport to the canvas width and height
    cw = window.innerWidth;
    ch = window.innerHeight;
    canvas.width = cw;
    canvas.height = ch;
    gl.viewport(0, 0, canvas.width, canvas.height);

    //    Load the vertex shader that's defined in a separate script
    //    block at the top of this page.
    //    More info about shaders: http://en.wikipedia.org/wiki/Shader_Model
    //    More info about GLSL: http://en.wikipedia.org/wiki/GLSL
    //    More info about vertex shaders: http://en.wikipedia.org/wiki/Vertex_shader

    //    Grab the script element
    var vertexShaderScript = document.getElementById("shader-vs");
    var vertexShader = gl.createShader(gl.VERTEX_SHADER);
    gl.shaderSource(vertexShader, vertexShaderScript.text);
    gl.compileShader(vertexShader);
    if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
        alert("Couldn't compile the vertex shader");
        gl.deleteShader(vertexShader);
        return;
    }

    //    Load the fragment shader that's defined in a separate script
    //    More info about fragment shaders: http://en.wikipedia.org/wiki/Fragment_shader
    var fragmentShaderScript = document.getElementById("shader-fs");
    var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
    gl.shaderSource(fragmentShader, fragmentShaderScript.text);
    gl.compileShader(fragmentShader);
    if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
        alert("Couldn't compile the fragment shader");
        gl.deleteShader(fragmentShader);
        return;
    }

    //    Create a shader program.
    gl.program = gl.createProgram();
    gl.attachShader(gl.program, vertexShader);
    gl.attachShader(gl.program, fragmentShader);
    gl.linkProgram(gl.program);
    if (!gl.getProgramParameter(gl.program, gl.LINK_STATUS)) {
        alert("Unable to initialise shaders");
        gl.deleteProgram(gl.program);
        gl.deleteProgram(vertexShader);
        gl.deleteProgram(fragmentShader);
        return;
    }
    gl.useProgram(gl.program);
    //var vertexPosition = gl.getAttribLocation(gl.program, "a_position");

    gl.clearColor(0.0, 0.0, 0.0, 1.0);
    gl.clearDepth(1.0);


    gl.enable(gl.BLEND);
    gl.disable(gl.DEPTH_TEST);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
;

    //color vbo



    // ------------------

    setup();

    // ------------------


    vertices = new Float32Array(vertices);
    velocities = new Float32Array(velocities);
    //console.log(c)
    colorArr = new Float32Array(colorArr);
    console.log(colorArr.length)

    /*
    thetaArr = new Float32Array(thetaArr);
    velThetaArr = new Float32Array(velThetaArr);
    velRadArr = new Float32Array(velRadArr);*/


    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    var colorVBO = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, colorVBO);
    gl.bufferData(gl.ARRAY_BUFFER, colorArr, gl.STATIC_DRAW);
    var colorPosAttrbLocation = gl.getAttribLocation(gl.program, "a_color");
    gl.enableVertexAttribArray(colorPosAttrbLocation);
    gl.vertexAttribPointer(colorPosAttrbLocation, 4.0, gl.FLOAT, false, 0, 0);


    var vertexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer)
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);
    var vertexPosAttribLocation = gl.getAttribLocation(gl.program, "a_position");
    gl.enableVertexAttribArray(vertexPosAttribLocation);
    gl.vertexAttribPointer(vertexPosAttribLocation, 2.0, gl.FLOAT, false, 0, 0);




    var resolutionLocation = gl.getUniformLocation( gl.program, "u_resolution" );
    gl.uniform2f( resolutionLocation, window.innerWidth, window.innerHeight );


    gl.enable(gl.BLEND);
    gl.disable(gl.DEPTH_TEST);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE);

    animate();
    //setTimeout(timer, 1500);
}
var count = 0;
var cn = 0;

function animate() {
    requestAnimationFrame(animate);
    drawScene();
}


function drawScene() {
    draw();

    gl.lineWidth(1);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);

    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.LINES, 0, numLines);



    gl.flush();
}

// ===================================

var centerRadX = 90;
var centerRadY = 120;
var centerTheta = 0;
var centerVelTheta = .03;

function setup() {

    vertices = [];
    velThetaArr = [];

    velRadArr0 = [];
    velRadArr1 = [];

    ratio = cw / ch;
    velocities = [];
    thetaArr = [];
    freqArr = [];
    boldRateArr = [];

    // -------------------------------

    for (var ii = 0; ii < numLines; ii++) {
        var rad, rad2;
        var random1 = Math.random();
        var random2 = Math.random();

        if(random1 < .33) rad = 10;
        else if(random1 < .66) rad = 60;
        else    rad = 120;

        if(random2 < .33) rad2 = 60;
        else if(random2 < .66) rad2 = 100;
        else    rad2 = 140;
      
        rad = 100 * Math.random() + 60;
        rad2 = 20 * Math.random() + 80;

        var theta = Math.random() * Math.PI * 2;

        var freq = Math.random() * 0.12 + 0.03;
        var boldRate = Math.random() * .04 + .01;
        var randomPosX = (Math.random() * 2  - 1) * window.innerWidth / window.innerHeight;
        var randomPosY = Math.random() * 2 - 1;

        vertices.push(rad * Math.cos(theta), rad * Math.sin(theta));
        vertices.push(rad * Math.cos(theta), rad * Math.sin(theta));

        thetaArr.push(theta);


        velRadArr0.push(rad);
        velRadArr1.push(rad2);

        freqArr.push(freq);
        boldRateArr.push(boldRate);


        randomTargetXArr.push(randomPosX);
        randomTargetYArr.push(randomPosY);

        var colRand = Math.random();
        var col;
        if(colRand < 1/3)       {
            type.push(0);
            col = [0.8, 0.0, 0.0];
            var velTheta = (Math.random() * .2 + .8) * Math.PI * 2 / 300;
        }
        else if(colRand < 2/3) {
            type.push(1);
            col = [0.0, 0.8, 0.0];
            var velTheta = (Math.random() * .2 + .8) * Math.PI * 2 / 100;
        }else {
            type.push(2);
            col = [0.0, 0.0, 0.8];
            var velTheta = (Math.random() * .2 + .8) * Math.PI * 2 / 200;
        }

        colorArr.push(col[0], col[1], col[2], 0.06 );
        colorArr.push(col[0], col[1], col[2], 0.06 );

        velThetaArr.push(velTheta);

    }


    freqArr = new Float32Array(freqArr);

}

// -------------------------------


// ===================================
// -------------------------------
var theta1 = 0, theta2 = 0;
var rCount = Math.PI/2;
function draw() {



    cn += .1;

    var i, n = vertices.length, p, bp;
    var px, py;
    var cenX, cenY;
    var pTheta;
    var rad;
    var num;
    rCount += 0.03;
    var rRad = 80 * Math.sin(rCount);
    var transX, transY;

    centerTheta += centerVelTheta;
    cenX = centerRadX * Math.cos(centerTheta);
    cenY = centerRadY * Math.sin(centerTheta);

    theta1 += .03;
    theta2 += .04;

    for (i = 0; i < numLines * 2; i += 2) {
        bp = i * 2;

        num = parseInt(i / 2);

        if(type[num] == 0){
            transX = rRad * Math.cos(0);
            transY = rRad * Math.sin(0);
        }else if(type[num] == 2){
            transX = rRad * Math.cos(120/180*Math.PI);
            transY = rRad * Math.sin(120/180*Math.PI);
        }else{
            transX = rRad * Math.cos(240/180*Math.PI);
            transY = rRad * Math.sin(240/180*Math.PI);
        }
        pTheta = thetaArr[num];

        pTheta = pTheta + velThetaArr[num];
        thetaArr[num] = pTheta;

        rad = velRadArr0[num];

        px = rad * Math.cos(pTheta - theta2) + transX;
        py = rad * Math.sin(pTheta - theta1) + transY;

        vertices[bp + 2] = px + cenX;
        vertices[bp + 3] = py + cenY;

        rad = velRadArr1[num];
        px = rad * Math.cos(pTheta + theta1 ) +transX;
        py = rad * Math.sin(pTheta + theta2 ) +transY;

        vertices[bp] = px;
        vertices[bp + 1] = py;

    }
}

// -------------------------------


function timer() {
    drawType = (drawType + 1) % 3;

    setTimeout(timer, 1500);
}

              
            
!
999px

Console