-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathtransitions.js
207 lines (175 loc) · 4.71 KB
/
transitions.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
import { assign, noop } from './utils.js';
import { createElement } from './dom.js';
export function linear(t) {
return t;
}
export function generateRule(
a,
b,
delta,
duration,
ease,
fn
) {
var keyframes = '{\n';
for (var p = 0; p <= 1; p += 16.666 / duration) {
var t = a + delta * ease(p);
keyframes += p * 100 + '%{' + fn(t) + '}\n';
}
return keyframes + '100% {' + fn(b) + '}\n}';
}
// https://github.com/darkskyapp/string-hash/blob/master/index.js
export function hash(str) {
var hash = 5381;
var i = str.length;
while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
return hash >>> 0;
}
export function wrapTransition(component, node, fn, params, intro, outgroup) {
var obj = fn(node, params);
var duration = obj.duration || 300;
var ease = obj.easing || linear;
var cssText;
// TODO share <style> tag between all transitions?
if (obj.css && !transitionManager.stylesheet) {
var style = createElement('style');
document.head.appendChild(style);
transitionManager.stylesheet = style.sheet;
}
if (intro) {
if (obj.css && obj.delay) {
cssText = node.style.cssText;
node.style.cssText += obj.css(0);
}
if (obj.tick) obj.tick(0);
}
return {
t: intro ? 0 : 1,
running: false,
program: null,
pending: null,
run: function(intro, callback) {
var program = {
start: window.performance.now() + (obj.delay || 0),
intro: intro,
callback: callback
};
if (obj.delay) {
this.pending = program;
} else {
this.start(program);
}
if (!this.running) {
this.running = true;
transitionManager.add(this);
}
},
start: function(program) {
component.fire(program.intro ? 'intro.start' : 'outro.start', { node: node });
program.a = this.t;
program.b = program.intro ? 1 : 0;
program.delta = program.b - program.a;
program.duration = duration * Math.abs(program.b - program.a);
program.end = program.start + program.duration;
if (obj.css) {
if (obj.delay) node.style.cssText = cssText;
program.rule = generateRule(
program.a,
program.b,
program.delta,
program.duration,
ease,
obj.css
);
transitionManager.addRule(program.rule, program.name = '__svelte_' + hash(program.rule));
node.style.animation = (node.style.animation || '')
.split(', ')
.filter(function(anim) {
// when introing, discard old animations if there are any
return anim && (program.delta < 0 || !/__svelte/.test(anim));
})
.concat(program.name + ' ' + duration + 'ms linear 1 forwards')
.join(', ');
}
this.program = program;
this.pending = null;
},
update: function(now) {
var program = this.program;
if (!program) return;
var p = now - program.start;
this.t = program.a + program.delta * ease(p / program.duration);
if (obj.tick) obj.tick(this.t);
},
done: function() {
var program = this.program;
this.t = program.b;
if (obj.tick) obj.tick(this.t);
if (obj.css) transitionManager.deleteRule(node, program.name);
program.callback();
program = null;
this.running = !!this.pending;
},
abort: function() {
if (obj.tick) obj.tick(1);
if (obj.css) transitionManager.deleteRule(node, this.program.name);
this.program = this.pending = null;
this.running = false;
}
};
}
export var transitionManager = {
running: false,
transitions: [],
bound: null,
stylesheet: null,
activeRules: {},
add: function(transition) {
this.transitions.push(transition);
if (!this.running) {
this.running = true;
requestAnimationFrame(this.bound || (this.bound = this.next.bind(this)));
}
},
addRule: function(rule, name) {
if (!this.activeRules[name]) {
this.activeRules[name] = true;
this.stylesheet.insertRule('@keyframes ' + name + ' ' + rule, this.stylesheet.cssRules.length);
}
},
next: function() {
this.running = false;
var now = window.performance.now();
var i = this.transitions.length;
while (i--) {
var transition = this.transitions[i];
if (transition.program && now >= transition.program.end) {
transition.done();
}
if (transition.pending && now >= transition.pending.start) {
transition.start(transition.pending);
}
if (transition.running) {
transition.update(now);
this.running = true;
} else if (!transition.pending) {
this.transitions.splice(i, 1);
}
}
if (this.running) {
requestAnimationFrame(this.bound);
} else if (this.stylesheet) {
var i = this.stylesheet.cssRules.length;
while (i--) this.stylesheet.deleteRule(i);
this.activeRules = {};
}
},
deleteRule: function(node, name) {
node.style.animation = node.style.animation
.split(', ')
.filter(function(anim) {
return anim.slice(0, name.length) !== name;
})
.join(', ');
}
};