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.

E.g.

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.


003600

 

The Code for Interactive Bearings is as follows:

 

<script>
{
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.beginPath();
context_six.moveTo(250, 200);
context_six.lineTo(250, 4);
context_six.lineWidth = 5;
context_six.strokeStyle = 'blue';
context_six.stroke()
context_six.closePath();
// Moving line
context_six.beginPath();
context_six.moveTo(250, 200);
context_six.lineTo(x, y);
context_six.lineWidth = 5;
context_six.strokeStyle = 'purple';
context_six.stroke()
context_six.closePath();
// Arc
context_six.beginPath();
context_six.arc(250, 200, 150, 1.5 * Math.PI, a);
context_six.strokeStyle = 'red';
context_six.stroke()
// 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';
}
}
</script>

 

All Canvas Animations

 

 

 

Recommended Reading

Amazon Best Seller

 

Advertisements

Amazon Best Seller

Advertisement