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

Commit 9c42b73

Browse files
committed
* Supporting complex tweening
* Fix `TWEEN.update` auto-call
1 parent 507901c commit 9c42b73

File tree

5 files changed

+40
-88
lines changed

5 files changed

+40
-88
lines changed

API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ Set easing
937937

938938
| Param | Type | Description |
939939
| --- | --- | --- |
940-
| _easingFunction | <code>function</code> | Easing function in non-reverse direction |
940+
| _easingFunction | <code>function</code> | Easing function, applies in non-reverse direction if Tween#yoyo second argument is applied |
941941

942942
**Example**
943943
```js

examples/test.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
<div id="container"></div>
2727

28-
<script src="../dist/Tween.js"></script>
29-
<script src="../../es6-tween-plugins/renderer/index.js"></script>
28+
<script src="../full/Tween.js"></script>
29+
<script src="../../es6-tween-plugins/renderer/render.js"></script>
3030
<script !src="../../es6-tween-plugins/transform/transform.js"></script>
3131
<script>
3232
TWEEN.autoPlay(true);
@@ -62,10 +62,10 @@
6262
tl = Math.floor(random(-200, 200));
6363
div.setAttribute("class", "line");
6464
div.style.top = [(Math.random() * wIh), "px"].join("");
65-
div.style.left = l + 'px';
66-
//div.style.transform = "translate3d(" + l + "px, 0px, 0px)";
65+
//div.style.left = l + 'px';
66+
div.style.transform = "translate3d(" + l + "px, 0px, 0px)";
6767
div.style.backgroundColor = bgC;
68-
var a = new TWEEN.Tween(div, { left : l }).to({ left: tl }, 1000).easing(TWEEN.Easing.Quadratic.InOut).repeat(Infinity).yoyo(true).start();
68+
var a = new TWEEN.Tween(div, { x : l }).to({ x: tl }, 1000).easing(TWEEN.Easing.Quadratic.InOut).repeat(Infinity).yoyo(true).start();
6969

7070
frag.appendChild(div);
7171

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "es6-tween",
3-
"version": "3.8.9",
3+
"version": "3.8.11",
44
"description": "ES6 implementation of amazing tween.js",
55
"browser": "full/Tween.min.js",
66
"cdn": "full/Tween.min.js",
@@ -23,8 +23,8 @@
2323
"lint": "eslint --fix src",
2424
"test": "npm run lint && ava --verbose",
2525
"prepublish": "npm run prepare && npm run test && npm run doc && npm run doc-md",
26-
"doc": "tsc && jsdoc --tutorials guide_notes --readme README.md --configure jsdoc.json --verbose",
27-
"doc-md": "tsc && jsdoc2md src/** > API.md"
26+
"doc": "jsdoc --tutorials guide_notes --readme README.md --configure jsdoc.json --verbose",
27+
"doc-md": "jsdoc2md src/** > API.md"
2828
},
2929
"repository": {
3030
"type": "git",
@@ -62,6 +62,6 @@
6262
"uglify-es": "latest"
6363
},
6464
"dependencies": {
65-
"intertween": "0.0.17"
65+
"intertween": "0.0.25"
6666
}
6767
}

ts/Tween.ts

+26-76
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export interface RenderType {
4949
class Tween extends EventClass {
5050
public id: number
5151
public object: Object
52-
public _valuesStart: Object
53-
public _valuesFunc: Object
54-
public _valuesEnd: Object
52+
public _valuesEnd: any
5553
public _duration: number
5654
public _easingFunction: Function
5755
public _easingReverse: Function
@@ -69,7 +67,6 @@ class Tween extends EventClass {
6967
public _isFinite: boolean
7068
public _isPlaying: boolean
7169
public _elapsed: number
72-
public isArr: boolean
7370
private _onStartCallbackFired: boolean
7471
private _rendered: boolean
7572
private __render: RenderType
@@ -133,10 +130,7 @@ class Tween extends EventClass {
133130
this.object = object
134131
}
135132
}
136-
const isArr = this.isArr = Array.isArray(object)
137-
this._valuesStart = isArr ? [] : {}
138133
this._valuesEnd = null
139-
this._valuesFunc = {}
140134

141135
this._duration = 1000
142136
this._easingFunction = defaultEasing
@@ -313,53 +307,33 @@ class Tween extends EventClass {
313307
*/
314308
public render() {
315309
if (this._rendered) {
316-
return this
310+
return this;
317311
}
318-
319-
let {
320-
_valuesEnd,
321-
_valuesFunc,
322-
_valuesStart,
323-
object,
324-
Renderer,
325-
node,
326-
InitialValues
327-
} = this
328-
312+
let { _valuesEnd, object, Renderer, node, InitialValues, _easingFunction } = this;
329313
if (node && InitialValues) {
330314
if (!object) {
331-
object = this.object = NodeCache(node, InitialValues(node, _valuesEnd))
332-
} else if (!_valuesEnd) {
333-
_valuesEnd = this._valuesEnd = InitialValues(node, object)
315+
object = this.object = NodeCache(node, InitialValues(node, _valuesEnd));
316+
}
317+
else if (!_valuesEnd) {
318+
_valuesEnd = this._valuesEnd = InitialValues(node, object);
334319
}
335320
}
336-
337321
for (const property in _valuesEnd) {
338-
const start = object && object[property]
339-
const end = _valuesEnd[property]
340-
322+
const start = object && object[property];
323+
const end = _valuesEnd[property];
341324
if (Plugins[property]) {
342-
const plugin = Plugins[property].prototype.update ? new Plugins[property](this, start, end, property, object) : Plugins[property](this, start, end, property, object)
325+
const plugin = Plugins[property].prototype.update ? new Plugins[property](this, start, end, property, object) : Plugins[property](this, start, end, property, object);
343326
if (plugin) {
344-
_valuesFunc[property] = plugin
327+
_valuesEnd[property] = plugin;
345328
}
346-
continue
347-
}
348-
349-
if (!object || object[property] === undefined) {
350-
continue
351-
}
352-
353-
if (typeof end === 'number' && typeof start === 'number') {
354-
_valuesStart[property] = start
355-
_valuesEnd[property] = end
356-
} else {
357-
_valuesFunc[property] = InterTween(start, end)
329+
continue;
358330
}
359331
}
360332

333+
this._valuesEnd = InterTween(object, _valuesEnd, null, _easingFunction)
334+
361335
if (Renderer && this.node) {
362-
this.__render = new Renderer(this, object, _valuesEnd)
336+
this.__render = new Renderer(this, object, _valuesEnd);
363337
}
364338

365339
return this
@@ -491,22 +465,17 @@ class Tween extends EventClass {
491465
*/
492466
public reassignValues() {
493467
const {
494-
_valuesStart,
495468
_valuesEnd,
496-
object,
497-
isArr
469+
object
498470
} = this
499471

500-
let property: any
501-
for (property in _valuesEnd) {
502-
if (isArr) {
503-
property = parseInt(property)
504-
}
472+
const _valuesStart: any = _valuesEnd(0)
473+
474+
for (const property in _valuesStart) {
505475

506476
const start = _valuesStart[property]
507-
const end = _valuesEnd[property]
508477

509-
object[property] = typeof end === 'function' ? end(0) : start
478+
object[property] = start
510479
}
511480

512481
return this
@@ -520,7 +489,7 @@ class Tween extends EventClass {
520489
* @memberof Tween
521490
*/
522491
public update(time: number, preserve?: boolean) {
523-
const {
492+
let {
524493
_onStartCallbackFired,
525494
_easingFunction,
526495
_easingReverse,
@@ -531,19 +500,15 @@ class Tween extends EventClass {
531500
_reversed,
532501
_startTime,
533502
_duration,
534-
_valuesStart,
535503
_valuesEnd,
536-
_valuesFunc,
537504
object,
538505
_isFinite,
539506
_isPlaying,
540507
__render
541508
} = this
542509

543-
let elapsed
544-
let value
545-
let property
546-
let currentEasing
510+
let elapsed: number
511+
let currentEasing: Function
547512

548513
time = time !== undefined ? time : now()
549514

@@ -555,6 +520,7 @@ class Tween extends EventClass {
555520
if (!this._rendered) {
556521
this.render()
557522
this._rendered = true
523+
_valuesEnd = this._valuesEnd
558524
}
559525

560526
this.emit(EVENT_START, object)
@@ -568,27 +534,11 @@ class Tween extends EventClass {
568534

569535
currentEasing = _reversed ? _easingReverse : _easingFunction
570536

571-
if (!object) {
537+
if (typeof _valuesEnd !== 'function' || !object) {
572538
return true
573539
}
574540

575-
for (property in _valuesEnd) {
576-
value = currentEasing[property] ? currentEasing[property](elapsed) : typeof currentEasing === 'function' ? currentEasing(elapsed) : defaultEasing(elapsed)
577-
578-
const start = _valuesStart[property]
579-
const end = _valuesEnd[property]
580-
const fnc = _valuesFunc[property]
581-
582-
if (fnc && fnc.update) {
583-
fnc.update(value, elapsed)
584-
} else if (fnc) {
585-
object[property] = fnc(value)
586-
} else if (typeof end === 'number') {
587-
object[property] = start + (end - start) * value
588-
} else {
589-
object[property] = end
590-
}
591-
}
541+
_valuesEnd(elapsed, elapsed, currentEasing)
592542

593543
if (__render) {
594544
__render.update(object, elapsed)

ts/core.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const add = (tween) => {
4444
* @example
4545
* TWEEN.onTick(time => console.log(time))
4646
*/
47-
const onTick = fn => _tweens.push({update:fn})
47+
const onTick = fn => _tweens.push({ update: fn })
4848

4949
/**
5050
* @returns {Array<Tween>} List of tweens in Array
@@ -147,7 +147,9 @@ const now = (function () {
147147
const update = (time: number, preserve?: boolean) => {
148148
time = time !== undefined ? time : now()
149149

150-
_tick = _ticker(update)
150+
if (_autoPlay && isStarted) {
151+
_tick = _ticker(update)
152+
}
151153

152154
if (_tweens.length === 0) {
153155
_stopTicker(_tick)

0 commit comments

Comments
 (0)