Something like this should work:
var meter = new Image, needle = new Image; window.onload = function () { var c = document.getElementsByTagName('canvas')[0], ctx = c.getContext('2d'), x,
I use the abbreviated if / else here to determine whether to add 1 to x , subtract 1 or do nothing. Functionally, this is the same as:
if(xTarget > x){ x += step; }else if(xTarget < x){ x += -step; }else{ x += 0; }
But it is shorter and, in my opinion, as easy to read as you know what the abbreviation if ( ternary operator ) looks like.
Please keep in mind that this code assumes that x is an integer value (So, not float, only rounded int).
Cerbrus
source share