-1
How do you make a small animation in Javascript? I tried to do so but it didn’t work:
var tela = document.querySelector('canvas');
var pincel = tela.getContext('2d');
pincel.fillStyle = 'lightgray';
pincel.fillRect(0, 0, 600, 400);
function desenhaCirculo(x, y, raio) {
pincel.fillStyle = 'blue';
pincel.beginPath();
pincel.arc(x, y, raio, 0, 2 * Math.PI);
pincel.fill();
}
function limpaTela() {
pincel.clearRect(0, 0, 600, 400);
}
var x = 20;
var sentido = 1;
function atualizaTela() {
if( x > 580) {
sentido = -1;
} else if (x < 20) {
sentido = 1;
}
desenhaCirculo(x, 20, 10);
x = x + sentido;
}
setInterval(atualizaTela, 1);
<canvas width="600" height="400"></canvas>