### Creating 2-D Animations on Canvas - orbiting heavenly bodies

In this tutorial, you will learn:

• How to rotate the Earth around the Sun and the Moon around the Earth.
• Making the timings realistic while using the Window.setInterval and other related JavaScript functions.
• How to use the timer in sync with image sizes to make the animation run smoothly.
Finding a good book to master HTML5 can be very challenging: there are so many around - most with eye-catching titles and very complex substance.
Therefore, Vivax Solutions strongly recommends Core HTML5 Canvas for those who really want to delve into HTML5.
Please click the image to access Amazon:

#### Sun, earth and moon animation

In this animation, three images of The Sun, Earth and the Moon are used.

The Sun remains stationary; Earth orbits the sun in 365 days; The Moon orbits Earth in 28 days.

Using both sine and cosine functions of trigonometry, the positions of orbits are calculated.

When the animation progresses, the day, month and year are updated - giving them realistic values.

The Source Code for Interactive Solar System is as follows:

<script>
var canvas = document.getElementById('Canvas_Six');
var context_six = canvas.getContext('2d');
// x:angle; b:increment; m:months; d:day;
var y=new Date();
var year=y.getFullYear();
x = 0; b = 1; m = 1; d = 1;
// variables for the sun, earth and moon
var imgSn = new Image();
var imgErth = new Image();
var imgMn = new Image();
// sketch the static images in black
context_six.beginPath();
context_six.arc(200, 200, 50, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.fillStyle = 'black';
context_six.fill();
context_six.arc(340, 340, 20, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.fillStyle = 'black';
context_six.fill();
context_six.arc(370, 370, 10, 0, 2 * Math.PI, false);
context_six.closePath();
context_six.fillStyle = 'black';
context_six.fill();
// orbiting function
function orbitting() {
x = x + b; d = d + 1;
// the earth makes the full circle; year updated;
if (x == 360) { x = 0; year = year + 1; m = 0; }
// days and months are updated
if (x % 30 == 0) { m = m + 1; d = 1; }
context_six.clearRect(0, 0, 400, 400);
// image of the sun
context_six.beginPath();
context_six.drawImage(imgSn, 150, 150);
imgSn.src = '../images/sun.png';
// image of the moving earth
var x1 = 200 + 140 * Math.sin(x * Math.PI / 180);
var y1 = 200 + 130 * Math.cos(x * Math.PI / 180);
context_six.drawImage(imgErth, x1 - 20, y1 - 20);
imgErth.src = '../images/earth.png';
// image of the moving moon
var x2 = x1 + 50 * Math.sin(13 * x * Math.PI / 180);
var y2 = y1 + 50 * Math.cos(13 * x * Math.PI / 180);
context_six.drawImage(imgMn, x2 - 10, y2 - 10);
imgMn.src = '../images/moon.png';
// year, month and day updated
context_six.font = '12pt Calibri';
context_six.fillStyle = 'red';
context_six.fillText('Year: ' + year, 30, 30);
context_six.fillStyle = 'green';
context_six.fillText('Month: ' + m, 30, 50);
context_six.fillStyle = 'blue';
context_six.fillText('Day: ' + d, 30, 70);
}
</script>