Creating 2-D Animations on Canvas - bearings

Bearings, measured from the north clockwise give the direction of a point from a point of reference. In the animation, the bearings of B from A is measured.

  • The concept of the bearings as a from navigation point of view.
  • How to change the bearing of a point interactively.
  • How to find the bearing of a given point.
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:

Interactive Bearings Tutorial

A bearing is defined as an angle measured clockwise from the north direction.

A bearing is usually expressed in three numbers; therefore, it is called 3-number-bearing.


300 is expressed as 0300.

1300 is expressed as 1300.

3300 is expressed as 3300.

Click the mouse along the slider and watch the updating below for the bearing.



The Code for Interactive Bearings is as follows:


window.setInterval('bear()', 1000)
function bear() {
var a = (Math.PI / 180) * eval(document.getElementById('ran').value);
var z;
var x = 250 + 150 * Math.cos(a);
var y = 200 + 150 * Math.sin(a);
var canvas = document.getElementById('Canvas_Six');
var context_six = canvas.getContext('2d');
context_six.clearRect(0, 0, 500, 400);
// Vertical line
context_six.moveTo(250, 200);
context_six.lineTo(250, 4);
context_six.lineWidth = 5;
context_six.strokeStyle = 'blue';
// Moving line
context_six.moveTo(250, 200);
context_six.lineTo(x, y);
context_six.lineWidth = 5;
context_six.strokeStyle = 'purple';
// Arc
context_six.arc(250, 200, 150, 1.5 * Math.PI, a);
context_six.strokeStyle = 'red';
// Text
context_six.font = 'bold 20pt Calibri';
context_six.fillStyle = 'red';
context_six.fillText('N', 220, 20);
context_six.font = 'bold 20pt Calibri';
context_six.fillStyle = 'blue';
context_six.fillText('A', 246, 220);
context_six.font = 'bold 20pt Calibri';
context_six.fillStyle = 'blue';
context_six.fillText('B', x, y);
context_six.font = 'bold 30pt Calibri';
context_six.fillStyle = 'blue';
context_six.fillText('^', 240, 25);
if (a + 90 <= 90) {
z = 'Bearings of B from A = 0' + (eval(document.getElementById('ran').value) + 90);
document.getElementById('p').innerHTML = z + '0';
else {
z = 'Bearings of B from A = ' + (eval(document.getElementById('ran').value) + 90);
document.getElementById('p').innerHTML = z + '0';





Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.


Stand Out - from the crowd


"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.



Recommended Reading


The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.