<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Star coordinates
var star1X = 100;
var star1Y = 100;
var star2X = 300;
var star2Y = 300;
// Planet starting position
var planetX = 200;
var planetY = 200;
// Planet movement speed
var planetSpeedX = 1;
var planetSpeedY = 1;
function animate() {
// Clear canvas
ctx.clearRect(0, 0, 400, 400);
// Draw stars
ctx.beginPath();
ctx.arc(star1X, star1Y, 5, 0, 2 * Math.PI);
ctx.fillStyle = "yellow";
ctx.fill();
ctx.beginPath();
ctx.arc(star2X, star2Y, 5, 0, 2 * Math.PI);
ctx.fillStyle = "yellow";
ctx.fill();
// Move planet in a figure 8 pattern
planetX += planetSpeedX;
planetY += planetSpeedY;
if(planetX > star1X + 50) planetSpeedX = -1;
if(planetX < star1X - 50) planetSpeedX = 1;
if(planetY > star1Y + 50) planetSpeedY = -1;
if(planetY < star1Y - 50) planetSpeedY = 1;
// Draw planet
ctx.beginPath();
ctx.arc(planetX, planetY, 5, 0, 2 * Math.PI);
ctx.fillStyle = "blue";
ctx.fill();
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>