I'm trying to learn how to learn basic animation using ONLY canvas with the setInterval function. I'm trying to draw a simple rectangle to the screen and move it to the right 1 pixel every 100 milliseconds. However when I do it paints over the previous rectangle. I called clearRect() but that doesn't seem to do anything.
How can I make this rectangle smoothly travel across the screen without leaving a trail? Also if there is a better way to do this rather than using clearRect() and translate() please do share.
var ctx = document.getElementById('mycanvas').getContext('2d');
var a = setInterval(draw,100);
var x = 50;
function draw()
{
ctx.clearRect(0,0,300,300);
ctx.translate(1,0);
ctx.rect(x,50,50,50);
ctx.stroke();
}
You can do it two different ways:
You can continue to use
rect()
andstroke()
, but you need to callbeginPath()
beforehand. When you call methods likerect()
, a list, called the "path," is kept of all of the shapes, or "subpaths," that you've created. Then, when you callstroke()
, the entire path is drawn. Thus, even if you clear the screen, all of the past rectangles are still remembered in the path, and drawn again.beginPath()
clears that list.Or, you can combine the
rect()
andstroke()
into one line, and not need to callbeginPath()
. That's because the rectangle is both created and drawn at the same time, and isn't put in the list.Either way, I advise incrementing
x
instead of usingtranslate()
, becausetranslate()
basically moves the imaginary "pen" that is drawing on the canvas. So if youtranslate(50, 50)
, and then you try to draw a rectangle at (0, 0) on the canvas, it will actually be at (50, 50).As Microsoft puts it on MSDN, "The translate method effectively remaps the (0,0) origin on a canvas."
If you repeatedly do that, it will become difficult to keep track of where you're actually drawing.