×

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

HTML Tutorial

Introduction This HTML tutorial helps beginners and professionals to learn HTML easily. HTML is a widely used web technology for website development. It stands for Hyper Text Markup Language. It is mainly used to...

7 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.

HTML Text Format

Introduction HTML may format text in two different ways. The HTML tags define a text property to make it appear one way or another. A text element may also be formatted...

7 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.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

HTML Id Attribute

The HTML id attribute represents the unique identifier for an element of the HTML document. Each element has its unique identifier. The id attribute is a global attribute that can be...

12 minutes read.

HTML <section> tag

The section element in HTML is used to define sections in a document. Syntax of Section Tag: <body> <h1>……….</h1> <section> <h2>……….</h2> <p> …………………………</p> </section> <section> <h2>………..</h2> <p>…………………………. <p> </section> </body> Examples of Section Tag Example 1: In this example, we use the HTML "section" element...

3 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 minutes read.

HTML Youtube

<object> element: It embed the object within the HTML documents and embed plug-ins such as Java applets, PDF readers, Flash Players etc in web pages. Example: <object width="100" height="100" data="bookmark.swf"></object> <embed> element: It defines an embedded...

1 minute 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.

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

HTML <span> tag

The span tag in HTML is used as an inline container element to mark up a specific or special chunk of a text or a section of a page. If you...

2 minutes read.

HTML Images

HTML has <img> tag to display image on the web pages or web application. This tag is an empty tag i.e. we don't have to close this tag while using. SYNTAX: <img src="source address" attributes="alt, height, width"> Example: <html>   <body>   <center>Image tag Example</center>   <img src="https://www.tutorialandexample.com/resources/images/favicon.png"    alt="tutorialandexample"/>   </body>   </html> Output: Image...

1 minute read.

HTML Button Disabled

The HTML <button> element has many attributes, one of them is the disabled attribute. The button on which you apply the disabled attribute will not work, or we can say...

4 minutes read.

HTML <select> tag

This tag helps in making the drop-down list on the web page. This is highly used in HTML forms for taking the input from user. You can use the id attribute...

3 minutes read.

Div Tag in HTML

In HTML, ‘div’ is a tag that specifies division or section in HTML document. But before going in detail of it, let’s first understand about tags in HTML. The tag...

8 minutes read.

HTML Background Image

Inserting image in HTML An image (technically digital image) in HTML or even in general technical aspects, is a binary representation of visual information. By visual information we mean, in the...

8 minutes read.

HTML Unordered List

In HTML, we have <ul> tag which defines the unordered list. And it is also known as bulleted list, because as we know it is not ordered list therefore it...

2 minutes read.

HTML Headings

The titles and subtitles of our webpages are known as HTML headings. Let’s not talk about webpages but giving heading to almost everything like normal paragraphs, articles or let it...

2 minutes read.