The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images. It is a low level, procedural model that updates a bitmap and does not have a built-in scene graph.
Canvas was initially introduced by Apple for use inside their own Mac OS X WebKit component in 2004, powering applications like Dashboard widgets and the Safari browser. Later, in 2005 it was adopted in version 1.8 of Gecko browsers, and Opera in 2006, and standardized by the Web Hypertext Application Technology Working Group (WHATWG) on new proposed specifications for next generation web technologies.
The following code creates a Canvas element in an HTML page:
<canvas id="example" width="200" height="200"> This text is displayed if your browser does not support HTML5 Canvas. </canvas>
var example = document.getElementById('example'); var context = example.getContext('2d'); context.fillStyle = 'red'; context.fillRect(30, 30, 50, 50);
This code draws a red rectangle on the screen.
Canvas versus Scalable Vector Graphics (SVG)
SVG is an earlier standard for drawing shapes in browsers. However, unlike Canvas, which is raster-based, SVG is vector-based, i.e., each drawn shape is remembered as an object in a scene graph or Document Object Model, which is subsequently rendered to a bitmap. This means that if attributes of an SVG object are changed, the browser can automatically re-render the scene.
SVG images are represented in XML, and complex scenes can be created and maintained with XML editing tools.
The SVG scene graph enables event handlers to be associated with objects, so a rectangle may respond to an
onClick event. To get the same functionality with canvas, one must manually match the coordinates of the mouse click with the coordinates of the drawn rectangle to determine whether it was clicked.
At the time of its introduction the canvas element was met with mixed reactions from the web standards community. There have been arguments against Apple's decision to create a new proprietary element instead of supporting the SVG standard. There are other concerns about syntax, e.g., the absence of a namespace.
Intellectual property over canvas
On March 14, 2007, WebKit developer Dave Hyatt forwarded an email from Apple's Senior Patent Counsel, Helene Plotka Workman, which stated that Apple reserved all intellectual property rights relative to WHATWG’s Web Applications 1.0 Working Draft, dated March 24, 2005, Section 10.1, entitled “Graphics: The bitmap canvas”, but left the door open to licensing the patents should the specification be transferred to a standards body with a formal patent policy. This caused considerable discussion among web developers, and raised questions concerning the WHATWG's lack of a policy on patents in comparison to the World Wide Web Consortium (W3C)'s explicit favoring of royalty-free licenses. Apple later disclosed the patents under the W3C's royalty-free patent licensing terms. The disclosure means that Apple is required to provide royalty-free licensing for the patent whenever the Canvas element becomes part of a future W3C recommendation created by the HTML working group.
The element is supported by the current versions of Mozilla Firefox, Google Chrome, Internet Explorer, Safari, Konqueror and Opera. Older versions of Internet Explorer, version 8 and earlier do not support
canvas, however Google and Mozilla plugins are available.
|Internet Explorer||Firefox||Safari (Desktop)||Chrome||Opera (Desktop)||Safari (Mobile)||Opera (Mobile)||Android Browser|
|6.0||2.0 - 6.0||3.1 - 3.2||4.0 - 13.0||9.0 - 11.0||3.2||10.0||2.0|
|8.0||8.0||5.0||15.0||11.5||4.2 - 4.3||11.1||2.3,3.0|
- Cairo (graphics)
- Comparison of layout engines (HTML5 Canvas)
- Display PostScript
- Quartz 2D
- Ian Hixie (2004-07-12). "Extending HTML". Retrieved 2011-06-13.
- Mozilla Developer Connection. "HTMLCanvasElement". Retrieved 2011-06-13.
- Opera 9.0 changelog
- Ian Hickson remarks regarding canvas and other Apple extensions to HTML
- [whatwg] Web Applications 1.0 Draft, David Hyatt, Wed Mar 14 14:31:53 PDT 2007
- Web Applications 1.0 Early Working Draft - Dynamic graphics: The bitmap canvas
- HTML Working Group Patent Policy Status – Known Disclosures
- W3C patent policy in use by HTML working group
- Sucan, Mihai (4 Feb, 2010). "SVG or Canvas? Сhoosing between the two". Opera Software. Retrieved 3 May 2010.
- "Mozilla drags IE into the future with Canvas element plugin". Ars Technica. 19 Aug, 2008. Retrieved 12 May 2010.
- "Compatibility tables for the support of HTML5, CSS3, SVG". Retrieved 27. Juli 2011.
- The w3c official description for the canvas element
- Canvas description in WHATWG Web Applications draft specifications
- Canvas reference page in Apple Developers Connection
- Basic Canvas Tutorial on Opera Developer Community
- Canvas tutorial and introductory page on Mozilla Developer center