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

Commit 75633a6

Browse files
committed
Active tween in-sync improvements
- Selector instance, makes calling easier
1 parent 915080d commit 75633a6

File tree

8 files changed

+82
-59
lines changed

8 files changed

+82
-59
lines changed

.eslintrc

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"no-mixed-operators": ["error", {
8888
"groups": [
8989
["==", "!=", "===", "!==", ">", ">=", "<", "<="],
90-
["&&", "||"],
9190
["in", "instanceof"]
9291
],
9392
"allowSamePrecedence": true

API.md

+29-17
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,21 @@ Lightweight, effecient and modular ES6 version of tween.js
3333
```
3434

3535
* [TWEEN](#TWEEN) : <code>object</code>
36-
* [.lastTime](#TWEEN.lastTime)
3736
* [.Plugins](#TWEEN.Plugins) : <code>object</code>
3837
* [.Easing](#TWEEN.Easing) : <code>object</code>
3938
* [.Interpolation](#TWEEN.Interpolation) : <code>object</code>
39+
* [.now](#TWEEN.now)
4040
* [.add(tween)](#TWEEN.add)
4141
* [.onTick(fn)](#TWEEN.onTick)
4242
* [.autoPlay(state)](#TWEEN.autoPlay)
4343
* [.removeAll()](#TWEEN.removeAll)
4444
* [.get(tween)](#TWEEN.get)[<code>Tween</code>](#Tween)
4545
* [.has(tween)](#TWEEN.has) ⇒ <code>Boolean</code>
4646
* [.remove(tween)](#TWEEN.remove)
47+
* [.update(time, [preserve])](#TWEEN.update)
4748
* [.getTime()](#TWEEN.getTime) ⇒ <code>number</code> \| <code>Time</code>
4849
* [.isRunning()](#TWEEN.isRunning) ⇒ <code>Boolean</code>
4950

50-
<a name="TWEEN.lastTime"></a>
51-
52-
### TWEEN.lastTime
53-
Updates global tweens by given time
54-
55-
**Kind**: static property of [<code>TWEEN</code>](#TWEEN)
56-
57-
| Param | Type | Description |
58-
| --- | --- | --- |
59-
| time | <code>number</code> \| <code>Time</code> | Timestamp |
60-
| [preserve] | <code>Boolean</code> | Prevents tween to be removed after finish |
61-
62-
**Example**
63-
```js
64-
TWEEN.update(500)
65-
```
6651
<a name="TWEEN.Plugins"></a>
6752

6853
### TWEEN.Plugins : <code>object</code>
@@ -93,6 +78,17 @@ List of full Interpolation
9378
```js
9479
import {Interpolation, Tween} from 'es6-tween'let bezier = Interpolation.Beziernew Tween({x:0}).to({x:[0, 4, 8, 12, 15, 20, 30, 40, 20, 40, 10, 50]}, 1000).interpolation(bezier).start()
9580
```
81+
<a name="TWEEN.now"></a>
82+
83+
### TWEEN.now ⇒
84+
Get browser/Node.js current time-stamp
85+
86+
**Kind**: static constant of [<code>TWEEN</code>](#TWEEN)
87+
**Returns**: Normalised current time-stamp in milliseconds
88+
**Example**
89+
```js
90+
TWEEN.now()
91+
```
9692
<a name="TWEEN.add"></a>
9793

9894
### TWEEN.add(tween)
@@ -191,6 +187,22 @@ Removes tween from list
191187
```js
192188
TWEEN.remove(tween)
193189
```
190+
<a name="TWEEN.update"></a>
191+
192+
### TWEEN.update(time, [preserve])
193+
Updates global tweens by given time
194+
195+
**Kind**: static method of [<code>TWEEN</code>](#TWEEN)
196+
197+
| Param | Type | Description |
198+
| --- | --- | --- |
199+
| time | <code>number</code> \| <code>Time</code> | Timestamp |
200+
| [preserve] | <code>Boolean</code> | Prevents tween to be removed after finish |
201+
202+
**Example**
203+
```js
204+
TWEEN.update(500)
205+
```
194206
<a name="TWEEN.getTime"></a>
195207

196208
### TWEEN.getTime() ⇒ <code>number</code> \| <code>Time</code>

package.json

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

ts/NodeCache.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { assign } from './shim'
22

33
const Store = {}
44
export default function (node, tween) {
5-
if (!node || !node.nodeType || !tween) return tween
5+
if (!node || !node.nodeType) {
6+
return tween
7+
}
68
const ID = node.queueID || 'q_' + Date.now()
79
if (!node.queueID) {
810
node.queueID = ID
911
}
1012
if (Store[ID]) {
1113
if (tween) {
12-
Store[ID] = assign(Store[ID], tween)
14+
Store[ID] = tween//assign(Store[ID], tween)
1315
}
1416
return Store[ID]
1517
}

ts/Timeline.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { add, now, remove, getTime } from './core'
22
import PlaybackPosition from './PlaybackPosition'
33
import Tween, { EVENT_COMPLETE, EVENT_REPEAT, EVENT_REVERSE, EVENT_RS, EVENT_UPDATE } from './Tween'
4+
import Selector from './selector'
45

56
export const shuffle = (a) => {
67
let j
@@ -94,16 +95,18 @@ class Timeline extends Tween {
9495
* @static
9596
*/
9697
public fromTo(nodes, from, to, params) {
97-
if (Array.isArray(nodes)) {
98+
nodes = Selector(nodes, true)
99+
if (nodes && nodes.length) {
98100
if (this._defaultParams) {
99101
params = { ...this._defaultParams, ...params }
100102
}
101103
const position = params.label
102104
const offset = typeof position === 'number' ? position : this.position.parseLabel(typeof position !== 'undefined' ? position : 'afterLast', null)
103105
const mode = this.getTiming(params.mode, nodes, params, offset)
104-
nodes.map((node, i) => {
106+
for (let i = 0, node, len = nodes.length; i < len; i++) {
107+
node = nodes[i]
105108
this.add(Tween.fromTo(node, typeof from === 'function' ? from(i, nodes.length) : { ...from }, typeof to === 'function' ? to(i, nodes.length) : to, typeof params === 'function' ? params(i, nodes.length) : params), mode[i])
106-
})
109+
}
107110
}
108111
return this.start()
109112
}

ts/Tween.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import Easing from './Easing'
1212
import EventClass from './Event'
1313
import NodeCache from './NodeCache'
14+
import Selector from './selector'
1415

1516
Object.create = create
1617

@@ -121,16 +122,12 @@ class Tween extends EventClass {
121122
super()
122123

123124
this.id = _id++
124-
if (typeof node !== 'undefined' && !object && !node.nodeType) {
125+
if (!!node && typeof node === 'object' && !object && !node.nodeType) {
125126
object = this.object = node
126127
node = null
127-
} else if (typeof node !== 'undefined') {
128-
this.node = node
129-
if (typeof object === 'object') {
130-
object = this.object = NodeCache(node, object)
131-
} else {
132-
this.object = object
133-
}
128+
} else if (!!node && (node.nodeType || node.length || typeof node === 'string')) {
129+
node = this.node = Selector(node)
130+
object = this.object = NodeCache(node, object)
134131
}
135132
this._valuesEnd = null
136133
this._valuesFunc = null

ts/core.ts

+36-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,36 @@ import { cancelAnimationFrame, requestAnimationFrame, root } from './shim'
33

44
declare let process: any
55

6+
/**
7+
* Get browser/Node.js current time-stamp
8+
* @return Normalised current time-stamp in milliseconds
9+
* @memberof TWEEN
10+
* @example
11+
* TWEEN.now()
12+
*/
13+
const now: any = (function () {
14+
if (typeof (process) !== 'undefined' && process.hrtime !== undefined) {
15+
return function () {
16+
const time: number = process.hrtime()
17+
18+
// Convert [seconds, nanoseconds] to milliseconds.
19+
return time[0] * 1000 + time[1] / 1000000
20+
}
21+
// In a browser, use window.performance.now if it is available.
22+
} else if (root.performance !== undefined &&
23+
root.performance.now !== undefined) {
24+
// This must be bound, because directly assigning this function
25+
// leads to an invocation exception in Chrome.
26+
return root.performance.now.bind(root.performance)
27+
// Use Date.now if it is available.
28+
} else {
29+
const offset: number = root.performance && root.performance.timing && root.performance.timing.navigationStart ? root.performance.timing.navigationStart : Date.now()
30+
return function () {
31+
return Date.now() - offset
32+
}
33+
}
34+
}())
35+
636
/**
737
* Lightweight, effecient and modular ES6 version of tween.js
838
* @copyright 2017 @dalisoft and es6-tween contributors
@@ -18,6 +48,10 @@ let _autoPlay: boolean = false
1848
let _tick: Function
1949
const _ticker: Function = requestAnimationFrame
2050
const _stopTicker: Function = cancelAnimationFrame
51+
let lastTime: number = now()
52+
let delta: number = 0
53+
let timeDiff: number = 0
54+
let frameMs: number = 50 / 3
2155

2256
/**
2357
* Adds tween to list
@@ -34,6 +68,8 @@ const add = (tween: any): void => {
3468
if (_autoPlay && !isStarted) {
3569
_tick = _ticker(update)
3670
isStarted = true
71+
timeDiff += now() - lastTime
72+
lastTime = now()
3773
}
3874
}
3975

@@ -113,29 +149,6 @@ const remove = (tween: any): void => {
113149
}
114150
}
115151

116-
const now: any = (function () {
117-
if (typeof (process) !== 'undefined' && process.hrtime !== undefined) {
118-
return function () {
119-
const time: number = process.hrtime()
120-
121-
// Convert [seconds, nanoseconds] to milliseconds.
122-
return time[0] * 1000 + time[1] / 1000000
123-
}
124-
// In a browser, use window.performance.now if it is available.
125-
} else if (root.performance !== undefined &&
126-
root.performance.now !== undefined) {
127-
// This must be bound, because directly assigning this function
128-
// leads to an invocation exception in Chrome.
129-
return root.performance.now.bind(root.performance)
130-
// Use Date.now if it is available.
131-
} else {
132-
const offset: number = root.performance && root.performance.timing && root.performance.timing.navigationStart ? root.performance.timing.navigationStart : Date.now()
133-
return function () {
134-
return Date.now() - offset
135-
}
136-
}
137-
}())
138-
139152
/**
140153
* Updates global tweens by given time
141154
* @param {number|Time} time Timestamp
@@ -145,10 +158,6 @@ const now: any = (function () {
145158
* TWEEN.update(500)
146159
*/
147160

148-
let lastTime: number = now()
149-
let delta: number = 0
150-
let timeDiff: number = 0
151-
let frameMs: number = 50 / 3
152161
const update = (time: number, preserve?: boolean): boolean => {
153162
time = time !== undefined ? time : now()
154163
delta = time - lastTime

ts/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { add, onTick, autoPlay, get, getAll, has, isRunning, now, Plugins, remov
33
import Easing from './Easing'
44
import Timeline from './Timeline'
55
import Tween from './Tween'
6+
import Selector from './selector'
67
import './shim'
78
export { Plugins, Interpolator, onTick, has, get, getAll, removeAll, remove, add, now, update, autoPlay, isRunning, Tween, Easing, Timeline }

0 commit comments

Comments
 (0)