Coding

 

 

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>

 

All Canvas Animations

 

 

 

Recommended Reading

Amazon Best Seller

 

Everything is evolving; so is the layout of a text book. By uniquely presenting the rich contents of the book, the author has elevated positive user experience of reading a text book to a new level: the examples are easy to follow and rich in standard. Highly recommended for those who want to master JavaScript and JQuery.

Progressive Web Apps(PWA)


 

The significance of app stores is over; progressive web apps is the next big thing. They are just websites that makes the need of going through app stores and need of storing redundant. They work offline too. If you have a reasonable understanding of HTML, CSS and JavaScript, this is the book for you to learn in no time.

HTML5 Canvas Animations


 

David Geary, in this book, shows how to combine JavaScript and HTML5 Canvas to produce amazing animations; he has set aside a whole chapter to teach you the role of physics in animations. If you want an in-depth understanding about HTML5 Canvas animations, this is a must read.