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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <canvas width="570" height="570" id="myCanvas"></canvas>
<a href='https://curtastic.com/tutorial/webgl-2d' target='_blank'>Full Tutorial &raquo;</a>
              
            
!

CSS

              
                body{margin:0}
              
            
!

JS

              
                var gl,canvas

function gameLoop() {
   window.requestAnimationFrame(gameLoop)
   
   // Clear the screen.
   gl.clear(gl.COLOR_BUFFER_BIT)
   
   // The points of our triangles we want to draw.
   // 0,0 is the center of the canvas. -1,1 is the top left.
   var vertices = [
      -0.5+Math.sin(Date.now()*.01), 0.5,
      -0.5, -0.5,
      0.5, -0.5, 
      // Another triangle for fun.
      -0.7, 0.0,
      -0.8, -0.2,
      -0.6, -0.1,
   ]
   
   // Tell webGL to draw these triangle this frame.
   gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW)
   
   // Draw all the triangles.
   gl.drawArrays(gl.TRIANGLES, 0, vertices.length/2)
}

function glSetup() {
   canvas = document.getElementById('myCanvas')
   gl = canvas.getContext('experimental-webgl')
   
   // Set the viewport size to be the whole canvas.
   gl.viewport(0,0,canvas.width,canvas.height)
   
   // Set the background color to sky blue.
   gl.clearColor(.5, .7, 1, 1)

   // Tell webGL that we aren't doing anything special with the vertex buffer, just use a default one.
   gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer())
   
   // Vertex shader source code.
   var vertCode =
       'attribute vec2 coordinates;' +

       'void main(void) {' +
       // We are passing in only 2D coordinates. Then Z is always 0.0 and alhpa is always 1.0
       ' gl_Position = vec4(coordinates, 0.0, 1.0);' +
       '}'

   // Create a vertex shader object.
   var vertShader = gl.createShader(gl.VERTEX_SHADER)
   gl.shaderSource(vertShader, vertCode)
   gl.compileShader(vertShader)

   // Fragment shader source code.
   var fragCode =
       'void main(void) {' +
       // We aren't passing in colors right now so all the triangles are green. G=1.0=full green.
       ' gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);' +
       '}'

   // Create fragment shader object.
   var fragShader = gl.createShader(gl.FRAGMENT_SHADER)
   gl.shaderSource(fragShader, fragCode)
   gl.compileShader(fragShader)

   // Tell webGL to use both my shaders.
   var shaderProgram = gl.createProgram()
   gl.attachShader(shaderProgram, vertShader)
   gl.attachShader(shaderProgram, fragShader)
   gl.linkProgram(shaderProgram)
   gl.useProgram(shaderProgram)

   // Tell webGL to read 2 floats from the vertex array for each vertex, and store them in my vec2 shader variable named "coordinates"
   var coord = gl.getAttribLocation(shaderProgram, "coordinates")
   gl.vertexAttribPointer(coord, 2, gl.FLOAT, false, 0, 0)
   gl.enableVertexAttribArray(coord)
}

window.onload = function() {
   glSetup()
   gameLoop()
}

              
            
!
999px

Console