-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine-helpers.js
451 lines (408 loc) · 12.7 KB
/
engine-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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @fileoverview Doodle game engine: All sorts of helper functions, polyfills,
* abstractions that are not unique to our engine and oftentimes
* indeed actually just come from elsewhere.
*
* @author sfdimino@google.com (Sophia Foster-Dimino) – graphics/animation
* @author mwichary@google.com (Marcin Wichary) – code
* @author jdtang@google.com (Jonathan Tang)
* @author khom@google.com (Kristopher Hom)
*/
/**
* Whether a given item is an array or not.
* @param {*} item A JavaScript item.
* @return {boolean} Whether it’s an array (true) or not (false).
*/
engine.isArray = function(item) {
return item.constructor == Array;
};
/**
* Whether a given item is an object or not.
* @param {*} item A JavaScript item.
* @return {boolean} Whether it’s an object (true) or not (false).
*/
engine.isObject = function(item) {
var type = typeof item;
return type == 'object' || type == 'array' || type == 'function';
};
/**
* Whether a given item is defined or not.
* @param {*} item A JavaScript item.
* @return {boolean} Whether it’s defined (true) or not (false).
*/
engine.isDef = function(item) {
return typeof item != 'undefined';
};
/**
* Make a copy of an array.
* @param {Array} array An array to be copied.
* @return {Array} A shiny new, free copy. (Free as in beer).
*/
engine.duplicateArray = function(array) {
var newArray = [];
for (var i in array) {
newArray.push(array[i]);
}
return newArray;
};
/**
* Partially applies this function to a particular 'this object' and zero or
* more arguments. The result is a new function with some arguments of the
* first function pre-filled and the value of |this| 'pre-specified'.
*
* Remaining arguments specified at call-time are appended to the pre-
* specified ones.
*
* @param {Function} fn A function to partially apply.
* @param {Object|undefined} selfObj Specifies the object which |this| should
* point to when the function is run.
* @param {...*} var_args Additional arguments that are partially
* applied to the function.
* @return {!Function} A partially-applied form of the function bind() was
* invoked as a method of.
* @suppress {deprecated} See above.
*/
engine.bind = function(fn, selfObj, var_args) {
if (!fn) {
throw new Error();
}
if (arguments.length > 2) {
var boundArgs = Array.prototype.slice.call(arguments, 2);
return function() {
// Prepend the bound arguments to the current arguments.
var newArgs = Array.prototype.slice.call(arguments);
Array.prototype.unshift.apply(newArgs, boundArgs);
return fn.apply(selfObj, newArgs);
};
} else {
return function() {
return fn.apply(selfObj, arguments);
};
}
};
/**
* Generates a random value from 0 and 1 and reports whether it’s below
* a given threshold of probability. In other worlds, running this
* function with a probability of .5 simulates a coin toss.
* @param {Object} params
* - {number} .probability A value from 0 to 1.
* @return {boolean} Whether the random number generated was below the value.
*/
engine.probability = function(params) {
return Math.random() < params.probability;
};
/**
* Picks a random value between a given minimum and maximum.
* @param {Object} params
* - {number} .min A minimum.
* - {number} .max A maximum.
* @return {number} The random value.
*/
engine.rangeRand = function(params) {
return Math.random() * (params.max - params.min) + params.min;
};
/**
* Picks a random integer number between a given minimum and maximum.
* @param {Object} params
* - {number} .min A minimum.
* - {number} .max A maximum.
* @return {number} The random integer value.
*/
engine.intRangeRand = function(params) {
return Math.floor(Math.random() *
(params.max - params.min + 1) + params.min);
};
/**
* Picks a random item from a given set (array) of items.
* @param {Object} params
* - {Array} .set An array of items to choose.
* @return {*} One item from that set.
*/
engine.setRand = function(params) {
return params.set[Math.floor(Math.random() * params.set.length)];
};
/**
* A modulo function, necessary because there are differences in how
* browsers support negative modulus. This is otherwise equivalent
* to the native (val $PERC$ mod).
* @param {Object} params
* - {number} .val Value.
* - {number} .mod Modulus.
* @return {number} val modulo mod.
*/
engine.modulo = function(params) {
return params.val - Math.floor(params.val / params.mod) * params.mod;
};
/**
* Linear easing mapping function.
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Linear easing of that position.
*/
engine.linear = function(t) {
return t;
};
/**
* Easing out mapping function.
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Easing out of that position.
*/
engine.easeOut = function(t) {
return 1 - Math.pow(1 - t, 3);
};
/**
* Easing in mapping function.
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Easing in of that position.
*/
engine.easeIn = function(t) {
return t * t * t;
};
/**
* Easing in/out mapping function.
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Easing in/out of that position.
*/
engine.easeInOut = function(t) {
return 3 * t * t - 2 * t * t * t;
};
/**
* A helper mapping function used by back mapping functions.
* @param {number} x Coefficient.
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Easing of that position.
*/
engine.backHelper = function(x, t) {
return Math.pow(t, 2) * ((x + 1) * t - x);
};
/**
* Back easing in/out mapping function (back = a little overshoot at the end).
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Back easing in/out of that position.
*/
engine.backEaseInOut = function(t) {
if (t <= .5) {
return engine.backHelper(1.618, 2 * t) / 2;
} else {
return (2 - engine.backHelper(1.618, 2 * (1 - t))) / 2;
}
};
/**
* Strong back easing in/out mapping function (even more overshoot).
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Strong back easing in/out of that position.
*/
engine.strongBackEaseOut = function(t) {
if (t <= .5) {
return engine.easeOut(t) / 2;
} else {
return (2 - engine.backHelper(10, 2 * (1 - t))) / 2;
}
};
/**
* Fall mapping function (rudely implements bounce as something falls to
* the ground).
* @param {number} t Position in sequence (0 to 1).
* @return {number} t Fall mapping of that position.
*/
engine.fall = function(t) {
if (t < .82) {
return engine.easeIn(t / .82);
} else if (t < .88) {
return 1 - engine.easeInOut((t - .82) / .06) * .05;
} else if (t < .93) {
return .95 + engine.easeInOut((t - .88) / .05) * .05;
} else if (t < .97) {
return 1 - engine.easeInOut((t - .93) / .04) * .02;
} else {
return .98 + engine.easeInOut((t - .97) / .03) * .02;
}
};
/**
* Get a position of an element vis-a-vis the viewport.
* @param {Object} params
* - {el} .el DOM Element.
* @return {!Object} Object with coordinates as .left and .top.
*/
engine.getElPagePos = function(params) {
var el = params.el;
var pos = { left: 0, top: 0 };
while (el) {
pos.left += el.offsetLeft;
pos.top += el.offsetTop;
el = el.offsetParent;
}
return pos;
};
/**
* Add an event listener.
* @param {Object} params
* - {element} .el Element to add an event listener to.
* - {string} .type Type of an event (e.g. "click").
* - {function()} .handler Event handler.
*/
engine.addEventListener = function(params) {
engine.listeners.push(params);
if (window.addEventListener) {
params.el.addEventListener(params.type, params.handler, false);
} else {
params.el.attachEvent('on' + params.type, params.handler);
}
};
/**
* Remove an event listener.
* @param {Object} params
* - {element} .el Element to remove an event listener from.
* - {string} .type Type of an event (e.g. "click").
* - {function()} .handler Event handler.
*/
engine.removeEventListener = function(params) {
if (window.removeEventListener) {
params.el.removeEventListener(params.type, params.handler, false);
} else {
params.el.detachEvent('on' + params.type, params.handler);
}
};
/**
* Removes all listeners added since the last time removeAllEventListeners was
* called.
*/
engine.removeAllEventListeners = function() {
var params;
while (params = engine.listeners.pop()) {
engine.removeEventListener.call(null, params);
}
};
/**
* Get an event in a cross-browser way (as passed in modern browsers, from
* window.event on IE), and save the element target as event.targetEl.
* @param {Object} params
* - {Event} .event Event if passed from a handler.
* @return {Event} An event.
*/
engine.getDomEvent = function(params) {
var event = window.event || params.event;
event.targetEl = event.target ? event.target : event.srcElement;
return event;
};
/**
* Prevent the default handler for an event, in a cross-browser way.
* @param {Object} params
* - {Event} .event Event if passed from a handler.
*/
engine.preventDefaultEvent = function(params) {
if (params.event.preventDefault) {
params.event.preventDefault();
} else {
params.event.returnValue = false;
}
};
/**
* Stop the propagation of an event, in a cross-browser way.
* @param {Object} params
* - {Event} .event Event if passed from a handler.
*/
engine.stopPropagationEvent = function(params) {
if (params.event.stopPropagation) {
params.event.stopPropagation();
} else {
params.event.cancelBubble = true;
}
};
/**
* Find elements by tag or class name.
* @param {Object} params
* - {element} .el Root element (optional, defaults to document.body).
* - {string} .className Class name.
* - {string} .tagName Tag name.
* @return {Array} Element array.
*/
engine.getDomElements = function(params) {
if (params.el) {
var el = params.el;
} else {
var el = document.body;
}
if (document.querySelectorAll) {
var query = '';
if (params.tagName) {
query += params.tagName;
}
if (params.className) {
query += '.' + params.className;
}
return el.querySelectorAll(query);
} else if (document.getElementsByClassName) {
if (params.tagName && params.className) {
var els = el.getElementsByClassName(params.className);
var newEls = [];
for (var i = 0, el; el = els[i]; i++) {
if (el.tagName.toLowerCase() == params.tagName) {
newEls.push(el);
}
}
return newEls;
} else if (params.tagName) {
return el.getElementsByTagName(params.tagName);
} else if (params.className) {
return el.getElementsByClassName(params.className);
}
} else {
if (params.className) {
if (params.tagName) {
var els = el.getElementsByTagName(params.tagName);
} else {
var els = el.getElementsByTagName('*');
}
var regExp = new RegExp('(^|\ )' + params.className + '(\ |$)');
var newEls = [];
for (var i = 0, el; el = els[i]; i++) {
if (el.className.match(regExp)) {
newEls.push(el);
}
}
return newEls;
} else if (params.tagName) {
return el.getElementsByTagName(params.tagName);
}
}
};
/**
* Set a CSS for a given element to result in an empty/no mouse pointer.
* Doing it cross-browser is a bit tricky.
* @param {Object} params
* - {element} .el Element to change the mouse pointer style on.
*/
engine.setEmptyCssCursor = function(params) {
params.el.style.cursor = 'url(' + engine.EMPTY_MOUSE_POINTER_URL + '), auto';
if (!engine.features.ie8OrLower) {
params.el.style.cursor = 'none';
}
};
/**
* Wrapper/polyfill around request animation frame that uses the native
* support, or falls back to setTimeout if a native function is not
* available.
* @param {function()} callback Callback function.
*/
engine.requestAnimFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, engine.REQUEST_ANIM_FRAME_FALLBACK_DELAY);
};