×

HTML Canvas

HTML Canvas: Using JavaScript, the < canvas > tag in HTML is used for drawing graphics on web page. It can be used to draw routes, boxes, text, gradient, and picture adding. It does not include boundary and text by nature. The HTML canvas feature provides a bitmapped surface for HTML to work on. This is used for drawing web page graphics.

It is a low-level, procedural model which updates a bitmap and has no integrated scene. There are many techniques for drawing routes, lines, circles, text and inserting pictures in the canvas.

Syntax:

<canvas id = "script"> Data..<canvas>

Attribute:The tag to the canvas contains two attributes listed below:

Height:Uses this attribute to set canvas height.

Width:Uses this attribute to set canvas width.

For Example:

<!-- HTML code to illustrate canvas tag -->
<!DOCTYPE html>
<html>
<head>
<title>canvas Tag</title>
</head>
<body>
<canvas id = "Tutorialandexample" height = "250" width = "250"
style = "border:1px solid black">
</canvas>
<script>
var c = document.getElementById("Tutorialandexample");
var cx = c.getContext("2d");
cx.beginPath();
cx.arc(150, 150, 90, 0, 2 * Math.PI);
cx.stroke();
</script >
</body>
</html>

Output:

HTML Canvas

Example no. 2 draw rectangle using Html.

<!DOCTYPE html> 
<html> 
<head> 
<title>canvas Tag</title> 
</head> 
<body> <!-- canvas Tag starts here --> 
<canvas id = "Tutorialandexample" width = "400" height = "150" style = "border:1px solid black"> </canvas> <!-- canvas Tag ends here --> 
</body> 
</html>

Output:

HTML Canvas

Example no.3:

<!-- HTML code to illustrate canvas tag --> 
<!DOCTYPE html> 
<html> 
<head> 
<title>canvas Tag</title> 
</head> 
<body> <!-- canvas tag starts here --> 
<canvas id = "Tutorialandexample" width = "300" height = "300" style = "border:1px solid black"> </canvas> <!-- canvas tag ends here --> 
<script> var c=document.getElementById("Tutorialandexample"); 
var cx = c.getContext("2d"); 
vargrd = cx.createRadialGradient(100,                              100, 5, 100, 100, 100); grd.addColorStop(0, "blue"); 
grd.addColorStop(1, "red"); 
cx.fillStyle = grd; 
cx.fillRect(0, 0, 300, 300); </script> 
</body> 
</html>

Output:

HTML Canvas

Drawing Line on Canvas

You can use the following two approaches if you want to draw a straight line on the canvas.

MovingTo(x, y): It determines the starting point of the row.

LineTo(x, y): It determines the end point of a line.

Using the stroke method, you can draw a line with starting point (0,0) and the end point (250,150).

Code:

<canvas id="CanvasLine" width="250" height="150" style="border:1px solid #d3d3d3;">

Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c = document.getElementById("CanvasLine"); 
varcctx = c.getContext("2d"); 
ctx.moveTo(0,0); 
ctx.lineTo(150,50); 
ctx.stroke(); 
</script>

Output:

HTML Canvas

Draw colored box border

<body>
<canvas id="totn_canvas" width="225" height="225"></canvas>
<script>
var canvas = document.getElementById("totn_canvas");
varctx = canvas.getContext("2d");
ctx.fillStyle = "#4B6692";
ctx.fillRect(50, 50, 200, 200);
ctx.clearRect(90, 90, 120, 120);
ctx.strokeRect(100, 100, 100, 100);
</script>
</body>

Output:

Drawing Circle on Canvas

You may use the arc () method to draw a circle on the canvas:     

arc(x, y, r, start, stop) 

Using one of the ink () methods, such as stroke () or fill () to draw circle on HTML canvas.

Code:

<canvas id="myCircle" width="400" height="200" style="border:1px solid #d3d5d3;">

Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c =document.getElementById("myCircle");
varcctx = c.getContext("2d");
ctx.beginPath(); 
ctx.arc(200,70,60,0,1*Math.PI); 
ctx.stroke(); 
</script>

Output:

HTML Canvas

Related Topics

How to create a link with no underline in HTML

In HTML, an anchor tag (<a>) is a Styled Container tag, which means when a hyperlink is created using this, browsers, by default, display the link by applying some styles...

5 minutes read.

HTML Nested Table

HTML nested table refers to a table that is created inside another table. The table is in the form of rows and columns. From left to right are called horizontal rows,...

5 minutes read.

How to add JavaScript to HTML

JavaScript is one of the most important languages today. It is a high-level, interpreted, lightweight programming language that makes web pages more interactive. It is a language of the web....

5 minutes read.

HTML Ordered List

We use <ol> tag to make an ordered list in html. An ordered list is made to make a list of items in ordered form, ordered list can be either...

3 minutes read.

HTML <strike> tag

The HTML <strike> tag is used to specifies strikethrough text. The tag is deprecated now and <del> tag is used in HTML files instead of it. All the global and...

2 minutes read.

HTML <address> Tag

Hyper Text Markup Language, HTML is the best markup language for creating Web pages. HTML consists of a wide variety of elements. In all of these elements, one element is...

4 minutes read.

HTML <body> tag

HTML means Hyper Text Markup Language, which is most preferred for making Web pages. It explains how a Web page is put together. There are many different elements in HTML,...

3 minutes read.

HTML <strong> tag

The strong element in HTML is used to display the real significant information in bold so that the user or reader will understand it easily. Syntax: <p> <strong>………… </strong> </p> Example Here, we will demonstrate how to...

2 minutes read.

HTML Search Box

The HTML search box is also known as the search bar. It is an input text field that is used to search the content. If the users want to explore...

7 minutes read.

HTML <br> tag

HTML is referred to as Hyper Text Markup Language, which is the most popular language for creating Web pages. HTML explains the construction of a Web page. It contains a...

4 minutes read.

HTML Basic Tags

Heading Tags: This tag represents the document header. It is said to be a good practice if every document starts with Heading. Heading tags ranges from H1 to H6 i.e....

3 minutes read.

HTML Video

HTML VIDEO: HTML5 has introduced the <video> tag to embed video into the webpage directly without using the plug-ins.   Syntax: <video attributes >     <source src="address" type="video/mp4">     Your browser does not support the video tag.   </video> <video width="320" height="240" controls>     <source src="mountain.mp4" type="video/mp4">     Your browser does not support the video tag.   </video> Output: ← Prev ...

1 minute read.

HTML5 Button Tag

HTML Button Tag The < button > tag is used on the webpage to construct a clickable button inside the HTML. Within the < button>....</button > tag, we can place content...

2 minutes read.

HTML Hide Element

The HTML hide element is used to hide the element. We place the hidden attribute inside those elements which we want to hide. The hidden attribute is a Boolean attribute. The...

3 minutes read.

HTML5 Tags

HTML5 Tags A list of newly included tags is provided in HTML5. These HTML 5 tags (elements) provide a better structure for the document. This list shows description of all HTML...

3 minutes read.

HTML Layouts

HTML Layouts HTML templates provide a way for well-managed, well-structured responsive web pages to be organized. We could say that the HTML layout specifies how web pages can be set up....

4 minutes read.

HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our...

4 minutes read.

HTML5 Formatting

HTML Formatting HTML Formatting is a method of text formatting for a better appearance and look. HTML allows us to format text without using the CSS. HTML includes a lot of formatting tags....

6 minutes read.

Html &lt;center> Tag

The term HTML refers to Hyper Text Markup Language. The most widely used markup language for making web pages is HTML. A structure of web page is described in HTML....

3 minutes read.

HTML Anchor

HTML Anchor The HTML anchor tags define a hyperlink that links to other pages on one page. It can create hyperlinks to any web page and files, locations or any other...

3 minutes read.