<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mover Bolinha</title>
</head>
<body>
<canvas width="600" height="400"></canvas>
<script>
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
context.fillStyle = 'lightgray';
context.fillRect(0, 0, 600, 400);
var x = 20;
var y = 20;
// taxa de incremento
var taxa = 10;
function desenhaCirculo(x, y, raio) {
context.fillStyle = 'blue';
context.beginPath();
context.arc(x, y, raio, 0, 2 * Math.PI);
context.fill();
}
function limpaTela() {
context.clearRect(0, 0, 600, 400);
}
function atualizaTela() {
limpaTela();
desenhaCirculo(x, y, 10);
}
setInterval(atualizaTela, 20);
function leDoTeclado(evento) {
if(evento.key == 'ArrowRight')
x += taxa
else if(evento.key == 'ArrowLeft')
x -= taxa
else if(evento.key == 'ArrowUp')
y -= taxa
else if(evento.key == 'ArrowDown')
y += taxa
}
document.addEventListener('keydown', leDoTeclado)
</script>
</body>
</html>