Horje
What is HTML <canvas> Tag

The <canvas> tag is used to draw graphics, on the fly, via scripting (usually JavaScript).

The <canvas> tag is transparent, and is only a container for graphics, you must use a script to actually draw the graphics.

Any text inside the <canvas> element will be displayed in browsers with JavaScript disabled and in browsers that do not support <canvas>.


How to create HTML <canvas> Tag

Draw a red rectangle on the fly, and show it inside the <canvas> element:
index.html
Example: HTML
 <canvas id="myCanvas">
Your browser does not support the canvas tag.
</canvas>

<script>
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 80, 80);
</script> 

Output should be:

How to create HTML <canvas> Tag

Tips and Notes for HTML <canvas> Tag

Tip: Learn more about the <canvas> element in our HTML Canvas Tutorial.

What Type of Browsers will Support for HTML <canvas> Tag

What Type of Browsers will Support for HTML <canvas> Tag

Attributes for HTML <canvas> Tag

Attribute Value Description
height pixels Specifies the height of the canvas. Default value is 150
width pixels Specifies the width of the canvas Default value is 300

Another <canvas> example

Follow the Example

index.html
Example: HTML
 <canvas id="myCanvas">
Your browser does not support the canvas tag.
</canvas>

<script>
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 75, 50);
//Turn transparency on
ctx.globalAlpha = 0.2;
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "green";
ctx.fillRect(80, 80, 75, 50);
</script> 

Output should be:

Another <canvas> example

Default CSS Settings For HTML <canvas> Tag

Most browsers will display the <canvas> element with the following default values:

index.html
Example: HTML
<style>
canvas {
  border:1px solid black;
}
</style>
<canvas id="myCanvas">Your browser does not support the HTML5 canvas tag.</canvas>
<script>
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 80, 100);
</script>

Output should be:

Default CSS Settings For HTML <canvas> Tag

How to add HTML <canvas> height Attribute

A <canvas> element with a height and width of 200 pixels.

More "Try it Yourself" examples below.

Definition and Usage

The height attribute specifies the height of the <canvas> element, in pixels.

Tip: Use the width attribute to specify the width of the <canvas> element, in pixels.

Tip: Each time the height or width of a canvas is re-set, the canvas content will be cleared (see example at bottom of page).

Browser Support

The numbers in the table specify the first browser version that fully supports the attribute.

Syntax

<canvas height="pixels">

Attribute Values

Value Description
pixels Specifies the height of the canvas, in pixels (e.g. "100"). Default value is 150

 

index.html
Example: HTML
<canvas id="myCanvas" width="200" height="200" style="border:1px solid">

Output should be:

How to add HTML <canvas> height Attribute

How to add HTML <canvas> width Attribute

A <canvas> element with a height and width of 200 pixels:

More "Try it Yourself" examples below.

Definition and Usage

The width attribute specifies the width of the <canvas> element, in pixels.

Tip: Use the height attribute to specify the height of the <canvas> element, in pixels.

Tip: Each time the height or width of a canvas is re-set, the canvas content will be cleared (see example at bottom of page).

Browser Support

The numbers in the table specify the first browser version that fully supports the attribute.

Syntax

<canvas width="pixels">

Attribute Values

Value Description
pixels Specifies the width of the canvas, in pixels (e.g. "100"). Default value is 300

 

index.html
Example: HTML
<canvas id="myCanvas" width="200" height="200" style="border:1px solid">

Output should be:

How to add HTML <canvas> width Attribute

How to Clear the canvas by setting the width attribute to 400px (using JavaScript)

Follow the Example.

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="200" style="border:1px solid">
Your browser does not support the HTML5 canvas tag.
</canvas>
<br>

<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillStyle = "#92B901";
ctx.fillRect(50, 50, 100, 100);

function clearCanvas() {
  c.width = 400;
}
</script>

<button onclick="clearCanvas()">Clear canvas</button>

</body>
</html>

Output should be:

How to Clear the canvas by setting the width attribute to 400px (using JavaScript)



Related Articles
What is HTML <!--...--> Tag HTML Tag
What is HTML <!DOCTYPE> Declaration HTML Tag
What is HTML Elements and Doctypes HTML Tag
What is HTML <a> Tag HTML Tag
What is HTML <abbr> Tag HTML Tag
What is HTML <acronym> Tag HTML Tag
What is HTML <address> Tag HTML Tag
What is HTML <applet> Tag HTML Tag
What is HTML <area> Tag HTML Tag
What is HTML <article> Tag HTML Tag
What is HTML <aside> Tag HTML Tag
What is HTML <audio> Tag HTML Tag
What is HTML <b> Tag HTML Tag
What is HTML <base> Tag HTML Tag
What is HTML <basefont> Tag HTML Tag
What is HTML <bdi> Tag HTML Tag
What is HTML <bdo> Tag HTML Tag
What is HTML <big> Tag HTML Tag
What is HTML <blockquote> Tag HTML Tag
What is HTML <body> Tag HTML Tag
What is HTML <br> Tag HTML Tag
What is HTML <button> Tag HTML Tag
What is HTML <canvas> Tag HTML Tag
What is HTML <caption> Tag HTML Tag
What is HTML <center> Tag HTML Tag
What is HTML <cite> Tag HTML Tag
What is HTML <code> Tag HTML Tag
What is HTML <col> Tag HTML Tag
How to create HTML <colgroup> Tag HTML Tag
What is HTML <data> Tag HTML Tag
What is HTML <datalist> Tag HTML Tag
What is HTML <dd> Tag HTML Tag
What is HTML <del> Tag HTML Tag
What is HTML <details> Tag HTML Tag
What is HTML <dfn> Tag HTML Tag
What is HTML <dialog> Tag HTML Tag
What is HTML <dir> Tag HTML Tag
What is HTML <div> Tag HTML Tag
What is HTML <dl> Tag HTML Tag
What is HTML <dt> Tag HTML Tag
What is HTML <em> Tag HTML Tag
What is HTML <embed> Tag HTML Tag
What is HTML <fieldset> Tag HTML Tag
What is HTML <figcaption> Tag HTML Tag
What is HTML <figure> Tag HTML Tag
What is HTML <font> Tag HTML Tag
What is HTML <footer> Tag HTML Tag
What is HTML <form> Tag HTML Tag
What is HTML <frame> Tag HTML Tag
What is HTML <frameset> Tag HTML Tag
What is HTML <h1> to <h6> Tags HTML Tag
What is HTML <head> Tag HTML Tag
What is HTML <header> Tag HTML Tag
What is HTML <hgroup> Tag HTML Tag
What is HTML <hr> Tag HTML Tag
What is HTML <html> Tag HTML Tag
What is HTML <i> Tag HTML Tag
What is HTML <iframe> Tag HTML Tag
What is HTML <img> Tag HTML Tag
What is HTML <input> Tag HTML Tag
What is HTML <ins> Tag HTML Tag
What is HTML <kbd> Tag HTML Tag
What is HTML <label> Tag HTML Tag
What is HTML <legend> Tag HTML Tag
What is HTML <li> Tag HTML Tag
What is HTML <link> Tag HTML Tag
What is HTML <main> Tag HTML Tag
What is HTML <map> Tag HTML Tag
What is HTML <mark> Tag HTML Tag
What is HTML <menu> Tag HTML Tag
What is HTML <meta> Tag HTML Tag

Single Articles
How to create HTML <canvas> TagHTML Tag
Tips and Notes for HTML <canvas> TagHTML Tag
What Type of Browsers will Support for HTML <canvas> TagHTML Tag
Attributes for HTML <canvas> TagHTML Tag
Another <canvas> exampleHTML Tag
Default CSS Settings For HTML <canvas> TagHTML Tag
How to add HTML <canvas> height AttributeHTML Tag
How to add HTML <canvas> width AttributeHTML Tag
How to Clear the canvas by setting the width attribute to 400px (using JavaScript)HTML Tag

Read Full::
HTML Tag
Category:
Web Tutorial
Sub Category:
HTML Tag
Uploaded:
7 months ago
Uploaded by:
Admin
Views:
1



Share on: