The WebGLRenderingContext interface provides the OpenGL ES 2.0 rendering context for the drawing surface of an HTML <canvas>
element.
To get an object of this interface, call getContext()
on a <canvas>
element, supplying "webgl" as the argument:
var canvas = document.getElementById('myCanvas'); var gl = canvas.getContext('webgl');
Once you have the WebGL rendering context for a canvas, you can render within it.
The WebGL tutorial has more information, examples, and resources on how to get started with WebGL.
See the WebGL constants page.
The following properties and methods provide general information and functionality to deal with the WebGL context:
WebGLRenderingContext.canvas
HTMLCanvasElement
. Might be null
if it is not associated with a <canvas>
element.WebGLRenderingContext.commit()
Pushes frames back to the original HTMLCanvasElement
, if the context is not directly fixed to a specific canvas.
WebGLRenderingContext.drawingBufferWidth
WebGLRenderingContext.drawingBufferHeight
WebGLRenderingContext.getContextAttributes()
WebGLContextAttributes
object that contains the actual context parameters. Might return null
, if the context is lost.WebGLRenderingContext.isContextLost()
true
if the context is lost, otherwise returns false
.WebGLRenderingContext.scissor()
WebGLRenderingContext.viewport()
WebGLRenderingContext.activeTexture()
WebGLRenderingContext.blendColor()
WebGLRenderingContext.blendEquation()
WebGLRenderingContext.blendEquationSeparate()
WebGLRenderingContext.blendFunc()
WebGLRenderingContext.blendFuncSeparate()
WebGLRenderingContext.clearColor()
WebGLRenderingContext.clearDepth()
WebGLRenderingContext.clearStencil()
WebGLRenderingContext.colorMask()
WebGLFramebuffer
.WebGLRenderingContext.cullFace()
WebGLRenderingContext.depthFunc()
WebGLRenderingContext.depthMask()
WebGLRenderingContext.depthRange()
WebGLRenderingContext.disable()
WebGLRenderingContext.enable()
WebGLRenderingContext.frontFace()
WebGLRenderingContext.getParameter()
WebGLRenderingContext.getError()
WebGLRenderingContext.hint()
WebGLRenderingContext.isEnabled()
WebGLRenderingContext.lineWidth()
WebGLRenderingContext.pixelStorei()
WebGLRenderingContext.polygonOffset()
WebGLRenderingContext.sampleCoverage()
WebGLRenderingContext.stencilFunc()
WebGLRenderingContext.stencilFuncSeparate()
WebGLRenderingContext.stencilMask()
WebGLRenderingContext.stencilMaskSeparate()
WebGLRenderingContext.stencilOp()
WebGLRenderingContext.stencilOpSeparate()
WebGLRenderingContext.bindBuffer()
WebGLBuffer
object to a given target.WebGLRenderingContext.bufferData()
WebGLRenderingContext.bufferSubData()
WebGLRenderingContext.createBuffer()
WebGLBuffer
object.WebGLRenderingContext.deleteBuffer()
WebGLBuffer
object.WebGLRenderingContext.getBufferParameter()
WebGLRenderingContext.isBuffer()
WebGLRenderingContext.bindFramebuffer()
WebGLFrameBuffer
object to a given target.WebGLRenderingContext.checkFramebufferStatus()
WebGLRenderingContext.createFramebuffer()
WebGLFrameBuffer
object.WebGLRenderingContext.deleteFramebuffer()
WebGLFrameBuffer
object.WebGLRenderingContext.framebufferRenderbuffer()
WebGLRenderingBuffer
object to a WebGLFrameBuffer
object.WebGLRenderingContext.framebufferTexture2D()
WebGLFrameBuffer
object.WebGLRenderingContext.getFramebufferAttachmentParameter()
WebGLRenderingContext.isFramebuffer()
WebGLFrameBuffer
object is valid.WebGLRenderingContext.readPixels()
WebGLFrameBuffer
.WebGLRenderingContext.bindRenderbuffer()
WebGLRenderBuffer
object to a given target.WebGLRenderingContext.createRenderbuffer()
WebGLRenderBuffer
object.WebGLRenderingContext.deleteRenderbuffer()
WebGLRenderBuffer
object.WebGLRenderingContext.getRenderbufferParameter()
WebGLRenderingContext.isRenderbuffer()
WebGLRenderingBuffer
is valid.WebGLRenderingContext.renderbufferStorage()
WebGLRenderingContext.bindTexture()
WebGLTexture
object to a given target.WebGLRenderingContext.compressedTexImage2D()
WebGLRenderingContext.compressedTexSubImage2D()
WebGLRenderingContext.copyTexImage2D()
WebGLRenderingContext.copyTexSubImage2D()
WebGLRenderingContext.createTexture()
WebGLTexture
object.WebGLRenderingContext.deleteTexture()
WebGLTexture
object.WebGLRenderingContext.generateMipmap()
WebGLTexture
object.WebGLRenderingContext.getTexParameter()
WebGLRenderingContext.isTexture()
WebGLTexture
is valid.WebGLRenderingContext.texImage2D()
WebGLRenderingContext.texSubImage2D()
WebGLTexture
.WebGLRenderingContext.texParameterf()
WebGLRenderingContext.texParameteri()
WebGLRenderingContext.attachShader()
WebGLShader
to a WebGLProgram
.WebGLRenderingContext.bindAttribLocation()
WebGLRenderingContext.compileShader()
WebGLShader
.WebGLRenderingContext.createProgram()
WebGLProgram
.WebGLRenderingContext.createShader()
WebGLShader
.WebGLRenderingContext.deleteProgram()
WebGLProgram
.WebGLRenderingContext.deleteShader()
WebGLShader
.WebGLRenderingContext.detachShader()
WebGLShader
.WebGLRenderingContext.getAttachedShaders()
WebGLShader
objects attached to a WebGLProgram
.WebGLRenderingContext.getProgramParameter()
WebGLRenderingContext.getProgramInfoLog()
WebGLProgram
object.WebGLRenderingContext.getShaderParameter()
WebGLRenderingContext.getShaderPrecisionFormat()
WebGLShaderPrecisionFormat
object describing the precision for the numeric format of the shader.WebGLRenderingContext.getShaderInfoLog()
WebGLShader
object.WebGLRenderingContext.getShaderSource()
WebGLShader
as a string.WebGLRenderingContext.isProgram()
WebGLProgram
is valid.WebGLRenderingContext.isShader()
WebGLShader
is valid.WebGLRenderingContext.linkProgram()
WebGLProgram
object.WebGLRenderingContext.shaderSource()
WebGLShader
.WebGLRenderingContext.useProgram()
WebGLProgram
as part the current rendering state.WebGLRenderingContext.validateProgram()
WebGLProgram
.WebGLRenderingContext.disableVertexAttribArray()
WebGLRenderingContext.enableVertexAttribArray()
WebGLRenderingContext.getActiveAttrib()
WebGLRenderingContext.getActiveUniform()
WebGLRenderingContext.getAttribLocation()
WebGLRenderingContext.getUniform()
WebGLRenderingContext.getUniformLocation()
WebGLRenderingContext.getVertexAttrib()
WebGLRenderingContext.getVertexAttribOffset()
WebGLRenderingContext.uniform[1234][fi][v]()
WebGLRenderingContext.uniformMatrix[234]fv()
WebGLRenderingContext.vertexAttrib[1234]f[v]()
WebGLRenderingContext.vertexAttribPointer()
WebGLRenderingContext.clear()
WebGLRenderingContext.drawArrays()
WebGLRenderingContext.drawElements()
WebGLRenderingContext.finish()
WebGLRenderingContext.flush()
These methods manage WebGL extensions:
WebGLRenderingContext.getSupportedExtensions()
Array
of DOMString
elements with all the supported WebGL extensions.WebGLRenderingContext.getExtension()
This example demonstrates how to detect a WebGL rendering context and reports the result to the user.
<p>[ Here would go the result of WebGL feature detection ]</p> <button>Press here to detect WebGLRenderingContext</button>
body { text-align : center; } button { display : block; font-size : inherit; margin : auto; padding : 0.6em; }
// Run everything inside window load event handler, to make sure // DOM is fully loaded and styled before trying to manipulate it. window.addEventListener("load", function() { var paragraph = document.querySelector("p"), button = document.querySelector("button"); // Adding click event handler to button. button.addEventListener("click", detectWebGLContext, false); function detectWebGLContext () { // Create canvas element. The canvas is not added to the // document itself, so it is never displayed in the // browser window. var canvas = document.createElement("canvas"); // Get WebGLRenderingContext from canvas element. var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl"); // Report the result. if (gl && gl instanceof WebGLRenderingContext) { paragraph.innerHTML = "Congratulations! Your browser supports WebGL."; } else { paragraph.innerHTML = "Failed to get WebGL context. " + "Your browser or device may not support WebGL."; } } }, false);
The source code of this example is also available on GitHub.
With scissor()
and clear()
we can demonstrate how the WebGL drawing buffer is affected by the size of the canvas.
The size of the first canvas is set to the styled Element
size, determined by CSS. This is done by assigning the width
and height
properties of the canvas to the values of the clientWidth
and clientHeight
properties, respectively.
In contrast, no such assignment is done for the second canvas. The internal width
and height
properties of the canvas remain at default values, which are different than the actual size of the canvas Element
in the browser window.
The effect is clearly visible when using scissor()
and clear()
to draw a square in the center of the canvas, by specifying its position and size in pixels. In the first canvas, we get the desired result. In the second, the square has the wrong shape, size, and position.
<p>Compare the two canvases.</p> <canvas>Your browser does not seem to support HTML5 canvas.</canvas> <canvas>Your browser does not seem to support HTML5 canvas.</canvas>
body { text-align : center; } canvas { display : inline-block; width : 120px; height : 80px; margin : auto; padding : 0; border : none; background-color : black; }
window.addEventListener("load", function() { "use strict" var firstCanvas = document.getElementsByTagName("canvas")[0], secondCanvas = document.getElementsByTagName("canvas")[1]; firstCanvas.width = firstCanvas.clientWidth; firstCanvas.height = firstCanvas.clientHeight; [firstCanvas, secondCanvas].forEach(function(canvas) { var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl"); if (!gl) { document.querySelector("p").innerHTML = "Failed to get WebGL context. " + "Your browser or device may not support WebGL."; return; } gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); gl.enable(gl.SCISSOR_TEST); gl.scissor(30, 10, 60, 60); gl.clearColor(1.0, 1.0, 0.0, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); }); }, false);
The source code of this example is also available on GitHub.
Specification | Status | Comment |
---|---|---|
WebGL 1.0 The definition of 'WebGLRenderingContext' in that specification. | Recommendation | Initial definition |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
---|---|---|---|---|---|---|---|
Basic Support | 9 | 121 | 4.0 | 111 | 12 | 5.1 | No support |
Available in workers | No support | No support | No support2 | No support | No support | No support | No support |
Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic Support | (Yes) | 25 | No support | (Yes) | 111 | 12 | 8.1 |
Available in workers | No support | No support | No support | No support | No support | No support | No support |
1. To access the WebGL context, use experimental-webgl rather than the standard webgl
2. This feature is experimentally implemented since Firefox 44; to activate it, in about:config, set gfx.offscreencanvas.enabled to true
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext