HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

Understanding the HTML <canvas> Element

The <canvas> element in HTML is a powerful tool used to draw graphics on a web page via scripting (usually JavaScript). It can be used for rendering graphs, game graphics, art, or other visual images on the fly.

What is the <canvas> Element?

The <canvas> element creates a fixed-size drawing surface that exposes one or more rendering contexts, which can be used to create and manipulate the content shown. The most common context, the "2D context", provides functions to draw text, lines, boxes, circles, and more.

Basic Usage of <canvas>

To use the <canvas> element, you simply need to add it to your HTML document with a specified width and height:

<canvas id="exampleCanvas" width="200" height="100" style="border:1px solid #000000;">
    Your browser does not support the canvas element.
</canvas>

This code creates a canvas area on the page with a black border. If the browser does not support the <canvas> element, it will display the text "Your browser does not support the canvas element."

Example 1: Drawing a Rectangle

Here's how you can draw a simple rectangle:

<script>
var canvas = document.getElementById('exampleCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(0, 0, 150, 75);
</script>
Your browser does not support the canvas element.

This script gets the canvas element, retrieves the 2D drawing context, sets the fill color to red, and draws a rectangle starting from the top-left corner (0,0) to 150 pixels wide and 75 pixels high.

Example 2: Drawing a Circle

To draw a circle, you can use the arc method:

<script>
var canvasCircle = document.getElementById('circleCanvas');
var ctx = canvasCircle.getContext('2d');
ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
</script>
Your browser does not support the canvas element.

This script starts a new path, creates an arc which is effectively a circle centered at (95, 50) with a radius of 40, and then strokes (outlines) the path.

Example 3: Adding Text

You can also use canvas to draw text with the fillText method:

<script>
var canvasText = document.getElementById('textCanvas');
var ctx = canvasText.getContext('2d');
ctx.font = '16px Arial';
ctx.fillStyle = 'blue';
ctx.fillText('Hello, Canvas!', 10, 50);
</script>
Your browser does not support the canvas element.

This code sets the font style to 16px Arial, sets the fill color to blue, and places the text "Hello, Canvas!" at the coordinates (10, 50) on the canvas.

Conclusion

The <canvas> element is a versatile and essential tool for any web developer looking to add graphics and animations to their websites. With JavaScript, the possibilities for what can be achieved are nearly limitless, making it an invaluable asset for interactive and dynamic content creation.