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

Commit 3dad0b5

Browse files
committed
* feat(string): now can input string as start value
* chore(performance): was improved by almost 25-30%, especialy for string, array, object tweening * chore(subtween): now can tween deep-object or arrays with best possible performance
1 parent 020780c commit 3dad0b5

File tree

6 files changed

+41
-79
lines changed

6 files changed

+41
-79
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ git reset --hard
122122

123123
## Features
124124

125-
* Does one thing and one thing only: tween properties
126-
* Takes care of CSS units (e.g. appending `px`)
125+
* Tweens everything you give them, string (numbers only), number, number of arrays, number of object, etc...
126+
* Can use CSS units (e.g. appending `px`)
127127
* Can interpolate colours (partially)
128128
* Easing functions are reusable outside of Tween
129129
* Can also use custom easing functions

rollup.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { minify } from 'uglify-js-harmony'
44

55
const { BUILD } = process.env
66

7-
const plugins = [ buble() ]
7+
const plugins = [ buble({
8+
objectAssign: 'Object.assign'
9+
}) ]
810

911
let moduleName = 'Tween'
1012
let destFile = 'dist/' + moduleName

src/dist/Composite.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import cloneTween from './clone';
2-
31
import Plugins from './Plugins';
42

53
export default class Composite {
@@ -61,9 +59,6 @@ export default class Composite {
6159
set object(obj) {
6260
return this.render(obj);
6361
}
64-
cloneLayer() {
65-
return cloneTween(this, {}, Composite)
66-
}
6762
appendTo(node) {
6863
node.appendChild(this.domNode);
6964
return this;

src/dist/Tween.js

+35-57
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
from './core';
88
import Easing from './Easing';
99
import Interpolation from './Interpolation';
10-
import cloneTween from './clone';
1110
import joinToString from './joinToString';
1211
import toNumber from './toNumber';
12+
import SubTween from './SubTween';
1313

1414
// Credits:
1515
// @jkroso for string parse library
@@ -19,6 +19,8 @@ const Number_Match_RegEx = /\s+|([A-Za-z?().,{}:""\[\]#]+)|([-+\/*%]+=)?([-+*\/%
1919
class Tween {
2020
constructor(object = {}, instate) {
2121

22+
this.isJoinToString = typeof object === "string" && Number_Match_RegEx.test(object);
23+
object = this.isJoinToString ? object.match(Number_Match_RegEx).map(toNumber) : object;
2224
this.object = object;
2325
this._valuesStart = Tween.createEmptyConst(object);
2426
this._valuesEnd = Tween.createEmptyConst(object);
@@ -200,7 +202,7 @@ class Tween {
200202
};
201203
this._valuesEnd = _vE;
202204
} else {
203-
this._valuesEnd = properties;
205+
this._valuesEnd = this.isJoinToString ? SubTween(this.object, properties.match(Number_Match_RegEx).map(toNumber)) : properties;
204206
}
205207

206208
if (typeof duration === "number") {
@@ -233,61 +235,22 @@ class Tween {
233235

234236
for (let property in _valuesEnd) {
235237

238+
property = object[+property] !== undefined ? +property : property;
239+
236240
if (typeof _valuesEnd[property] === "object") {
237-
if (Array.isArray(_valuesEnd[property])) {
238-
if (typeof object[property] === "number") {
239-
this._valuesEnd[property] = [object[property]].concat(_valuesEnd[property]);
240-
} else {
241-
let clonedTween = cloneTween(this, {
242-
object: object[property],
243-
_valuesEnd: _valuesEnd[property],
244-
_events: undefined
245-
})
246-
.start()
247-
.stop();
248-
249-
this._valuesEnd[property] = clonedTween;
250-
}
251-
} else {
252-
let clonedTween = cloneTween(this, {
253-
object: object[property],
254-
_valuesEnd: _valuesEnd[property],
255-
_events: undefined
256-
})
257-
.start()
258-
.stop();
259-
260-
this._valuesStart[property] = 1;
261-
this._valuesEnd[property] = clonedTween;
262-
}
241+
242+
this._valuesEnd[property] = SubTween(object[property], _valuesEnd[property]);
243+
263244
} else if (typeof _valuesEnd[property] === "string" && typeof object[property] === "string" && Number_Match_RegEx.test(object[property]) && Number_Match_RegEx.test(_valuesEnd[property])) {
264245

265246
let __get__Start = object[property].match(Number_Match_RegEx);
266247
__get__Start = __get__Start.map(toNumber);
267248
let __get__End = _valuesEnd[property].match(Number_Match_RegEx);
268249
__get__End = __get__End.map(toNumber);
269-
let clonedTween = cloneTween(this, {
270-
object: __get__Start,
271-
_valuesEnd: __get__End,
272-
_events: {}
273-
})
274-
.start()
275-
.stop();
276-
277-
clonedTween.join = true; // For string tweening
278-
this._valuesStart[property] = 1;
279-
this._valuesEnd[property] = clonedTween;
280250

281-
}
282-
283-
// If value presented as function,
284-
// we should convert to value again by passing function
285-
if (typeof object[property] === "function") {
286-
object[property] = this.object[property] = object[property](this);
287-
}
251+
this._valuesEnd[property] = SubTween(__get__Start, __get__End);
252+
this._valuesEnd[property].join = true;
288253

289-
if (typeof _valuesEnd[property] === "function") {
290-
this._valuesEnd[property] = _valuesEnd[property](this);
291254
}
292255

293256
// If `to()` specifies a property that doesn't exist in the source object,
@@ -410,7 +373,8 @@ class Tween {
410373
_duration,
411374
_valuesStart,
412375
_valuesEnd,
413-
object
376+
object,
377+
isJoinToString
414378
} = this;
415379

416380
let property;
@@ -436,6 +400,20 @@ class Tween {
436400

437401
value = _easingFunction(elapsed);
438402

403+
if (typeof _valuesEnd === "function") {
404+
405+
let get = _valuesEnd(value);
406+
407+
if (isJoinToString) {
408+
409+
get = joinToString(get);
410+
411+
}
412+
413+
object = get;
414+
415+
} else {
416+
439417
for (property in _valuesEnd) {
440418

441419
// Don't update properties that do not exist in the source object
@@ -446,19 +424,17 @@ class Tween {
446424
let start = _valuesStart[property];
447425
let end = _valuesEnd[property];
448426

449-
if (end instanceof Tween) {
427+
if (typeof end === "function") {
450428

451-
let getValue = end.get(time);
429+
let get = end(value);
452430

453-
if (end.join) {
431+
if (end.join) {
454432

455-
object[property] = joinToString(getValue);
433+
get = joinToString(get);
456434

457-
} else {
458-
459-
object[property] = getValue;
435+
}
460436

461-
}
437+
object[property] = get;
462438

463439
} else if (Array.isArray(end)) {
464440

@@ -482,6 +458,8 @@ class Tween {
482458

483459
}
484460

461+
}
462+
485463
this.emit('update', object, value, elapsed);
486464

487465
if (elapsed === 1 || (_reversed && elapsed === 0)) {

src/dist/clone.js

-13
This file was deleted.

src/dist/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ if (root.document) {
201201

202202
for ( let tween in _tweens ) {
203203

204-
tween._startTime += timeDiff
204+
_tweens[tween]._startTime += timeDiff
205205

206206
}
207207

0 commit comments

Comments
 (0)