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></canvas>
<div id="controls"></div>
<script id="vs" type="notjs">
uniform mat4 u_worldViewProjection;

attribute vec4 a_position;
attribute vec2 a_texcoord;

varying vec4 v_position;
varying vec2 v_texCoord;

void main() {
  v_texCoord = a_texcoord;
  gl_Position = u_worldViewProjection * a_position;
}
</script>
<script id="fs" type="notjs">
precision mediump float;
uniform sampler2D u_texture;
uniform vec4 u_frontColor;
uniform vec4 u_backColor;
varying vec2 v_texCoord;

void main() {
  gl_FragColor = texture2D(u_texture, v_texCoord) * (gl_FrontFacing ? u_frontColor : u_backColor);
}
</script>
<script src="//twgljs.org/dist/twgl-full.min.js"></script>

              
            
!

CSS

              
                body, html {
  height: 100%;
  margin: 0;
  font-family: monospace;
}
canvas {
  width: 100%;
  height: 100%;
}
#controls {
  position: absolute;
  left: 10px;
  top: 10px;
  z-index: 2;
  background-color: rgba(0,0,0,0.5);
  width: 30%;
  padding: 5px;
}
input[type=range] {
  width: 100%;
  display: block;
}

              
            
!

JS

              
                "use strict";
var m4 = twgl.m4;
var $ = document.getElementById.bind(document);
twgl.setAttributePrefix("a_");
var gl = twgl.getWebGLContext(document.querySelector("canvas"));
                                                 var programInfo = twgl.createProgramInfo(gl, ["vs", "fs"], ["a_position", "a_texcoord"]);

var bufferInfo = twgl.primitives.createSphereBufferInfo(gl, 1, 24, 12);
var bufferInfo2 = twgl.primitives.createCubeBufferInfo(gl, 2);

var textures = twgl.createTextures(gl, {
  // A 2x2 pixel texture from a JavaScript array
  checker: {
    mag: gl.NEAREST,
    min: gl.LINEAR,
    src: [
      255, 255, 255, 255,
      192, 192, 192, 255,
      192, 192, 192, 255,
      255, 255, 255, 255,
    ],
  },
});

var g = {
  zNear: 3,
  zFar: 7,
  fovy: 45,
};

function setupRange(id, value, min, max) {
  var range = document.createElement("input");
  range.type = "range";
  range.value = value;
  range.min = min;
  range.max = max;
  range.step = 0.01;
  var label = document.createElement("label");
  label.appendChild(document.createTextNode(id + ": "));
  var span = document.createTextNode("");
  range.addEventListener('input', function(e) {
    span.nodeValue = e.target.value;
    g[id] = parseFloat(e.target.value);
  });
  span.nodeValue = range.value;
  label.appendChild(span);
  $("controls").appendChild(label);
  $("controls").appendChild(range);
}

setupRange("zNear", 3, 3, 7);
setupRange("zFar", 7, 3, 7);
//setupRange("fovy", 45, 20, 70);

var uniforms = {
  u_worldViewProjection: m4.identity(),
  u_frontColor: [1, 0, 1, 1],
  u_backColor: [0, 1, 1, 1],
  u_texture: textures.checker,
};  

var projection;

function draw(bufferInfo, translation, time) {
  var world =  m4.identity();
  m4.translate(world, translation, world);
  m4.rotateY(world, time, world);
  m4.rotateZ(world, time, world);
  m4.multiply(world, projection, uniforms.u_worldViewProjection);

  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  twgl.setUniforms(programInfo, uniforms);
  twgl.drawBufferInfo(gl, gl.TRIANGLES, bufferInfo);
}

function render(time) {
  time *= 0.001;
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  gl.enable(gl.DEPTH_TEST);

  var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  projection = m4.perspective(g.fovy * Math.PI / 180, aspect, g.zNear, g.zFar);
  
  gl.useProgram(programInfo.program);
  
  draw(bufferInfo , [ 2, 0, -5], time);
  draw(bufferInfo2, [-2, 0, -5], time);
  
  requestAnimationFrame(render);
}
requestAnimationFrame(render);

              
            
!
999px

Console