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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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.

HTML

            
              <canvas></canvas>

<script id="fragment-shader" type="x-shader/x-fragment">
#ifdef GL_ES
  precision mediump float;
#endif

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

float lessCoord = min( u_resolution.x, u_resolution.y );
vec2 resRatio = u_resolution / vec2( lessCoord );

void main() {
  vec2 grad = abs( ( gl_FragCoord.xy  - u_mouse ) / u_resolution * resRatio );
  // could do something fun with u_time
  // float theta = sin( u_time ) * 0.5 + 0.5;
  float screenX = gl_FragCoord.x / u_resolution.x;
  gl_FragColor = vec4( grad.x, grad.y, screenX, 1.0 );
}
</script>

<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec2 a_position;

void main() {
  gl_Position = vec4( a_position, 0, 1 );
}
</script>

            
          
!

CSS

            
              html, body {
  padding: 0px;
  margin: 0px;
  overflow: hidden;
}

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

            
          
!

JS

            
              var canvas = document.querySelector('canvas');
var gl = canvas.getContext('webgl');
var width, height;
var mouseX = 0;
var mouseY = 0;
var startTime = new Date().getTime(); // Get start time for animating

// ----- Uniform ----- //

function Uniform( name, suffix ) {
  this.name = name;
  this.suffix = suffix;
  this.location = gl.getUniformLocation( program, name );
}

Uniform.prototype.set = function( ...values ) {
  var method = 'uniform' + this.suffix;
  var args = [ this.location ].concat( values );
  gl[ method ].apply( gl, args );
};

// ----- Rect ----- //

function Rect( gl ) {
  var buffer = gl.createBuffer();
  gl.bindBuffer( gl.ARRAY_BUFFER, buffer );
  gl.bufferData( gl.ARRAY_BUFFER, Rect.verts, gl.STATIC_DRAW );
}

Rect.verts = new Float32Array([
  -1, -1,
   1, -1,
  -1,  1,
   1,  1,
]);

Rect.prototype.render = function( gl ) {
  gl.drawArrays( gl.TRIANGLE_STRIP, 0, 4 );
};

// ----- init WebGL ----- //

// create program
var program = gl.createProgram();
// add shaders
var vertexShaderSource = document.querySelector('#vertex-shader').text;
var fragmentShaderSource = document.querySelector('#fragment-shader').text;
addShader( vertexShaderSource, gl.VERTEX_SHADER );
addShader( fragmentShaderSource, gl.FRAGMENT_SHADER );
// link & use program
gl.linkProgram( program );
gl.useProgram( program );

// create fragment uniforms
var uResolution = new Uniform( 'u_resolution', '2f' );
var uMouse = new Uniform( 'u_mouse', '2f' );
var uTime = new Uniform( 'u_time', '1f' );
// create position attrib
var billboard = new Rect( gl );
var positionLocation = gl.getAttribLocation( program, 'a_position' );
gl.enableVertexAttribArray( positionLocation );
gl.vertexAttribPointer( positionLocation, 2, gl.FLOAT, false, 0, 0 );

resize();
animate();

// ----- addShader ----- //

function addShader( source, type ) {
  var shader = gl.createShader( type );
  gl.shaderSource( shader, source );
  gl.compileShader( shader );
  var isCompiled = gl.getShaderParameter( shader, gl.COMPILE_STATUS );
  if ( !isCompiled ) {
    throw new Error( 'Shader compile error: ' + gl.getShaderInfoLog( shader ) );
  }
  gl.attachShader( program, shader );
}

// ----- render ----- //

function animate() {
  // update
  var now = new Date().getTime();
  var currentTime = ( now - startTime ) / 1000;
  uTime.set( currentTime );
  uMouse.set( mouseX, mouseY );
  // render
  billboard.render( gl );
  // animate next frame
  requestAnimationFrame( animate );
}

// ----- resize ----- //

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

window.addEventListener( 'resize', resize );

// ----- mouse move ----- //

document.addEventListener( 'mousemove', function( event ) {
  mouseX = event.pageX;
  mouseY = height - event.pageY;
});

            
          
!
999px

Console