Canvas

Onze vrijwilligers hebben dit artikel nog niet naar het Nederlands vertaald. Doe mee en help de klus te klaren!

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 do hardware-accelerated 3D graphics on web pages.

Example:

Canvas

HTML Content

<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>

CSS Content


JavaScript Content

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);

Reference

The interfaces related to the WebGLRenderingContext are referenced under WebGL.

[1] CanvasPathMethods abstracts the path-related methods and properties used by both CanvasRenderingContext2D and Path. For the moment, no browser implements Path (see bug 830734) and the spec is still in flux.

[2] CanvasDrawingStyles abstracts the style-related methods and properties used by both CanvasRenderingContext2D and DrawingStyles.

Guides and tutorials

Canvas tutorial
A comprehensive tutorial covering both the basic usage of <canvas> and its advanced features.
Code snippets:Canvas
Some extension developer-oriented code snippets involving <canvas>.
Canvas examples
A few <canvas> demos.
Drawing DOM objects into a canvas
How to draw DOM content, such as HTML elements, into a canvas.
A basic raycaster
A demo of ray-tracing animation using canvas.
Drawing graphics with canvas
An older introductory page to the canvas, mostly superceded by the Canvas tutorial.

Resources

Generic

Libraries

  • Fabric.js is an open-source canvas library with SVG parsing capabilities 
  • Kinetic.js is an open-source canvas library focused on interactivity for desktop and mobile applications
  • Paper.js is an open source vector graphics scripting framework that runs on top of the HTML5 Canvas
  • libCanvas is powerful and lightweight canvas framework
  • Processing.js is a port of the Processing visualization language
  • EaselJS is a library with a Flash-like API
  • PlotKit is a charting and graphing library
  • Rekapi is an animation keyframing API for Canvas
  • PhiloGL is a WebGL framework for data visualization, creative coding and game development.
  • JavaScript InfoVis Toolkit creates interactive 2D Canvas data visualizations for the Web.
  • Frame-Engine is a framework for developping applications and games

Specifications

Specification Status Comment
WHATWG HTML Living Standard Living Standard  
HTML5 Candidate Recommendation