HTML Canvas
The HTML Canvas is one of the Amazing Features of the HTML Language. You can make an impressive website modal with help of HTML Canvas. You can draw graphics with Javascript using HTML Canvas. This feature is a part of HTML5. This allows us to make a 2D-like feature in an object on your website.
Now a day canvas is used by big companies like Apple, Microsoft, and many other tech giants.
In earlier days before the introduction of HTML Canvas. SVG (Scalable Vector Graphics) was used to draw 2d objects on websites. but that was not an efficient way to draw vector images. SVG was XML Based while HTML Canvas is a Bitmap-based vector graphics Format used with HTML and CSS.
It is supported by almost all browsers like Mozilla Firefox, Google Chrome, Internet Explorer, Opera, and Microsoft Edge. It is just like a container that is used with JavaScript.
You can draw some amazing 2D figures like circles, lines, ellipses, and rectangles.
Code in HTML and Js for drawing Rectangle
Here is the code to Draw A Rectangle on your website using HTML, CSS, and JavaScript (HTML Canvas)
<HTML>
<HEAD>
<STYLE>
#ELEMENT{BORDER:5px solid yellow;}
</STYLE>
</HEAD>
<BODY>
<CANVAS id = "ELEMENT" width = "150" height = "150"></CANVAS>
</BODY>
</HTML>
Code to Draw Circle in HTML Canvas
<html>
<head>
<style>
#element{border:5px solid Violet; border-radius:50px 50px;}
</style>
</head>
<body>
<canvas id ="element"width="150"height="150"></canvas>
</body>
</html>
Make a Triangle Using HTML and CSS
<html>
<head>
<style>
#triangle {
width: 0;
height: 0;
border-left:150px solid transparent;
border-right:150px solid transparent;
border-bottom:150px solid brown;
}
</style>
</head>
<body>
<canvas id="triangle"></canvas>
</body>
</html>
Draw Trapezium Using HTML
<html>
<head>
<style>
#trapezium {
height: 0;
border-left: 130px solid transparent;
border-right: 130px solid transparent;
border-bottom: 130px solid green;
}
</style>
</head>
<body>
<canvas id="trapezium"></canvas>
</body>
</html>
These are some common shapes that you can draw easily by using HTML Canvas and make them more colorful by using CSS elements.
METHOD OF HTML CANVAS TO DRAW SHAPES
- beginPath()
- Fill()
- Stroke()
- moveTo()
- clip()