<html>
<head>
<meta name="description" content="One Triangle">
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body onload="start()">
<canvas id="mycanvas" width="400" height="400"></canvas>
</body>
</html>
// draw a triangle using WebGL
// write everything out, step at a time
//
// written by gleicher on October 3, 2015
function start() {
"use strict";
// first we need to get the canvas and make an OpenGL context
// in practice, you need to do error checking
var canvas = document.getElementById("mycanvas");
var gl = canvas.getContext("webgl");
// now we have to program the hardware
// we need to have our GLSL code somewhere
// putting it in strings is bad - but it's easy so I'll
// do it for now
var vertexSource = ""+
"attribute vec3 pos;" +
"void main(void) {" +
" gl_Position = vec4(pos, 1.0);" +
"}";
var fragmentSource = "" +
"void main(void) {" +
" gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);" +
"}";
// now we need to make those programs into
// "Shader Objects" - by running the compiler
// watch the steps:
// create an object
// attach the source code
// run the compiler
// check for errors
// first compile the vertex shader
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader,vertexSource);
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(vertexShader));
return null;
}
// now compile the fragment shader
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader,fragmentSource);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(fragmentShader));
return null;
}
// OK, we have a pair of shaders, we need to put them together
// into a "shader program" object
var shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
// with the vertex shader, we need to pass it positions
// as an attribute - so set up that communication
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "pos");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
// now that we have programs to run on the hardware, we can
// make our triangle
// let's define the vertex positions
var vertexPos = [
0.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0
];
// we need to put the vertices into a buffer so we can
// block transfer them to the graphics hardware
var trianglePosBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, trianglePosBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexPos), gl.STATIC_DRAW);
trianglePosBuffer.itemSize = 3;
trianglePosBuffer.numItems = 3;
// this is the "draw scene" function, but since this
// is execute once...
// first, let's clear the screen
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// now we draw the triangle
// we tell GL what program to use, and what memory block
// to use for the data, and that the data goes to the pos
// attribute
gl.useProgram(shaderProgram);
gl.bindBuffer(gl.ARRAY_BUFFER, trianglePosBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, trianglePosBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |