cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <script id="shader-fs" type="x-shader/x-fragment">

#define PI 3.14159265359
#define TWO_PI PI * 2.0

precision mediump float;

uniform float u_time;
uniform vec2 u_mouse;
uniform vec2 u_resolution;


void main(void) {
  float m = min(u_resolution.x, u_resolution.y);
  vec2 st = gl_FragCoord.xy / m;
  st = st * 2.0 - u_resolution / m;

  gl_FragColor = vec4(vec3(length(st)), 1.0);
}

</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 position;

void main(void) {
  gl_Position = vec4(position, 1.0);
}
</script>
<canvas id="canvas">
</canvas>
            
          
!
            
              var canvas;
var gl;

window.onload = function() {
  var fragmentShader, vertexShader;
  var shaderProgram;
  var mouse;
  var startTime;
  var positionAttribute;
  var timeUniformLocatoin, mouseUniformLocation, resolutionUniformLocation;
  var verticesBuffer;
  var vertices;

  canvas = document.getElementById("canvas");
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");

  if (!gl) {
    console.error('can not get context');
    return;
  }

  canvas.addEventListener('mousemove', function(e) {
    mouse = [e.offsetX, e.offsetY];
  });
  mouse = [0, 0];
  startTime = new Date().getTime();

  fragmentShader = getShader('shader-fs');
  vertexShader = getShader('shader-vs');
  shaderProgram = gl.createProgram();
  gl.attachShader(shaderProgram, vertexShader);
  gl.attachShader(shaderProgram, fragmentShader);
  gl.linkProgram(shaderProgram);

  if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
    console.error('can not initialize shader program');
    return;
  }

  gl.useProgram(shaderProgram);
  positionAttribute = gl.getAttribLocation(shaderProgram, 'position');
  gl.enableVertexAttribArray(positionAttribute);

  timeUniformLocatoin = gl.getUniformLocation(shaderProgram, 'u_time');
  mouseUniformLocation = gl.getUniformLocation(shaderProgram, 'u_mouse');
  resolutionUniformLocation = gl.getUniformLocation(shaderProgram, 'u_resolution');

  verticesBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
  vertices = [
    1.0, 1.0, 0.0,
    -1.0, 1.0, 0.0,
    1.0, -1.0, 0.0,
    -1.0, -1.0, 0.0
  ];
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

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

  function getShader(id) {
    var shaderScript, shader;

    shaderScript = document.getElementById(id);
    if(!shaderScript) {
      return null;
    }

    if (shaderScript.type == 'x-shader/x-fragment') {
      shader = gl.createShader(gl.FRAGMENT_SHADER);
    } else if (shaderScript.type == 'x-shader/x-vertex') {
      shader = gl.createShader(gl.VERTEX_SHADER);
    } else {
      return null;
    }

    gl.shaderSource(shader, shaderScript.text);
    gl.compileShader(shader);

    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
      console.error('can not comple shader source');
      console.error(gl.getShaderInfoLog(shader));
      return null;
    }

    return shader;
  }

  function render() {
    var time, resolution;

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

    time = (new Date().getTime() - startTime);
    resolution = [canvas.width, canvas.height];
    gl.uniform1f(timeUniformLocatoin, time);
    gl.uniform2fv(mouseUniformLocation, mouse);
    gl.uniform2fv(resolutionUniformLocation, resolution);

    gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
    gl.vertexAttribPointer(verticesBuffer, 3, gl.FLOAT, false, 0, 0);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
    gl.flush();
  }
}

window.onresize = function() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    gl.viewport(0, 0, canvas.width, canvas.height);
}

            
          
!
999px
Loading ..................

Console