Skip to content

Commit bcc63ed

Browse files
authored
Merge pull request #526 from tweenjs/relativeyoyo
Fix to make yoyo work with relative values
2 parents 37bba67 + 0c82109 commit bcc63ed

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

examples/10_yoyo.html

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ <h2>10 _ yoyo</h2>
1515
<div id="target1" data-rotation="0" data-y="0" class="box" style="left: 0px; top: 0px">
1616
yoyo with repeat once
1717
</div>
18-
<div id="target2" data-rotation="0" data-y="0" class="box" style="left: 300px; top: 0px">
18+
<div id="target2" data-rotation="0" data-y="0" class="box" style="left: 200px; top: 0px">
1919
yoyo with repeat forever
2020
</div>
21+
<div id="target3" data-rotation="0" data-y="0" class="box" style="left: 400px; top: 0px">
22+
yoyo with repeat once, relative values
23+
</div>
24+
<div id="target4" data-rotation="0" data-y="0" class="box" style="left: 600px; top: 0px">
25+
yoyo with repeat forever, relative values
26+
</div>
2127
</div>
2228

2329
<script src="../dist/tween.umd.js"></script>
@@ -50,6 +56,28 @@ <h2>10 _ yoyo</h2>
5056
updateBox( target2, object );
5157
})
5258
.start();
59+
target3 = document.getElementById( 'target3' ),
60+
tween3 = new TWEEN.Tween( target3.dataset )
61+
.to( { rotation: "+360", y:"+300" }, 750 )
62+
.repeat( 1 )
63+
.delay( 1000 )
64+
.yoyo( true )
65+
.easing( TWEEN.Easing.Cubic.InOut )
66+
.onUpdate( function(object) {
67+
updateBox( target3, object );
68+
})
69+
.start();
70+
target4 = document.getElementById( 'target4' ),
71+
tween4 = new TWEEN.Tween( target4.dataset )
72+
.to( { rotation: "+360", y:"+300" }, 750 )
73+
.repeat( Infinity )
74+
.delay( 1000 )
75+
.yoyo( true )
76+
.easing( TWEEN.Easing.Cubic.InOut )
77+
.onUpdate( function(object) {
78+
updateBox( target4, object );
79+
})
80+
.start();
5381
}
5482

5583
function animate( time ) {
@@ -94,6 +122,12 @@ <h2>10 _ yoyo</h2>
94122
#target2 {
95123
background: #cfc;
96124
}
125+
#target3 {
126+
background: deeppink;
127+
}
128+
#target4 {
129+
background: cyan;
130+
}
97131
</style>
98132
</body>
99133
</html>

src/Tween.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,19 @@ TWEEN.Tween.prototype = {
475475
// Reassign starting values, restart by making startTime = now
476476
for (property in this._valuesStartRepeat) {
477477

478-
if (typeof (this._valuesEnd[property]) === 'string') {
479-
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property]);
478+
if (!this._yoyo && typeof(this._valuesEnd[property]) === 'string') {
479+
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property], 10);
480480
}
481481

482482
if (this._yoyo) {
483483
var tmp = this._valuesStartRepeat[property];
484484

485-
this._valuesStartRepeat[property] = this._valuesEnd[property];
485+
if (typeof(this._valuesEnd[property]) === 'string') {
486+
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property], 10);
487+
} else {
488+
this._valuesStartRepeat[property] = this._valuesEnd[property];
489+
}
490+
486491
this._valuesEnd[property] = tmp;
487492
}
488493

test/unit/tests.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345

346346
},
347347

348-
'Tween relative positive value, with sign': function(test) {
348+
'Tween relative positive value': function(test) {
349349

350350
var obj = { x: 0 },
351351
t = new TWEEN.Tween( obj );
@@ -387,6 +387,52 @@
387387

388388
},
389389

390+
'Tween relative positive value, with yoyo': function(test) {
391+
392+
var obj = { x: 0 },
393+
t = new TWEEN.Tween( obj );
394+
395+
t.to( { x: "+100" }, 1000 );
396+
t.repeat( 1 );
397+
t.yoyo( true );
398+
t.start( 0 );
399+
400+
t.update( 500 );
401+
test.equal( obj.x, 50 );
402+
t.update( 1000 );
403+
test.equal( obj.x, 100 );
404+
t.update( 1500 );
405+
test.equal( obj.x, 50 );
406+
t.update( 2000 );
407+
test.equal( obj.x, 0 );
408+
409+
test.done();
410+
411+
},
412+
413+
'Tween relative negative value, with yoyo': function(test) {
414+
415+
var obj = { x: 0 },
416+
t = new TWEEN.Tween( obj );
417+
418+
t.to( { x: "-100" }, 1000 );
419+
t.repeat( 1 );
420+
t.yoyo( true );
421+
t.start( 0 );
422+
423+
t.update( 500 );
424+
test.equal( obj.x, -50 );
425+
t.update( 1000 );
426+
test.equal( obj.x, -100 );
427+
t.update( 1500 );
428+
test.equal( obj.x, -50 );
429+
t.update( 2000 );
430+
test.equal( obj.x, -0 );
431+
432+
test.done();
433+
434+
},
435+
390436
'Test TWEEN.Tween.start()': function(test) {
391437

392438
var obj = { },

0 commit comments

Comments
 (0)