Categories
HTML JavaScript Web development

HTML Canvas

If you want to start to learn JavaScript you need to know what HTML is.

The HTML <canvas> tag is used to draw graphics on the website.

The HTML <canvas> tag is used to draw graphics, on the fly, via JavaScript.

The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics.

Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

Canvas Examples:

Example1:

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

Note: Always specify an id attribute (to be referred to in a script), and a width and height attribute to define the size of the canvas.

To add a border, use the style attribute.

Here is an example of a basic, empty canvas:

<!DOCTYPE html>
<html>
<body>

<canvas id="canvas-example" width="200" height="200" style="border:1px solid #333;">
</canvas>

</body>
</html>

Output:


Example2 – draw a line:

<!DOCTYPE html>
<html>
<body>

<canvas id="exampleCanvas" width="200" height="200" style="border:1px solid #333;"></canvas>

<script>
var c = document.getElementById("exampleCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,200);
ctx.stroke();
</script>

</body>
</html>

Output:


To learn more about HTML Canvas Paths click here.

Example3 – draw a circle:

<!DOCTYPE html>
<html>
<body>

<canvas id="circleCanvas" width="200" height="200" style="border:1px solid #333;"></canvas>

<script>
var c = document.getElementById("circleCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(100,100,50,0,2*Math.PI);
ctx.stroke();
</script> 

</body>
</html>

Output:


To learn more about HTML Canvas Paths click here.

Example4 – draw a text:

<!DOCTYPE html>
<html>
<body>

<canvas id="textCanvas" width="250" height="100" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("textCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Have a good day!",8,60);
</script>

</body>
</html>

Output:


Example5 – draw linear gradient:

<!DOCTYPE html>
<html>
<body>

<canvas id="square-3" width="200" height="200" style="border:1px solid #333;">
</canvas>

<script>
var c = document.getElementById("square-3");
var ctx = c.getContext("2d");
var createGradient = ctx.createLinearGradient(0, 0, 150, 0);
createGradient.addColorStop(0, "#e76f51");
createGradient.addColorStop(0.5, "#f4a261");
createGradient.addColorStop(1, "#e9c46a");
ctx.fillStyle = createGradient;
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:


To learn more about HTML Canvas Gradients click here.

Example6 – draw an image:

<!DOCTYPE html>
<html>
<body>
<p>Image to use:</p>
<img id="dance" src="https://lenadesign.org/wp-content/uploads/2020/01/dance1-1.jpg" alt="Dancer" width="320" height="240">

<p>Canvas to fill:</p>
<canvas id="danceCanvas" width="640" height="480"
style="border:1px solid #333;"></canvas>

<p><button onclick="danceCanvas()">Click here</button></p>

<script>
function danceCanvas() {
  var c = document.getElementById("danceCanvas");
  var ctx = c.getContext("2d");
  var img = document.getElementById("dance");
  ctx.drawImage(img,0,0);
}
</script>
</body>
</html>

Output:

Image to use:

Dancer

Canvas to fill:

To read more about HTML Canvas drawImage() method click here.

Enjoy coding!