<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/By_example/Clearing_with_colors -->
<p>A very simple WebGL program that shows some color.</p>
<!-- Text within a canvas element is displayed
only if canvas is not supported. -->
<canvas>Your browser does not seem to support
HTML5 canvas.</canvas>
// Run everything inside window load event handler, to make sure
// DOM is fully loaded and styled before trying to manipulate it,
// and to not mess up the global scope. We are giving the event
// handler a name (setupWebGL) so that we can refer to the
// function object within the function itself.
window.addEventListener("load", function setupWebGL (evt) {
"use strict"
// Cleaning after ourselves. The event handler removes
// itself, because it only needs to run once.
window.removeEventListener(evt.type, setupWebGL, false);
// References to the document elements.
var paragraph = document.querySelector("p"),
canvas = document.querySelector("canvas");
// Getting the WebGL rendering context.
var gl = canvas.getContext("webgl")
|| canvas.getContext("experimental-webgl");
// If failed, inform user of failure. Otherwise, initialize
// the drawing buffer (the viewport) and clear the context
// with a solid color.
if (!gl) {
paragraph.innerHTML = "Failed to get WebGL context. "
+ "Your browser or device may not support WebGL.";
return;
}
paragraph.innerHTML =
"Congratulations! Your browser supports WebGL. ";
gl.viewport(10, 20,
gl.drawingBufferWidth, gl.drawingBufferHeight);
// Set the clear color to darkish green.
gl.clearColor(12.0, 0.5, 0.0, 1.0);
// Clear the context with the newly set color. This is
// the function call that actually does the drawing.
gl.clear(gl.COLOR_BUFFER_BIT);
}, false);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.