Our volunteers haven't translated this article into বাংলা (বাংলাদেশ) yet. Join us and help get the job done!
You can also read the article in English (US).

Use the HTML <canvas> element with either the canvas scripting API or the WebGL API to draw graphics and animations.

Content categories Flow content, phrasing content, embedded content, palpable content.
Permitted content Transparent but with no interactive content descendants except for <a> elements, <button> elements, <input> elements whose type attribute is checkbox, radio, or button.
Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that accepts phrasing content.
Permitted ARIA roles Any
DOM interface HTMLCanvasElement

Attributes

This element's attributes include the global attributes.

height
The height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque
Lets the canvas know whether or not translucency will be a factor. If the canvas knows there's no translucency, painting performance can be optimized. This is only supported by Mozilla-based browsers; use the standardized canvas.getContext('2d', { alpha: false }) instead.
width
The width of the coordinate space in CSS pixels. Defaults to 300.

Usage notes

Alternative content

You may (and should) provide alternate content inside the <canvas> block. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled.

Required </canvas> tag

Unlike the <img> element, the <canvas> element requires the closing tag (</canvas>).

Sizing the canvas

The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size. If your renderings seem distorted, try specifying your width and height attributes explicitly in the <canvas> attributes, and not using CSS.

Examples

This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to render the canvas, or if can't read a canvas. Providing a useful fallback text or sub DOM helps to make the the canvas more accessible.

<canvas id="canvas" width="300" height="300">
  An alternative text describing what your canvas displays. 
</canvas> 

Then in the JavaScript code, call HTMLCanvasElement.getContext() to get a drawing context and start drawing onto the canvas:

<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);
</script>

Opaque canvas

If your canvas does not use transparency, you can tell the browser that your canvas is opaque, this will be used internally to optimize rendering. To do this, set alpha to false when getting the drawing context:

<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d', { alpha: false });
</script>

Before the alpha option was standardized, you could use the moz-opaque attribute on the canvas tag. However, this only works in Mozilla-based rendering engines and should be avoided; check bug 878155 to track when this attribute will be removed.

<canvas id="myCanvas" moz-opaque></canvas>

Specifications

Specification Status Comment
HTML Living Standard
The definition of '<canvas>' in that specification.
Living Standard  
HTML5
The definition of '<canvas>' in that specification.
Recommendation Initial definition

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support1 Yes1.51 2 39924
height1 Yes1.51 2 39924
moz-opaque No No3.5 No No No
width1 Yes1.51 2 39924
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic support ? ? Yes41 2 3 ? No1
height ? ? Yes41 2 3 ? No1
moz-opaque No No No4 No No No
width ? ? Yes41 2 3 ? No1

1. Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.

2. Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.

3. Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

4. Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Versions of Safari prior to version 2 will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.

See also

ডকুমেন্ট ট্যাগ এবং অবদানকারী

 সর্বশেষ হালনাগাদ করেছেন: Jedipedia,