forked from bit101/CodingMath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
78 lines (64 loc) · 1.93 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
window.onload = function() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight,
start = {
x: 100,
y: 100
},
target = {},
change = {},
startTime,
duration = 1000;
drawCircle(start.x, start.y);
document.body.addEventListener("click", function(event) {
target.x = event.clientX;
target.y = event.clientY;
change.x = target.x - start.x;
change.y = target.y - start.y;
startTime = new Date();
update();
});
function update() {
context.clearRect(0, 0, width, height);
var time = new Date() - startTime;
if(time < duration) {
var x = easeInOutQuad(time, start.x, change.x, duration),
y = easeInOutQuad(time, start.y, change.y, duration);
drawCircle(x, y);
requestAnimationFrame(update);
}
else {
drawCircle(target.x, target.y);
start.x = target.x;
start.y = target.y;
}
}
// simple linear tweening - no easing
// t: current time, b: beginning value, c: change in value, d: duration
function linearTween(t, b, c, d) {
return c * t / d + b;
}
///////////// QUADRATIC EASING: t^2 ///////////////////
// quadratic easing in - accelerating from zero velocity
// t: current time, b: beginning value, c: change in value, d: duration
// t and d can be in frames or seconds/milliseconds
function easeInQuad(t, b, c, d) {
return c*(t/=d)*t + b;
};
// quadratic easing out - decelerating to zero velocity
function easeOutQuad(t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
};
// quadratic easing in/out - acceleration until halfway, then deceleration
function easeInOutQuad(t, b, c, d) {
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
};
function drawCircle(x, y) {
context.beginPath();
context.arc(x, y, 20, 0, Math.PI * 2, false);
context.fill();
}
};