Professional Documents
Culture Documents
51
51
5. Create a web page to model solar system using canvas element animation, where it contains sun,
earth and moon (all must be created using canvas shapes not images). Earth should revolve around
sun and moon should revolve around earth simultaneously. Sample screen shot below:
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
background-color:navy;
</style>
</head>
<body>
<script>
const sun = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 50,
color: 'yellow'
};
NAME:abhiram CLASS:BCA-B ROLLNUMBER:CA21371 DATE:28/05/2003
const earth = {
x: sun.x + 200,
y: sun.y,
radius: 20,
color: 'green',
angle: 0,
speed: 0.005
};
const moon = {
x: earth.x + 50,
y: earth.y,
radius: 10,
color: 'silver',
angle: 0,
speed: 0.045
};
function draw() {
ctx.beginPath();
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = sun.color;
ctx.fill();
ctx.beginPath();
ctx.fillStyle = earth.color;
ctx.fill();
ctx.beginPath();
ctx.fillStyle = moon.color;
ctx.fill();
earth.angle += earth.speed;
moon.angle += moon.speed;
requestAnimationFrame(draw);
draw();
</script>
</body>
</html>
NAME:abhiram CLASS:BCA-B ROLLNUMBER:CA21371 DATE:28/05/2003
OUTPUT: