Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 52ee9b3

Browse files
committed
* fix = Timeline.js
* NEW ISSUE = Tests didn't pass ( i see it, when have time )
1 parent e75132d commit 52ee9b3

File tree

11 files changed

+132
-148
lines changed

11 files changed

+132
-148
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Starting at `v3`, we provide excluded plugins from core, so our core has lighter
3636

3737
* Demo #1 [Morphing SVG Shape + Cross-browser SVG Transform](https://codepen.io/dalisoft/pen/mMJmxX)
3838
* Demo #2 [Morphing SVG Shape](https://codepen.io/dalisoft/pen/BdLydv)
39+
* Demo #3 [Timeline](https://codepen.io/dalisoft/pen/mMRWdr)
3940

4041
## Installation
4142

examples/test.html

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, minimum-scale=1.0">
6-
<title>TWEEN.js</title>
6+
<title>es6-tween</title>
77
<style>
88
#container {
99
width: 200px;
@@ -33,10 +33,14 @@
3333
return Math.random() * (max - min) + min;
3434
}
3535

36+
function update (v, t) {
37+
v.el.style.left = (v.x | 0) + 'px';
38+
}
39+
3640
var nodes = [],
3741
c = document.getElementById("container"),
3842
easing = "quadraticInOut",
39-
ratio = 0.9,
43+
ratio = 0.8,
4044
wIh = (window.innerHeight || screen.height - 40) * ratio,
4145
wIw = window.innerWidth,
4246
hwiw = (wIw / 5) * ratio,
@@ -53,14 +57,12 @@
5357
tl = Math.floor(random(-200, 200));
5458
div.setAttribute("class", "line");
5559
div.style.top = [(Math.random() * wIh), "px"].join("");
56-
div.style.transform = "translate3d(" + l + "px, 0px, 0px)";
57-
//div.style.left = l + "px";
60+
//div.style.transform = "translate3d(" + l + "px, 0px, 0px)";
61+
div.style.left = l + "px";
5862
div.style.backgroundColor = bgC;
5963
var a = new TWEEN.Tween({ x: l, el: div }).to({
6064
x: tl
61-
}, 1000).easing(TWEEN.Easing.Circular.InOut).on('update', ({el, x}) => {
62-
el.style.transform = `translate3d(${x}px, 0px, 0px)`
63-
}).repeat(Infinity).yoyo(true).start();
65+
}, 1000).on('update', update).easing(TWEEN.Easing.Quadratic.InOut).repeat(Infinity).yoyo(true).start();
6466

6567
frag.appendChild(div);
6668

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "es6-tween",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "ES6 implementation of amazing tween.js",
55
"browser": "dist/Tween.min.js",
66
"cdn": "dist/Tween.min.js",

src/Tween.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { nextId, has, get, getAll, removeAll, remove, add, now, update, autoPlay, Plugins } from './dist/core'
1+
import { nextId, has, get, getAll, removeAll, remove, add, now, update, autoPlay, Plugins, isRunning } from './dist/core'
22
import Easing from './dist/Easing'
33
import Tween from './dist/Tween'
44
import Interpolation from './dist/Interpolation'
55
import Timeline from './dist/Timeline'
66
import SubTween from './dist/SubTween'
7-
export { Plugins, SubTween as Interpolator, nextId, has, get, getAll, removeAll, remove, add, now, update, autoPlay, Tween, Easing, Interpolation, Timeline }
7+
export { Plugins, SubTween as Interpolator, nextId, has, get, getAll, removeAll, remove, add, now, update, autoPlay, isRunning, Tween, Easing, Interpolation, Timeline }

src/dist/Event.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ export default class EventClass {
33
this._events = {}
44
}
55

6-
bind (scope) {
7-
this._bind = scope
8-
return this
9-
}
10-
116
on (event, callback) {
127
if (!this._events[event]) {
138
this._events[event] = []
@@ -22,10 +17,10 @@ export default class EventClass {
2217
this._events[event] = []
2318
}
2419

25-
let {_events, _bind} = this
20+
let {_events} = this
2621
let spliceIndex = _events[event].length
2722
this._events[event].push((...args) => {
28-
callback.apply(_bind, args)
23+
callback.apply(this, args)
2924
_events[event].splice(spliceIndex, 1)
3025
})
3126
return this
@@ -48,7 +43,7 @@ export default class EventClass {
4843
}
4944

5045
emit (event, arg1, arg2, arg3, arg4) {
51-
let {_events, _bind} = this
46+
let {_events} = this
5247

5348
if (event === undefined || !_events[event]) {
5449
return this
@@ -57,7 +52,7 @@ export default class EventClass {
5752
let _event = _events[event]
5853

5954
for (let i = 0, length = _event.length; i < length; i++) {
60-
_event[i].call(_bind, arg1, arg2, arg3, arg4)
55+
_event[i](arg1, arg2, arg3, arg4)
6156
}
6257
}
6358
}

src/dist/Interpolation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const Interpolation = {
5858
Utils: {
5959

6060
Linear (p0, p1, t) {
61-
return typeof p1 === 'function' ? p1(t) : (p1 - p0) * t + p0
61+
return typeof p0 === 'function' ? p0(t) : (p1 - p0) * t + p0
6262
},
6363

6464
Bernstein (n, i) {

src/dist/NodeCache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default function (node, tween) {
33
if (!node) return tween
44
if (Store[node]) {
55
if (tween) {
6-
return tween
6+
return Object.assign(Store[node], tween)
77
}
88
return Store[node]
99
}

src/dist/SubTween.js

-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ const SubTween = (start, end, roundv = 10000) => {
106106
return s
107107
}
108108
} else if (!Array.isArray(start) && Array.isArray(end)) {
109-
end.unshift(start)
110-
end.push(end[end.length - 1])
111109
return end.map((v, i) => SubTween(i === 0 ? start : end[i - 1], v))
112110
} else {
113111
return end

src/dist/Timeline.js

+25-47
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,29 @@
1-
import Tween from './Tween'
2-
import { add, now, nextId } from './core'
1+
import Tween, { EVENT_UPDATE, EVENT_RS, EVENT_REPEAT, EVENT_REVERSE, EVENT_COMPLETE } from './Tween'
2+
import { add, now } from './core'
3+
import PlaybackPosition from './PlaybackPosition'
34

5+
let _id = 0
46
class Timeline extends Tween {
57
constructor (params) {
68
super()
79
this._totalDuration = 0
810
this._startTime = now()
911
this._tweens = {}
10-
this._timing = []
1112
this._elapsed = 0
12-
this._id = nextId()
13-
this._labels = {}
13+
this._id = _id++
1414
this._defaultParams = params
15+
this.position = new PlaybackPosition()
16+
this.position.addLabel('afterLast', this._totalDuration)
17+
this.position.addLabel('afterInit', this._startTime)
1518

1619
return this
1720
}
1821

19-
setLabel (name, value) {
20-
this._labels[name] = this.parsePosition(0, value, 0)
22+
addLabel (name, offset) {
23+
this.position.addLabel(name, offset)
2124
return this
2225
}
2326

24-
parsePosition (startTime, input, total) {
25-
let position = startTime + total
26-
27-
if (typeof input === 'string') {
28-
for (let label in this._labels) {
29-
if (input.indexOf(label) === 0) {
30-
let inp = input.split(label)[1]
31-
32-
if (inp.length === 0 || (inp[0] === '+' || inp[0] === '-')) {
33-
position = this._labels[label] + startTime
34-
input = input.replace(label, '')
35-
}
36-
}
37-
}
38-
39-
if (input.indexOf('+') === 0 || input.indexOf('-') === 0) {
40-
position += parseFloat(input)
41-
}
42-
} else if (typeof input === 'number') {
43-
position += input
44-
}
45-
46-
return Math.max(0, position)
47-
}
48-
4927
map (fn) {
5028
for (let tween in this._tweens) {
5129
let _tween = this._tweens[tween]
@@ -62,7 +40,7 @@ class Timeline extends Tween {
6240
})
6341
return this
6442
} else if (typeof tween === 'object' && !(tween instanceof Tween)) {
65-
tween = new Tween(tween.from, tween)
43+
tween = new Tween(tween.from).to(tween.to, tween)
6644
}
6745

6846
let {
@@ -76,10 +54,12 @@ class Timeline extends Tween {
7654
}
7755
}
7856

79-
tween._startTime = this.parsePosition(now() + tween._delayTime, position, _totalDuration)
80-
this._timing[tween.id] = tween._startTime
81-
this._totalDuration = Math.max(_totalDuration, tween._duration + tween._startTime + tween._delayTime)
57+
const offset = typeof position === 'number' ? position : this.position.parseLabel(typeof position !== 'undefined' ? position : 'afterLast', null)
58+
tween._startTime = this._startTime
59+
tween._startTime += offset
60+
this._totalDuration = Math.max(_totalDuration, tween._startTime + tween._delayTime + tween._duration)
8261
this._tweens[tween.id] = tween
62+
this.position.setLabel('afterLast', this._totalDuration)
8363
return this
8464
}
8565

@@ -88,7 +68,7 @@ class Timeline extends Tween {
8868

8969
add(this)
9070

91-
return this.emit('restart')
71+
return this.emit(EVENT_RS)
9272
}
9373

9474
easing (easing) {
@@ -101,11 +81,7 @@ class Timeline extends Tween {
10181

10282
reverse () {
10383
this._reversed = !this._reversed
104-
this._timing = this._timing.reverse()
105-
for (let tween in this._tweens) {
106-
this._tweens[tween]._startTime = this._timing[+tween]
107-
}
108-
return this
84+
return this.emit(EVENT_REVERSE)
10985
}
11086

11187
update (time) {
@@ -117,7 +93,8 @@ class Timeline extends Tween {
11793
_startTime,
11894
_reversed,
11995
_yoyo,
120-
_repeat
96+
_repeat,
97+
_isFinite
12198
} = this
12299

123100
if (time < _startTime) {
@@ -142,15 +119,15 @@ class Timeline extends Tween {
142119
}
143120
}
144121

145-
this.emit('update', elapsed, timing)
122+
this.emit(EVENT_UPDATE, elapsed, timing)
146123

147124
if (elapsed === 1 || (_reversed && elapsed === 0)) {
148125
if (_repeat) {
149-
if (isFinite(_repeat)) {
126+
if (_isFinite) {
150127
this._repeat--
151128
}
152129

153-
this.emit(_reversed ? 'reverse' : 'repeat')
130+
this.emit(_reversed ? EVENT_REVERSE : EVENT_REPEAT)
154131

155132
if (_yoyo) {
156133
this.reverse()
@@ -169,11 +146,12 @@ class Timeline extends Tween {
169146
if (_tween.skip) {
170147
_tween.skip = false
171148
}
149+
_tween.reassignValues()
172150
}
173151

174152
return true
175153
} else {
176-
this.emit('complete')
154+
this.emit(EVENT_COMPLETE)
177155
this._repeat = this._r
178156

179157
for (let tween in _tweens) {

0 commit comments

Comments
 (0)