0

I'm following the canvas tutorial on w3schools and I've just discovered that there is a canvas function for drawing images.

This is the example that I was looking at:

window.onload = function() {
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    var img = document.getElementById("scream");
    ctx.drawImage(img, 10, 10);
};  

Well ... sience I've always used the <img> tag for showing an image in my page I asked myself what was the difference between this two ways of showing images.

Any idea ?

1 Answers1

2

You wouldn't, typically, use a canvas for displaying a single static image.

What you might do, is have a number of different image files, and compose on one canvas. For example, if you had images for "man" and "block" and "monster", and you rendered them onto a canvas 30 times a second, updating their relative positions based on, say, key events, then you've basically created a game.

If you just display a single static image, then there is no different from just using a <img> tag, and that is probably what you should do, because that makes it clearer what it is.

A <canvas> is for graphics you want to dynamically generate/manipulate using javascript.

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68