Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="canvas" width="500" height="500" />

<script id="vertexShader" type="x-shader/x-vertex">
  #version 300 es

  in vec4 vertexPosition;

  void main() {
    gl_Position = vertexPosition;
  }
</script>

<script id="fragmentShader" type="x-shader/x-fragment">
  #version 300 es
  precision highp float;

  uniform vec2 canvasSize;
  uniform float time;
  out vec4 fragColor;

  void main() {
    vec2 coord = 2.*gl_FragCoord.xy/canvasSize.xy - 1.; // [-1,1]
    float scale = (sin(time) + 1.)/2.; // from 1 to 0
    coord /= scale; // from [-1,1] to [-infinity,infinity]
    if (abs(coord.x) < 1. && abs(coord.y) < 1.) {
      coord = (coord + 1.)/2.; // [0,1]
      fragColor = vec4(coord.x, coord.y, 1.-coord.x, 1);
    } else {
      fragColor = vec4(1,1,1,1);
    }
  }
</script>
              
            
!

CSS

              
                body {
  text-align: center;
}
canvas {
  max-width: 100vw; 
  max-height: 100vh;
}
              
            
!

JS

              
                // Requires a WebGL2-compatible browser: https://caniuse.com/?search=webgl2
// Confirm your compatibility: https://get.webgl.org/webgl2/
// Older versions of Safari (iOS/MacOS) may need to enable WebGL2 under "Experimental Features"

// More WebGL2 demos: https://codepen.io/collection/aMoEeq

const gl = canvas.getContext("webgl2");
if (!gl) throw "WebGL2 not supported";

function createShader(type, sourceCode) {
  const shader = gl.createShader(type);
  gl.shaderSource(shader, sourceCode.trim());
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    throw gl.getShaderInfoLog(shader);
  }
  return shader;
}

const program = gl.createProgram();
gl.attachShader(program, createShader(gl.VERTEX_SHADER, vertexShader.textContent));
gl.attachShader(program, createShader(gl.FRAGMENT_SHADER, fragmentShader.textContent));
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  throw gl.getProgramInfoLog(program);
}
gl.useProgram(program);

const vertices = [
  [-1, -1],
  [1, -1],
  [-1, 1],
  [1, 1],
];
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices.flat()), gl.STATIC_DRAW);

const vertexPosition = gl.getAttribLocation(program, "vertexPosition");
gl.enableVertexAttribArray(vertexPosition);
gl.vertexAttribPointer(vertexPosition, vertices[0].length, gl.FLOAT, false, 0, 0);

gl.uniform2f(
  gl.getUniformLocation(program, 'canvasSize'),
  canvas.width, canvas.height
);
const timeUniform = gl.getUniformLocation(program, 'time');

function draw() {
  gl.uniform1f(timeUniform, performance.now() / 1000.);
  gl.drawArrays(gl.TRIANGLE_STRIP, 0, vertices.length);
  requestAnimationFrame(draw);
}
draw();
              
            
!
999px

Console