<!DOCTYPE html>
<html lang="es-CO">
<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>¡Ya que va, que vuelva!</title>
<meta name="description" content="Circulo en canvas que invierte su ruta cuando alcanza sus extremos.">
</head>
<body>
<canvas width="600" height="400"></canvas>
<script>
const screen = document.querySelector("canvas");
const pincel = screen.getContext("2d");
pincel.fillStyle = "lightgrey";
pincel.fillRect(0, 0, 600, 400);
function desineCircumference(x, y, radio) {
pincel.fillStyle = "blue";
pincel.beginPath();
pincel.arc(x, y, radio, 0, 2 * Math.PI);
pincel.fill();
}
function cleanScreen() {
pincel.clearRect(0, 0, 600, 400);
}
let xPosition = 0;
let reverseRoute = false;
function updateScreen() {
cleanScreen();
desineCircumference(xPosition, 20, 10);
if (!reverseRoute) {
xPosition++;
} else {
xPosition--;
}
defineRoute();
}
function defineRoute() {
if (xPosition == 590) {
reverseRoute = true;
return;
}
if (xPosition == 10) {
reverseRoute = false;
}
}
setInterval(updateScreen, 10);
</script>
</body>
</html>