-
Notifications
You must be signed in to change notification settings - Fork 13
/
helpers.js
61 lines (46 loc) · 1.59 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var TO_RADIANS = Math.PI/180;
function drawRotatedImage(image, x, y, angle) {
// save the current co-ordinate system
// before we screw with it
context.save();
// move to the middle of where we want to draw our image
context.translate(x, y);
// rotate around that point, converting our
// angle from degrees to radians
context.rotate(angle * TO_RADIANS);
// draw it up and to the left by half the width
// and height of the image
context.drawImage(image, -(image.width/2), -(image.height/2));
// and restore the co-ords to how they were when we began
context.restore();
}
function rotatePoint (coords, angle, distance) {
return {
x: Math.sin(angle * TO_RADIANS) * distance + coords.x,
y: Math.cos(angle * TO_RADIANS) * distance * -1 + coords.y,
};
}
function drawPoint (xy) {
context.fillRect(xy.x,xy.y,1,1);
}
function distance (from, to) {
var a = from.x > to.x ? from.x - to.x : to.x - from.x,
b = from.y > to.y ? from.y - to.y : to.y - from.y
;
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))
}
/**
* Provides requestAnimationFrame in a cross browser way.
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
if ( !window.requestAnimationFrame ) {
window.requestAnimationFrame = ( function() {
return window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
window.setTimeout( callback, 1000 / 60 );
};
}());
}