Added in HTML5, the HTML <canvas> element can be used to draw graphics via scripting in JavaScript. For example, it can be used to draw graphs, make photo compositions, create animations, or even do real-time video processing or rendering.
Mozilla applications gained support for <canvas> starting with Gecko 1.8 (i.e. Firefox 1.5). The element was originally introduced by Apple for the OS X Dashboard and Safari. Internet Explorer supports <canvas> from version 9 onwards; for earlier versions of IE, a page can effectively add support for <canvas> by including a script from Google's Explorer Canvas project. Google Chrome and Opera 9 also support <canvas>.
The <canvas> element is also used by WebGL to draw hardware-accelerated 3D graphics on web pages.
This is just a simple code snippet which uses the CanvasRenderingContext2D.fillRect() method.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);
Edit the code below and see your changes update live in the canvas:
The interfaces related to the WebGLRenderingContext are referenced under WebGL.
CanvasCaptureMediaStream is related.
<canvas> and its advanced features.<canvas>.<video> and <canvas> to manipulate video data in real time.ImageBitmap to directly examine and alter pixels in the canvas's bitmap.| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'the 2D rendering context' in that specification. | Living Standard |
© 2005–2018 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/Canvas_API