-
Notifications
You must be signed in to change notification settings - Fork 238
/
piuTimeline.js
142 lines (138 loc) · 4.3 KB
/
piuTimeline.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
/*
* Copyright (c) 2016-2020 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
class TweenProperty @ "xs_TweenProperty_destructor" {
constructor(name, from, to) @ "xs_TweenProperty";
tween(target, fraction) @ "xs_TweenProperty_tween";
}
class TweenOnProperty @ "xs_TweenOnProperty_destructor" {
constructor(name, values) @ "xs_TweenOnProperty";
tween(target, fraction) @ "xs_TweenOnProperty_tween";
}
export class Tween {
constructor(target, properties, duration, easing = null, delay = 0) {
this.target = target;
this.properties = properties;
this.delay = delay;
this.duration = duration;
this.easing = easing;
this.time = undefined;
}
static from(target, fromProperties, duration, easing, delay) {
let properties = [], name
for (name in fromProperties) {
let from = fromProperties[name];
properties.push(new TweenProperty(name, from, target[name]));
}
return new Tween(target, properties, duration, easing, delay);
}
static on(target, onProperties, duration, easing, delay) {
let properties = [], name
for (name in onProperties) {
let values = onProperties[name];
properties.push(new TweenOnProperty(name, values));
}
return new Tween(target, properties, duration, easing, delay);
}
static to(target, toProperties, duration, easing, delay) {
let properties = [], name
for (name in toProperties) {
let to = toProperties[name];
properties.push(new TweenProperty(name, target[name], to));
}
return new Tween(target, properties, duration, easing, delay);
}
seekTo(time) {
let duration = this.duration;
time -= this.delay;
if (time < 0)
time = 0;
else if (time > duration)
time = duration;
if (this.time !== time) {
this.time = time;
let target = this.target;
let fraction = time / duration;
let easing = this.easing;
if (easing)
fraction = easing.call(Math, fraction);
let properties = this.properties;
let c = properties.length;
for (let i = 0; i < c; i++)
properties[i].tween(target, fraction)
}
}
}
Object.freeze(Tween.prototype);
export default class Timeline {
constructor() {
this.tweens = [];
this.delay = 0;
this.duration = 0;
this.time = undefined;
}
add(tween, when = this.duration) {
when += tween.delay;
tween.delay = when;
this.duration = Math.max(when + tween.duration, this.duration);
this.tweens.push(tween);
return this;
}
from(target, fromProperties, duration, easing = null, delay = 0, when = this.duration) {
when += delay;
this.duration = Math.max(when + duration, this.duration);
this.tweens.push(Tween.from(target, fromProperties, duration, easing, when));
return this;
}
on(target, onProperties, duration, easing = null, delay = 0, when = this.duration) {
when += delay;
this.duration = Math.max(when + duration, this.duration);
this.tweens.push(Tween.on(target, onProperties, duration, easing, when));
return this;
}
to(target, toProperties, duration, easing = null, delay = 0, when = this.duration) {
when += delay;
this.duration = Math.max(when + duration, this.duration);
this.tweens.push(Tween.to(target, toProperties, duration, easing, when));
return this;
}
seekTo(time) {
let duration = this.duration;
time -= this.delay;
if (time < 0)
time = 0;
else if (time > duration)
time = duration;
if (this.time !== time) {
this.time = time;
let tweens = this.tweens;
let c = tweens.length;
for (let i = 0; i < c; i++) {
tweens[i].seekTo(time);
}
}
}
get fraction() {
return this.time / this.duration;
}
set fraction(it) {
this.seekTo(it * this.duration);
}
}
Object.freeze(Timeline.prototype);