<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="800" height="600" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// Set up stars
var star1 = {x: 200, y: 200};
var star2 = {x: 600, y: 200};
// Set up planet
var planet = {x: 400, y: 300, radius: 10, speed: 2, angle1: 0, angle2: 0};
// Set up animation
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw stars
ctx.beginPath();
ctx.arc(star1.x, star1.y, 5, 0, 2 * Math.PI);
ctx.fillStyle = "yellow";
ctx.fill();
ctx.beginPath();
ctx.arc(star2.x, star2.y, 5, 0, 2 * Math.PI);
ctx.fillStyle = "yellow";
ctx.fill();
// Calculate new planet position
planet.angle1 += planet.speed;
planet.angle2 += planet.speed;
planet.x = star1.x + 200 * Math.cos(planet.angle1);
planet.y = star1.y + 100 * Math.sin(planet.angle1) + 100 * Math.cos(planet.angle2);
// Draw planet
ctx.beginPath();
ctx.arc(planet.x, planet.y, planet.radius, 0, 2 * Math.PI);
ctx.fillStyle = "blue";
ctx.fill();
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
This code creates a canvas element and sets up two stars at positions (200, 200) and (600, 200). A planet is also set up with a radius of 10 and a speed of 2. The animate function is called repeatedly using requestAnimationFrame to update the planet's position and draw it on the canvas. The planet orbits the first star in an elliptical path and the second star in a figure 8 pattern by using two angles and trigonometric functions. The stars and planet are drawn using the canvas context's arc and fill methods.