<canvas width="600" height="400"> </canvas>
<script>
var pantalla = document.querySelector("canvas");
var pincel = pantalla.getContext("2d");
var colores = ["blue", "red", "green", "yellow"];
var indiceColor = 0;
var x = 0;
var sentido = 1;
pincel.fillStyle = "grey" ;
pincel.fillRect(0,0,600,400);
function disenarCircunferencia(x,y,radio){
pincel.fillStyle = colores[indiceColor];
pincel.beginPath();
pincel.arc(x, y ,radio, 0, 2*Math.PI);
pincel.fill();
}
function limpiarPantalla(){
pincel.clearRect(0,0,600,400);
}
function actualizarPantalla(){
limpiarPantalla();
if(x > 600){
sentido = -1;
}
else if(x < 0){
sentido = 1;
}
disenarCircunferencia(x,20,10);
x = x + sentido;
}
function cambiarColor(){
indiceColor++;
while(indiceColor >=colores.length){
indiceColor = 0;
}
return false;
}
pantalla.oncontextmenu = cambiarColor;
setInterval(actualizarPantalla,10);
</script>