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 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

              
                <div id="controls">
  <label>
    <div>Scale</div>
    <input type="range" id="scale" min="-1" max="4" value="1" step="0.01" />
  </label>
  <label>
    <div>Rotate</div>
    <input type="range" id="rotate" min="0" max="360" value="0" step="1" />
  </label>
  <label>
    <div>TranslateX</div>
    <input type="range" id="translateX" min="-500" max="1000" value="0" step="1" />
  </label>
  <label>
    <div>TranslateY</div>
    <input type="range" id="translateY" min="-500" max="1000" value="0" step="1" />
  </label>
</div>

<canvas></canvas>

<!-- 
 Future todos:
   - Add some animated rotation/scale/transform via matrix mult.
   - Maybe find a way to center the star (without changing all the coordiantes)?
     - Option 1: some sort of translate?
     - Option 2: reduce canvas size and center the canvas? Maybe this would prevent pixelation on wider screens.
-->

<!-- 
 Resources I used:
  - https://webglfundamentals.org/
  - http://learnwebgl.brown37.net/rendering/render_example_02.html
-->
              
            
!

CSS

              
                body {
  margin: 0;
  background: black;
}

canvas {
  /* ensure a square canvas, defined by the viewport width */
  width: 100vw;
  height: 100vw;
  display: block;
}

#controls {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  right: 0;
  color: white;
  font-family: sans-serif;
  padding: 8px;
  
  label {
    margin-bottom: 8px;
  }
}
              
            
!

JS

              
                function createShader(gl, type, source) {
  const shader = gl.createShader(type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  
  const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  if (success) return shader;
  
  console.log(gl.getShaderInfoLog(shader));
  gl.deleteShader(shader);
}

function createProgram(gl, vertexShader, fragmentShader) {
  const program = gl.createProgram();
  
  console.log('V', vertexShader, 'F', fragmentShader);
  
  gl.attachShader(program, vertexShader);
  gl.attachShader(program, fragmentShader);
  gl.linkProgram(program);
  
  var success = gl.getProgramParameter(program, gl.LINK_STATUS);
  if (success) return program;

  console.log(gl.getProgramInfoLog(program));
  gl.deleteProgram(program);
}

// A singleton with matrix methods.
const mat3 = {
  multiply(a, b) {
    const a00 = a[0];
    const a01 = a[1];
    const a02 = a[2];
    const a10 = a[3];
    const a11 = a[4];
    const a12 = a[5];
    const a20 = a[6];
    const a21 = a[7];
    const a22 = a[8];
    const b00 = b[0];
    const b01 = b[1];
    const b02 = b[2];
    const b10 = b[3];
    const b11 = b[4];
    const b12 = b[5];
    const b20 = b[6];
    const b21 = b[7];
    const b22 = b[8];

    return [
      b00 * a00 + b01 * a10 + b02 * a20,
      b00 * a01 + b01 * a11 + b02 * a21,
      b00 * a02 + b01 * a12 + b02 * a22,
      b10 * a00 + b11 * a10 + b12 * a20,
      b10 * a01 + b11 * a11 + b12 * a21,
      b10 * a02 + b11 * a12 + b12 * a22,
      b20 * a00 + b21 * a10 + b22 * a20,
      b20 * a01 + b21 * a11 + b22 * a21,
      b20 * a02 + b21 * a12 + b22 * a22,
    ];
  },
  
  translate(m, tx, ty) {
    const translationMatrix = [
      1, 0, 0,
      0, 1, 0,
      tx, ty, 1,
    ];
    
    return this.multiply(m, translationMatrix);
  },
  
  rotate(m, angleInRadians) {
    const c = Math.cos(angleInRadians);
    const s = Math.sin(angleInRadians);
    const rotationMatrix = [
      c, -s, 0,
      s, c, 0,
      0, 0, 1,     
    ];
    
    return this.multiply(m, rotationMatrix);
  },
  
  rotateDeg(m, angleInDegrees) {
    const angleInRadians = (angleInDegrees / 360) * 2 * Math.PI;
    return this.rotate(m, angleInRadians);
  },
 
  scale(m, sx, sy) {
    const scaleMatrix = [
      sx, 0, 0,
      0, sy, 0,
      0, 0, 1,
    ];
    
    return this.multiply(m, scaleMatrix);
  },
  
  projection(width, height) {
    return [
      2 / width, 0, 0,
      0, -2 / height, 0,
      -1, 1, 1
    ];
  }
};

function setupScene() {
  const canvasEl = document.querySelector('canvas');
  const gl = canvasEl.getContext('webgl');
  if (!gl) {
    console.log('no cavase context found');
    return;
  }
  
  const vertexShader = createShader(gl, gl.VERTEX_SHADER, `
    attribute vec2 a_position;
    attribute vec3 a_color;
    uniform mat3 u_matrix;
    varying vec4 v_vertex_color;
    
    void main() {
      gl_Position = vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);
      v_vertex_color = vec4(a_color / 255.0, 1.0);
    }
  `);
  
  const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, `
    precision mediump float;
    varying vec4 v_vertex_color;
    
    void main() {
      gl_FragColor = v_vertex_color;
    }
  `);
  
  const program = createProgram(gl, vertexShader, fragmentShader);
  
  const drawScene = (sliderValues) => {
    // Prep and clear the canvas.
    canvasEl.width = canvasEl.clientWidth;
    canvasEl.height = canvasEl.clientHeight;
    gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
    gl.clearColor(0, 0, 0, 1); // Black background
    gl.clear(gl.COLOR_BUFFER_BIT);

    gl.useProgram(program);


    const projectionMatrix = mat3.projection(gl.canvas.width, gl.canvas.height);
    let transformationMatrix = mat3.scale(projectionMatrix, sliderValues.scale, sliderValues.scale);
    transformationMatrix = mat3.translate(transformationMatrix, sliderValues.translateX, sliderValues.translateY);

    transformationMatrix = mat3.rotateDeg(transformationMatrix, sliderValues.rotate);
    
    gl.uniformMatrix3fv(gl.getUniformLocation(program, "u_matrix"), false, transformationMatrix);

    // Position Buffer Setup
    const positionBuffer = gl.createBuffer();
    const positions = [
     [142,147], [142,6.5], [172.5,105], // Triangle 1
     [142,147], [276,105], [172.5,105], // Triangle 2
     [142,147], [193,165], [276,105], // Triangle 3
     [142,147], [193,165], [225,262], // Triangle 4
     [142,147], [142,203], [225,262], // Triangle 5
     [142,147], [142,203], [58,262], // Triangle 6
     [142,147], [90,165], [58,262], // Triangle 7
     [142,147], [6,105], [90,165], // Triangle 8
     [142,147], [108,105], [6,105], // Triangle 9
     [142,147], [142,6.5], [108,105], // Triangle 10
    ].flat();  
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

    // Position Attribute setup
    //  - Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
    //  - There's no reference to ARRAY_BUFFER here. Is it just assumed?
    const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
    var size = 2, type = gl.FLOAT, normalize = false, stride = 0, offset = 0;
    gl.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);
    gl.enableVertexAttribArray(positionAttributeLocation);  

    // Color Buffer Setup
    const colorBuffer = gl.createBuffer();
    const colors = [ // [R, G, B]
      [210, 195, 8], [210, 195, 8], [210, 195, 8], // Triangle 1 
      [250, 242, 111], [250, 242, 111], [250, 242, 111], // Triangle 2
      [162, 153, 16], [162, 153, 16], [162, 153, 16], // Triangle 3
      [210, 195, 8], [210, 195, 8], [210, 195, 8], // Triangle 4
      [162, 153, 16], [162, 153, 16], [162, 153, 16], // Triangle 5
      [162, 153, 16], [162, 153, 16], [162, 153, 16], // Triangle 6
      [250, 242, 111], [250, 242, 111], [250, 242, 111], // Triangle 7
      [210, 195, 8], [210, 195, 8], [210, 195, 8], // Triangle 8
      [250, 242, 111], [250, 242, 111], [250, 242, 111], // Triangle 9
      [240, 229, 79], [240, 229, 79], [240, 229, 79] // Triangle 10
    ].flat();
    gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);

    // Color Attribute Setup
    const colorAttributeLocation = gl.getAttribLocation(program, "a_color");
    var size = 3, type = gl.FLOAT, normalize = false, stride = 0, offset = 0;
    gl.vertexAttribPointer(colorAttributeLocation, size, type, normalize, stride, offset);
    gl.enableVertexAttribArray(colorAttributeLocation);  

    const startingIndex = 0, count = 30;
    gl.drawArrays(gl.TRIANGLES, startingIndex, count);
  }
  
  return drawScene;
}


const drawScene = setupScene();
const drawSceneWithSliderValues = () => {
  const sliderValues = {
    scale: Number(document.querySelector('#scale').value),
    rotate: Number(document.querySelector('#rotate').value),
    translateX: Number(document.querySelector('#translateX').value),
    translateY: Number(document.querySelector('#translateY').value)
  };
  
  drawScene(sliderValues);
};

drawSceneWithSliderValues();


document.querySelector('#scale').addEventListener('input', drawSceneWithSliderValues);
document.querySelector('#rotate').addEventListener('input', drawSceneWithSliderValues);
document.querySelector('#translateX').addEventListener('input', drawSceneWithSliderValues);
document.querySelector('#translateY').addEventListener('input', drawSceneWithSliderValues);
              
            
!
999px

Console