Skip to content

Commit 7083435

Browse files
committed
add tests for .to() with relative array values, and implement it
1 parent bcc63ed commit 7083435

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/Tween.js

+13
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ TWEEN.Tween.prototype = {
204204
continue;
205205
}
206206

207+
// handle relative values
208+
this._valuesEnd[property] = this._valuesEnd[property].map(n => {
209+
if (typeof n !== 'string') {
210+
return n;
211+
}
212+
213+
if (n.charAt(0) === '+' || n.charAt(0) === '-') {
214+
return this._object[property] + parseFloat(n);
215+
} else {
216+
return parseFloat(n);
217+
}
218+
});
219+
207220
// Create a local copy of the Array with the start value at the front
208221

209222
this._valuesEnd[property] = [this._object[property]].concat(this._valuesEnd[property]);

test/unit/tests.js

+43
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,49 @@
433433

434434
},
435435

436+
'Tween relative positive array values': function(test) {
437+
438+
var obj = { x: 0 },
439+
t = new TWEEN.Tween( obj );
440+
441+
t.to( { x: ["+100", "+0", "-100", "+0"] }, 2000 );
442+
t.start( 0 );
443+
444+
t.update( 250 );
445+
test.equal( obj.x, 50 );
446+
t.update( 500 );
447+
test.equal( obj.x, 100 );
448+
t.update( 750 );
449+
test.equal( obj.x, 50 );
450+
t.update( 1000 );
451+
test.equal( obj.x, 0 );
452+
t.update( 1250 );
453+
test.equal( obj.x, -50 );
454+
t.update( 1500 );
455+
test.equal( obj.x, -100 );
456+
t.update( 1750 );
457+
test.equal( obj.x, -50 );
458+
t.update( 2000 );
459+
test.equal( obj.x, 0 );
460+
461+
test.done();
462+
463+
},
464+
465+
// 'String values without a + or - sign should not be interpreted as relative with array values': function(test) {
466+
467+
// var obj = { x: 100 },
468+
// t = new TWEEN.Tween( obj );
469+
470+
// t.to( { x: "100" }, 1000 );
471+
// t.start( 0 );
472+
// t.update( 1000 );
473+
474+
// test.equal( obj.x, 100 );
475+
// test.done();
476+
477+
// },
478+
436479
'Test TWEEN.Tween.start()': function(test) {
437480

438481
var obj = { },

0 commit comments

Comments
 (0)