Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Canvas element

From Wikipedia, the free encyclopedia
HTML element
This article is about the HTML element. For the general element in user interfaces, seeCanvas (GUI).
HTML
HTML5 logo
HTML and variants
HTML elements and attributes
Editing
Character encodings and language
Document and browser models
Client-side scripting and APIs
Graphics and Web3D technology
Comparisons

TheHTMLcanvas element allows for dynamic,scriptablerendering of2D shapes andbitmap images. Introduced inHTML5, it is a low level, procedural model that updates abitmap. The<canvas> element also helps in making2D games.

While the<canvas> element offers its own 2D drawingAPI, it also supports theWebGL API to allow 3D rendering withOpenGL ES.

History

[edit]

Canvas was initially introduced byApple for use in their ownMac OS XWebKit component in 2004,[1] powering applications likeDashboardwidgets and theSafari browser. Later, in 2005, it was adopted in version 1.8 ofGecko browsers,[2] andOpera in 2006,[3] and standardized by theWeb Hypertext Application Technology Working Group (WHATWG) on new proposed specifications for next generation web technologies.[4]

Usage

[edit]

Acanvas consists of a drawable region defined inHTML code withheight andwidth attributes.JavaScript code may access the area through a full set of drawing functions similar to those of other common 2DAPIs, 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 WebGL2rendering context.

Example

[edit]

The following code creates a<canvas> element in an HTML page:

<canvasid="example"width="200"height="200">This text is displayed if your browser does not support HTML canvas element.</canvas>

UsingJavaScript, you can draw on the canvas:

varexample=document.getElementById('example');varcontext=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 providessave() andrestore(), for saving and restoring all the canvas context's attributes.

Canvas element size versus drawing surface size

[edit]

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 usesCSS to set the canvas element's size, the size of the element is 600pixels 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:

<!DOCTYPE html><html><head><title>Canvas element size: 600 x 300,    Canvas drawing surface size: 300 x 150</title><style>body{background:#dddddd;}#canvas{margin:20px;padding:20px;background:#ffffff;border:thininset#aaaaaa;width:600px;height:300px;}</style></head><body><canvasid="canvas">        Canvas not supported</canvas></body></html>

Canvas versus Scalable Vector Graphics (SVG)

[edit]
icon
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(October 2017) (Learn how and when to remove this message)

SVG is an alternative approach to drawing shapes in browsers.[5] Unlike canvas, which israster-based, SVG isvector-based, so that each drawn shape is remembered as an object in ascene graph orDocument Object Model, which is subsequently rendered to abitmap. 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 inimmediate 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 additionalJavaScript 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 inXML, and complex scenes can be created and maintained with XML editing tools.

The SVG scene graph enablesevent handlers to be associated with objects, so a rectangle may respond to anonClick event. To get the same functionality with canvas, one must manually match thecoordinates 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

[edit]

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 theSVG standard. There are other concerns about syntax, such as the absence of anamespace.[6]

Intellectual property over canvas

[edit]

On March 14, 2007, WebKit developerDave Hyatt forwarded an email from Apple's Senior Patent Counsel, Helene Plotka Workman,[7] which stated that Apple reserved allintellectual property rights relative to WHATWG's Web Applications 1.0 Working Draft, dated March 24, 2005, Section 10.1, entitled "Graphics: The bitmap canvas",[8] but left the door open to licensing the patents should the specification be transferred to a standards body with aformal 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 theWorld 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.[9] 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.[10]

Privacy concerns

[edit]

Canvas fingerprinting is one of a number ofbrowser fingerprinting techniques for tracking online users that allow websites to identify and track visitors using the HTML<canvas> element. The technique received wide media coverage in 2014,[11][12][13][14] after researchers fromPrinceton University andKU Leuven University described it in their paperThe Web never forgets.[15] 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

[edit]

The element is supported by the current versions ofMozilla Firefox,Google Chrome,Internet Explorer,Safari,Konqueror,Opera[16] andMicrosoft Edge.[17]

See also

[edit]

References

[edit]
  1. ^Ian Hixie (2004-07-12)."Extending HTML". Retrieved2011-06-13.
  2. ^Mozilla Developer Connection."HTMLCanvasElement". Archived fromthe original on 2011-06-04. Retrieved2011-06-13.
  3. ^"Opera 9.0 changelog". Archived fromthe original on 2012-09-10. Retrieved2006-06-20.
  4. ^"HTML Standard".html.spec.whatwg.org. Retrieved2025-02-09.
  5. ^"Scalable Vector Graphics | CorelDRAW".www.coreldraw.com. Retrieved2022-09-23.
  6. ^Ian Hickson remarks regarding canvas and other Apple extensions to HTML
  7. ^"[whatwg] Web Applications 1.0 Draft, David Hyatt, Wed Mar 14 14:31:53 PDT 2007". Archived fromthe original on 2007-05-02. Retrieved2007-05-01.
  8. ^Web Applications 1.0 Early Working Draft -Dynamic graphics: The bitmap canvas
  9. ^HTML Working Group Patent Policy Status – Known Disclosures
  10. ^W3C patent policy in use by HTML working group
  11. ^Knibbs, Kate (July 21, 2014)."What You Need to Know About the Sneakiest New Online Tracking Tool".Gizmodo. RetrievedJuly 21, 2014.
  12. ^Joseph Steinberg (July 23, 2014)."You Are Being Tracked Online By A Sneaky New Technology -- Here's What You Need To Know".Forbes. RetrievedNovember 15, 2014.
  13. ^Angwin, Julia (July 21, 2014)."Meet the Online Tracking Device That is Virtually Impossible to Block".ProPublica. RetrievedJuly 21, 2014.
  14. ^Kirk, Jeremy (July 21, 2014)."Stealthy Web tracking tools pose increasing privacy risks to users".PC World. RetrievedJuly 21, 2014.
  15. ^Acar, Gunes; Eubank, Christian; Englehardt, Steven; Juarez, Marc; Narayanan, Arvind; Diaz, Claudia (July 24, 2014)."The Web never forgets: Persistent tracking mechanisms in the wild". RetrievedJuly 24, 2014.
  16. ^Sucan, Mihai (4 Feb 2010)."SVG or Canvas? Choosing between the two".Opera Software. Archived fromthe original on 23 June 2010. Retrieved3 May 2010.
  17. ^"Canvas, Microsoft Edge documentation".

External links

[edit]
Protocols
Server APIs
Apache modules
Topics
Browser APIs
Web APIs
WHATWG
W3C
Khronos
Others
Topics
Related topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Canvas_element&oldid=1330262858"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp