Canvas element
{{Short description|HTML element}}
{{about|the HTML element|the general element in user interfaces|Canvas (GUI)}}
{{HTML}}
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. HTML5 Canvas also helps in making 2D games.
While the HTML5 canvas offers its own 2D drawing API, it also supports the WebGL API to allow 3D rendering with OpenGL ES.
History
Canvas was initially introduced by Apple for use in their own Mac OS X WebKit component in 2004,{{cite web | url=http://ln.hixie.ch/?start=1089635050&count=1 | author=Ian Hixie | title=Extending HTML | date=2004-07-12 | access-date=2011-06-13 }} powering applications like Dashboard widgets and the Safari browser. Later, in 2005, it was adopted in version 1.8 of Gecko browsers,{{cite web | url=https://developer.mozilla.org/en/DOM/HTMLCanvasElement | author=Mozilla Developer Connection | title=HTMLCanvasElement | access-date=2011-06-13 | archive-url=https://web.archive.org/web/20110604062413/https://developer.mozilla.org/en/DOM/HTMLCanvasElement | archive-date=2011-06-04 | url-status=dead }} and Opera in 2006,{{Cite web |url=http://www.opera.com/docs/changelogs/windows/900/ |title=Opera 9.0 changelog |access-date=2006-06-20 |archive-url=https://archive.today/20120910/http://www.opera.com/docs/changelogs/windows/900/ |archive-date=2012-09-10 |url-status=dead }} and standardized by the Web Hypertext Application Technology Working Group (WHATWG) on new proposed specifications for next generation web technologies.{{Cite web |title=HTML Standard |url=https://html.spec.whatwg.org/multipage/canvas.html |access-date=2025-02-09 |website=html.spec.whatwg.org}}
Usage
A canvas
consists of a drawable region defined in HTML code with height and width attributes. JavaScript code may access the area through a full set of drawing functions similar to those of other common 2D APIs, thus allowing for dynamically generated graphics. Some anticipated uses of canvas include building graphs, animations, games, and image composition.
Interacting with the canvas involves obtaining the canvas' rendering context, which determines whether to use the canvas API, WebGL, or WebGL2 rendering context.
Example
The following code creates a Canvas element in an HTML page:
This text is displayed if your browser does not support HTML5 Canvas.
Using JavaScript, you can draw on the 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.
The Canvas API also provides save()
and restore()
, for saving and restoring all the canvas context's attributes.
Canvas element size versus drawing surface size
A canvas actually has two sizes: the size of the element itself and the size of the element's drawing surface. Setting the element's width and height attributes sets both of these sizes; CSS attributes affect only the element's size and not the drawing surface.
By default, both the canvas element's size and the size of its drawing surface is 300 screen pixels wide and 150 screen pixels high. In the listing shown in the example, which uses CSS to set the canvas element's size, the size of the element is 600 pixels wide and 300 pixels high, but the size of the drawing surface remains unchanged at the default value of 300 pixels × 150 pixels. When a canvas element's size does not match the size of its drawing surface, the browser scales the drawing surface to fit the element (which may result in surprising and unwanted effects).
Example setting element size and drawing surface size to different values:
Canvas drawing surface size: 300 x 150
body {
background: #dddddd;
}
#canvas {
margin: 20px;
padding: 20px;
background: #ffffff;
border: thin inset #aaaaaa;
width: 600px;
height: 300px;
}
Canvas not supported
Canvas versus Scalable Vector Graphics (SVG)
{{Unreferenced section|date=October 2017}}
SVG is an alternative approach to drawing shapes in browsers.{{Cite web |title=Scalable Vector Graphics {{!}} CorelDRAW |url=https://www.coreldraw.com/en/learn/guide-to-vector-design/scalable-vector-graphics/ |access-date=2022-09-23 |website=www.coreldraw.com}} Unlike canvas, which is raster-based, SVG is vector-based, so that 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.
Canvas objects, on the other hand, are drawn in immediate mode. In the canvas example above, the rectangle draw operation modifies the canvas, and its representation as a rectangle is forgotten by the system. If the rectangle's position were to be changed, the canvas would need to be redrawn, including any objects that might have been covered by the rectangle. In the equivalent SVG case, one could simply change the position attributes of the rectangle and the browser would determine how to repaint it. There are additional JavaScript libraries that abstract the canvas model to have svg-like scene capabilities within the canvas element. Multiple canvas layers can also be used, meaning that only specific layers need to be recreated when changes are required.
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.
Conceptually, canvas is a lower-level API upon which higher-level interfaces might be built (for example, SVG support). There are JavaScript libraries that provide partial SVG implementations using canvas for browsers that do not provide SVG but support canvas, such as the browsers in Android 2.x. However, this is not normally the case—they are independent standards. The situation is complicated because there are scene graph libraries for canvas, and SVG has some bitmap manipulation functionality.
Reactions
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, such as the absence of a namespace.[http://ln.hixie.ch/?start=1089635050&count=1 Ian Hickson remarks regarding canvas and other Apple extensions to HTML]
= Intellectual property over canvas =
On March 14, 2007, WebKit developer Dave Hyatt forwarded an email from Apple's Senior Patent Counsel, Helene Plotka Workman,{{Cite web |url=http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2007-March/010129.html |title=
= Privacy concerns =
Canvas fingerprinting is one of a number of browser fingerprinting techniques for tracking online users that allow websites to identify and track visitors using HTML5 canvas element. The technique received wide media coverage in 2014,{{cite web | url=https://gizmodo.com/what-you-need-to-know-about-the-sneakiest-new-online-tr-1608455771 | title=What You Need to Know About the Sneakiest New Online Tracking Tool | website=Gizmodo | date=July 21, 2014 | access-date=July 21, 2014 | author=Knibbs, Kate}}{{cite magazine | url=https://www.forbes.com/sites/josephsteinberg/2014/07/23/you-are-being-tracked-online-by-a-sneaky-new-technology-heres-what-you-need-to-know/ | title=You Are Being Tracked Online By A Sneaky New Technology -- Here's What You Need To Know | date=July 23, 2014 | magazine=Forbes | access-date=November 15, 2014 | author=Joseph Steinberg }}{{cite web | url=https://www.propublica.org/article/meet-the-online-tracking-device-that-is-virtually-impossible-to-block | title=Meet the Online Tracking Device That is Virtually Impossible to Block | publisher=ProPublica | date=July 21, 2014 | access-date=July 21, 2014 | author=Angwin, Julia | author-link=Julia Angwin}}{{cite magazine | url=http://www.pcworld.com/article/2456640/stealthy-web-tracking-tools-pose-increasing-privacy-risks-to-users.html | title=Stealthy Web tracking tools pose increasing privacy risks to users | magazine=PC World | date=July 21, 2014 | access-date=July 21, 2014 | author=Kirk, Jeremy}} after researchers from Princeton University and KU Leuven University described it in their paper The Web never forgets.{{cite web | url=https://securehomes.esat.kuleuven.be/~gacar/persistent/index.html | title=The Web never forgets: Persistent tracking mechanisms in the wild | date=July 24, 2014 | access-date=July 24, 2014 | author=Acar, Gunes | author2=Eubank, Christian | author3=Englehardt, Steven | author4=Juarez, Marc | author5=Narayanan, Arvind | author6=Diaz, Claudia }} The privacy concerns regarding canvas fingerprinting center around the fact that even deleting cookies and clearing the cache will not be sufficient for users to avoid online tracking.
Browser support
The element is supported by the current versions of Mozilla Firefox, Google Chrome, Internet Explorer, Safari, Konqueror, Opera{{cite web|url=http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/|title=SVG or Canvas? Сhoosing between the two|last=Sucan|first=Mihai|date=4 Feb 2010|publisher=Opera Software|access-date=3 May 2010|archive-url=https://web.archive.org/web/20100623002104/http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/|archive-date=23 June 2010|url-status=dead}} and Microsoft Edge.{{cite web|url=https://docs.microsoft.com/en-us/microsoft-edge/dev-guide/graphics/canvas|title=Canvas, Microsoft Edge documentation}}
See also
References
{{Reflist}}
External links
- {{Citation |url = http://www.w3.org/TR/html5/scripting-1.html#the-canvas-element |title=The
canvas
element |publisher=W3C |date=2014-10-28 |access-date=2015-01-09 }} - {{Citation |url = http://www.w3.org/TR/2dcontext/ |title=HTML Canvas 2D Context |publisher=W3C |date=2014-08-21 |access-date=2015-01-09 }}
- [https://web.archive.org/web/20090427053125/http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html Canvas description in WHATWG Web Applications draft specifications]
- [https://web.archive.org/web/20080725095805/http://developer.apple.com/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/Canvas.html Canvas reference page in Apple Developers Connection]
- [http://dev.opera.com/articles/view/html-5-canvas-the-basics/ Basic Canvas Tutorial on Opera Developer Community]
- [https://developer.mozilla.org/en/Canvas_tutorial Canvas tutorial and introductory page on Mozilla Developer center] {{Webarchive|url=https://web.archive.org/web/20120803112420/https://developer.mozilla.org/en/Canvas_tutorial |date=2012-08-03 }}
{{Web interfaces}}
{{DEFAULTSORT:Canvas Element}}