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.
What is HTML Canvas?
The <canvas> element is only a container for graphics, it has no drawing abilities of its own. User must use a script to actually draw the graphics.
The getContext() method returns an object that provides methods and properties for drawing on the canvas. Canvas has several methods for drawing paths, boxes, circles, text, and adding images. A canvas is a rectangular area on an HTML page. By default, a canvas has no border and no content.
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support <canvas> and its properties and methods.
Canvas vs SVG (Scalable Vector Graphics)
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.
How to use Canvas
The following code creates a Canvas element in an HTML page:
var element = document.getElementById('canvasSample'); var context = element.getContext('2d'); context.fillStyle = 'red'; context.fillRect(30, 30, 50, 50);