-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.js
216 lines (193 loc) · 5.43 KB
/
animation.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function (w) {
console.log("loading animation.js");
// set global Anim object
if (w.Anim) {
return;
}
// _anim global
const _anim = {};
// global consts
const _animating = {};
const axes = ["", "X", "Y", "Z"];
const degrees = 360.0;
const fps = 60.0;
const second = 1000.0;
const seconds = 1.0; // how long does the animation last?
const gravity = 0.196;
// delta's
const degDelta = degrees / (fps * seconds); // rotation
const tickDuration = seconds * second / fps; // ms
// console.log(degDelta, tickDuration);
// poor man's mutex
var _animateMutex = 0;
_anim.inc = function (k) {
if (!_animating[k]) {
// console.log("increment mutex");
_animating[k] = true;
_animateMutex++;
return true;
}
return false;
};
_anim.dec = function (k) {
if (_animating[k]) {
// console.log("decrement mutex");
_animating[k] = false;
_animateMutex--;
return true;
}
return false;
};
_anim.rotate = function (e, i) {
var k = e + i;
if (!_anim.inc(k)) return;
var deg = 0;
var dir = (random() >= 0.5) ? 1 : -1;
var axis = axes[random(axes.length)];
var rotateFn = function () {
e.style.transform = "rotate" + axis + "(" + deg + "deg)";
if (Math.abs(deg) < degrees) {
deg += degDelta * dir;
setTimeout(rotateFn, tickDuration);
} else {
_anim.dec(k);
e.style.transform = "";
}
};
rotateFn();
};
_anim.rotateChildren = function (e, selector) {
w.foreach(w.all(e, selector), _anim.rotate);
};
_anim.jump = function (e, i) {
var k = e + i;
if (!_anim.inc(k)) return;
var height = 0.0;
var scale = 1.0;
var minScale = 0.85;
var velocity = 3.0;
// jump states
const preJump = Object();
const jumping = Object();
const falling = Object();
const landing = Object();
const postJump = Object();
const jumpDone = Object();
const Duration = {
preJump: 0.15 * seconds,
jumping: 0.375 * seconds,
falling: 0.375 * seconds,
landing: 0.1 * seconds
};
var jumpState = preJump;
var jumpFn = function () {
// console.log(jumpState);
switch (jumpState) {
case preJump:
// console.log("preJump");
jumpState = function () {
if (scale > minScale) {
scale -= Duration.preJump / ((1 - minScale) * tickDuration);
// console.log(e, scale);
e.style.transform = "scaleY(" + scale + ")";
return preJump;
} else {
scale = 1.0;
e.style.transform = "scaleY(" + scale + ")";
return jumping;
}
}();
break;
case jumping:
// console.log("jumping");
jumpState = function () {
if (velocity > 0.0) {
velocity -= Duration.jumping / (gravity * tickDuration);
height -= velocity;
e.style.transform = "translateY(" + height + "px)";
return jumping
}
return falling;
}();
break;
case falling:
// console.log("falling");
jumpState = function () {
if (height < 0) {
velocity -= Duration.falling / (gravity * tickDuration);
height -= velocity;
e.style.transform = "translateY(" + height + "px)";
return falling;
}
return landing;
}();
break;
case landing:
// console.log("landing");
jumpState = function () {
if (scale > minScale) {
scale -= (Duration.landing / 2) / ((1 - minScale) * tickDuration);
e.style.transform = "scaleY(" + scale + ")";
return landing;
}
return postJump;
}();
break;
case postJump:
// console.log("postJump");
jumpState = function () {
if (scale < 1.0) {
scale += (Duration.landing / 2) / ((1 - minScale) * tickDuration);
e.style.transform = "scaleY(" + scale + ")";
return postJump;
} else {
scale = 1.0;
e.style.transform = "scaleY(" + scale + ")";
return jumpDone;
}
}();
break;
default:
// console.log("default");
e.style.transform = "";
_anim.dec(k)
return;
}
// call this again
// console.log("setTimeout(jumpFn, ", tickDuration, ");")
setTimeout(jumpFn, tickDuration);
};
// execute jump
jumpFn();
};
_anim.jumpChildren = (e, selector) => {
var children = w.all(e, selector);
var offset = second * seconds / children.length / 2;
var start = 0;
w.foreach(children, function (c, i) {
function doIt() {
_anim.jump(c, i);
}
setTimeout(doIt, start);
start += offset;
});
};
_anim.pickAnimation = function (e, selector) {
return function () {
if (_animateMutex === 0) {
switch (random(3)) {
case 0:
_anim.rotate(e, 0); break;
case 1:
_anim.rotateChildren(e, selector); break;
case 2:
_anim.jump(e, 0); break;
case 3:
_anim.jumpChildren(e, selector); break;
}
}
};
};
// set global object
w.Anim = _anim;
})(window);