Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Elements

0/1

HTML Attributes

0/1

HTML Headings

0/1

HTML Paragraphs

0/1

HTML Styles

0/1

HTML Formatting

0/1

HTML Quotation

0/1

HTML Comments

0/1

HTML Colors

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Block and Inline

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML - The Head Element

0/1

HTML Style Guide

0/1

HTML Entities

0/1

HTML Symbols

0/1
Text lesson

HTML Canvas

IMG_4259

The graphic on the left is generated using the <canvas> element. It features four components: a red rectangle, a gradient rectangle, a multicolor rectangle, and multicolor text.

What is HTML Canvas?

The HTML <canvas> element is employed to dynamically draw graphics using JavaScript.

While the <canvas> element itself is merely a container for graphics, you need JavaScript to render the graphics.

The canvas provides numerous methods for drawing paths, rectangles, circles, text, and incorporating images.

All major browsers support the canvas element.

Canvas Examples

A canvas is a rectangular region on an HTML page. By default, it has no border and contains no content.

The HTML code appears as follows:

<canvas id=”myCanvas” width=”200″ height=”100″></canvas>

Note: Always include an id attribute (to reference in a script) and specify width and height attributes to set the canvas size. To add a border, use the style attribute. 

Here is an example of a simple, empty canvas:

IMG_4260

 

Example

<canvas id=”myCanvas” width=”200″ height=”100″ style=”border:1px solid #000000;”>
</canvas>

Add a JavaScript

After creating the rectangular canvas area, you need to add JavaScript to perform the drawing.

Here are some examples:

Draw a Line

IMG_4253

Example

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.moveTo(00);
ctx.lineTo(200100);
ctx.stroke();
</script>

Draw a Circle

IMG_4254

Example

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.beginPath();
ctx.arc(95504002 * Math.PI);
ctx.stroke();
</script>

Draw a Text

IMG_4255

Example

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.font = “30px Arial”;
ctx.fillText(“Hello World”1050);
</script>

Stroke Text

IMG_4256

Example

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.font = “30px Arial”;
ctx.strokeText(“Hello World”1050);
</script>

Draw Linear Gradient

IMG_4257

Example

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);

// Create gradient
var grd = ctx.createLinearGradient(002000);
grd.addColorStop(0“red”);
grd.addColorStop(1“white”);

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(101015080);
</script>

Draw Circular Gradient

IMG_4258

Example

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);

// Create gradient
var grd = ctx.createRadialGradient(755059060100);
grd.addColorStop(0“red”);
grd.addColorStop(1“white”);

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(101015080);
</script>

Draw Image

<script>
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
var img = document.getElementById(“scream”);
ctx.drawImage(img, 1010);
</script>