-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
139 lines (110 loc) · 3.36 KB
/
utils.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
var DEFAULT_DURATION = 400;
//
// If `requestAnimationFrame` is not available, we simulate it with a 60
// frames per second timeout.
//
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function (fn) {
setTimeout(fn, 1000 / 60);
};
}
//
// ## Function `transform`
//
// A function that takes another function and feeds it values from `0` to `1`
// over a specified duration. This is the most basic function everything
// else here is built upon. To do something useful, the supplied callback
// function usually has to do some basic math to calculate the *real* start
// and end values of a transformation.
//
// Please note that no **easing** is used here at all, so everything is just linear.
// The supplied function is responsible for applying any easing if it is needed.
//
function transform (fn, duration, then) {
var tStart = Date.now();
var end = 1;
//
// The parameters `duration` and `then` are both optional.
//
duration = typeof duration === "number" ? duration : DEFAULT_DURATION;
then = typeof duration === "function" ? duration : then;
then = typeof then === "function" ? then : function () {};
function loop () {
var tCurrent = Date.now() - tStart;
var value = end * ((tCurrent / (duration / 100)) / 100);
value = value > end ? end : value;
if (tCurrent > duration || value === end) {
fn(end);
then();
return;
}
fn(value);
requestAnimationFrame(loop);
}
loop();
}
function tween (start, end, fn, duration, then) {
var diff = Math.abs(start - end);
function down (v) {
fn(end + diff * (1 - v));
}
function up (v) {
fn(start + diff * v);
}
return transform(start < end ? up : down, duration, then);
}
function getHeight (element) {
var height;
//
// If an element is hidden, it needs to be made visible temporarily to calculate its real height.
//
if (window.getComputedStyle(element).display === "none") {
element.style.display = "block";
height = window.getComputedStyle(element).height;
element.style.display = "none";
}
else {
height = window.getComputedStyle(element).height;
}
return parseInt(height, 10);
}
function hasHeight (element) {
return getHeight(element) > 0;
}
function getOpacity (element) {
return (element.style.opacity === "" ? 1 : parseInt(element.style.opacity, 10));
}
function ensureIsPositive (v) {
return (v < 0 ? 0 : v);
}
function isHidden (element) {
return window.getComputedStyle(element).display === "none";
}
function isOpacityZero (element) {
return !(element.style.opacity === "" || element.style.opacity > 0);
}
function getThenArgument () {
return (
Array.prototype.find.call(arguments, function (arg) {
return typeof arg === "function";
}) ||
function () {}
);
}
function swing (t) {
return 0.5 - Math.cos(t * Math.PI) / 2;
}
module.exports = {
getHeight: getHeight,
getOpacity: getOpacity,
getThenArgument: getThenArgument,
ensureIsPositive: ensureIsPositive,
isHidden: isHidden,
isOpacityZero: isOpacityZero,
hasHeight: hasHeight,
transform: transform,
tween: tween,
easing: {
swing: swing
}
};