Skip to content

Instantly share code, notes, and snippets.

@mathdoodle
Last active August 29, 2015 14:22
Show Gist options
  • Save mathdoodle/2e684eed43835f280439 to your computer and use it in GitHub Desktop.
Save mathdoodle/2e684eed43835f280439 to your computer and use it in GitHub Desktop.
WebGL 001 canvas
{
"uuid": "8f482ee9-5870-4c78-b6e0-54e46563beab",
"description": "WebGL 001 canvas",
"dependencies": {
"DomReady": "latest"
}
}
<!doctype html>
<html>
<head>
<style>
/* STYLE-MARKER */
</style>
<!-- SCRIPTS-MARKER -->
<script>
// CODE-MARKER
</script>
</head>
<body>
<canvas id='my-canvas' width='400' height='300'></canvas>
</body>
</html>
DomReady.ready(main);
function main() {
var canvas = <HTMLCanvasElement>document.getElementById('my-canvas');
var gl = initWebGL(canvas);
gl.clearColor(1.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
}
function initWebGL(canvas: HTMLCanvasElement): WebGLRenderingContext {
var context: WebGLRenderingContext = null;
try {
// Try to grab the standard context. If it fails, fallback to experimental.
context = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
}
catch(e) {
}
if (context) {
return context;
}
else {
throw new Error("Unable to initialize WebGL. Your browser may not support it.");
}
}
body { background-color: grey; }
canvas { background-color: white;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment