Skip to content

Commit

Permalink
corrected direction
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Fawcett authored and Steven Fawcett committed Dec 10, 2022
1 parent f7a43d6 commit ea8e3c2
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import * as Files from "./modules/file.js"
import * as Draw from "./modules/draw.js"

var HEADING=0; // DESIRED HEADING
var HEADING = 0; // DESIRED HEADING
var _HEADING = 0; // DIAL HEADING
var FPS = 50 ; // SPEED DIAL REFRESHS
var FPS = 25 ; // SPEED DIAL REFRESHS
var DIRECTION = 0;

export function SetHeading( hdg )
{
HEADING = 360 - hdg;
HEADING = 360 - mod(hdg , 360);
if( HEADING == 0 )
{
HEADING = 360
}
DIRECTION = direction()
}
export function Compass() {
var canvas = document.querySelector("canvas");
Expand All @@ -23,14 +29,12 @@ export function Compass() {

img.onload = function () {
setInterval(function () {
// Need a proper function to drive
if( HEADING != _HEADING )
_HEADING += 1;
rotate();

ctx.save(); //saves the state of canvas
ctx.clearRect(0, 0, canvas.width, canvas.height); //clear the canvas
ctx.translate(canvas.width/2, canvas.height/2); //let's translate
ctx.rotate(Math.PI / 180 * (_HEADING )); //increment the angle and rotate the image
ctx.rotate( radians( _HEADING ) );
ctx.translate(-(canvas.width/2), -(canvas.height/2)); //let's translate
Draw.drawImageScaled(img , ctx );
ctx.restore();
Expand All @@ -39,3 +43,37 @@ export function Compass() {
}, FPS);
}
}

function rotate()
{
if( Math.abs( HEADING % 360 )== Math.abs( _HEADING % 360 ) ) return ;

_HEADING = mod(_HEADING , 360);

_HEADING += DIRECTION;

//console.log( "Target Heading = " + _HEADING );
//console.log( "Requested Heading = " + HEADING);
}

function direction()
{
var a = ( _HEADING % 360 ) - HEADING
a = (a + 180) % 360


console.log( "Direction " + a);

return ( a < 0 ) ? -1 : 1;
}

function mod( a, n )
{
return a - Math.floor( a / n) * n ;

}

function radians( degree )
{
return degree * Math.PI / 180;
}

0 comments on commit ea8e3c2

Please sign in to comment.