English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The width attribute specifies the width of the <canvas> element in pixels, using the width attribute to specify the width of the <canvas> element in pixels.
Height and width are200-pixel <canvas> element:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML <canvas> width Attribute Usage-Basic Tutorial(oldtoolbag.com)</title> </<head> <body> <canvas id="myCanvas" width="200" height="200" style="border:1px solid"> Your browser does not support the HTML5 canvas tag. </canvas> <script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.fillStyle = "#92B901"; ctx.fillRect(50, 50, 100, 100); </<script> </body> </html>Test to see ‹/›
IEFirefoxOperaChromeSafari
All major browsers support the width attribute.
Note: Internet Explorer 8 and earlier IE versions do not support the <canvas> tag.
The width attribute specifies the width of the <canvas> element in pixels.
Tip: Use the width attribute to specify the width of the <canvas> element in pixels.
Tip: When the height or width of the canvas is reset, the content of the canvas is cleared (see the example at the bottom of the page).
Tip: Please visit our HTML Canvas Learn more about <canvas> in the HTML tutorial.
<canvas> is an HTML5 of the new tag.
<canvas width="pixels">
Value | Description |
---|---|
pixels | Specify the canvas's width attribute, in pixels (such as"100) Default width is "300" |
Clear the canvas by setting the width or height attribute (using JavaScript):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML <canvas> width Attribute Usage-Basic Tutorial(oldtoolbag.com)</title> </<head> <body> <canvas id="myCanvas" width="200" height="200" style="border:1px solid"> Your browser does not support the HTML5 canvas tag. </canvas> <br> <script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.fillStyle = "#92B901"; ctx.fillRect(50, 50, 100, 100); function clearCanvas() { c.height = 300; } </<script> <button onclick="clearCanvas()">Clear canvas</button>/button> </body> </html>Test to see ‹/›