Pen Settings



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


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


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.


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.


                <canvas id="canvas" width="500" height="500"></canvas>




                const main = () => {
  // Find the canvas element
  const canvas = document.querySelector('#canvas');

  if (!(canvas instanceof HTMLCanvasElement)) {
    throw new Error('No html canvas element.');

  // WebGL rendering context
  const gl = canvas.getContext('webgl');

  if (!gl) {
    throw new Error('Unable to initialize WebGL.');

  // Clear color
  gl.clearColor(0, 0, 0, 0);

  // A user-defined function to create and compile shaders
  const initShader = (type: 'VERTEX_SHADER' | 'FRAGMENT_SHADER', source: string) => {
    const shader = gl.createShader(gl[type]);

    if (!shader) {
      throw new Error('Unable to create a shader.');

    gl.shaderSource(shader, source);


    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
      throw new Error(`An error occurred compiling the shaders: ${gl.getShaderInfoLog(shader)}`);

    return shader;

  // Vertex shader
  const vertexShader = initShader('VERTEX_SHADER', `
    attribute vec4 a_position;

    void main() {
      gl_Position = a_position;

  // Fragment shader
  const fragmentShader = initShader('FRAGMENT_SHADER', `
    void main() {
      gl_FragColor = vec4(0, 0, 0, 1);

  // WebGL program
  const program = gl.createProgram();

  if (!program) {
    throw new Error('Unable to create the program.');

  gl.attachShader(program, vertexShader);
  gl.attachShader(program, fragmentShader);


  if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
    throw new Error(`Unable to link the shaders: ${gl.getProgramInfoLog(program)}`);


  // Vertext buffer
  const positionBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

  const positions = [
    0, 1,
    0.866, -0.5,
    -0.866, -0.5,
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

  const index = gl.getAttribLocation(program, 'a_position');
  const size = 2;
  const type = gl.FLOAT;
  const normalized = false;
  const stride = 0;
  const offset = 0;
  gl.vertexAttribPointer(index, size, type, normalized, stride, offset);

  // Draw the scene
  gl.drawArrays(gl.TRIANGLES, 0, 3);
window.onload = main;