Skip to content

Commit

Permalink
fix: Don't interpret string values as relative if they do not start w…
Browse files Browse the repository at this point in the history
…ith a sign. Closes #239 and also add tests to define the behaviour of relative values, closes #243
  • Loading branch information
sole committed Jan 12, 2016
1 parent 6a6e037 commit 6cb21f2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
15 changes: 7 additions & 8 deletions examples/09_relative_values.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
function init() {
var target1 = document.getElementById( 'target1' ),
tween1 = new TWEEN.Tween( target1.dataset )
.to( { top: "+20", left: "-20" }, 1000 )
.to( { top: "+20", left: "-20" }, 500 )
.repeat( 5 )
.delay( 500 )
.easing( TWEEN.Easing.Exponential.In )
.onUpdate( function() {
this.top = Math.round(this.top);
this.left = Math.round(this.left);
updateBox( target1, this );
})
.start();
Expand All @@ -43,17 +45,14 @@
function animate( time ) {

requestAnimationFrame( animate );

TWEEN.update( time );

}

function updateBox( box, params ) {
var field,
s = box.style;
for (field in params) {
s[field] = params[field] + "px";
}
var s = box.style;
var transform = 'translate(' + params.left + 'px, ' + params.top + 'px)';
s.transform = transform;
}

</script>
Expand All @@ -65,7 +64,7 @@ <h2>09 _ relative values</h2>
<p>Tweening to relative values, with repeat.</p>
</div>
<div style="position: absolute; left: 400px; ">
<div id="target1" style="position: absolute;" data-top="150" class="box">
<div id="target1" style="position: absolute;" data-top="150" data-left="150" class="box">
</div>
</div>
</body>
Expand Down
7 changes: 6 additions & 1 deletion src/Tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ TWEEN.Tween = function (object) {

// Parses relative end values with start as base (e.g.: +10, -3)
if (typeof (end) === 'string') {
end = start + parseFloat(end, 10);

if (end.startsWith('+') || end.startsWith('-')) {
end = start + parseFloat(end, 10);
} else {
end = parseFloat(end, 10);
}
}

// Protect against non numeric properties.
Expand Down
42 changes: 42 additions & 0 deletions test/unit/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,48 @@

},

'Tween relative positive value, with sign': function(test) {

var obj = { x: 0 },
t = new TWEEN.Tween( obj );

t.to( { x: "+100" }, 1000 );
t.start( 0 );
t.update( 1000 );

test.equal( obj.x, 100 );
test.done();

},

'Tween relative negative value': function(test) {

var obj = { x: 0 },
t = new TWEEN.Tween( obj );

t.to( { x: "-100" }, 1000 );
t.start( 0 );
t.update( 1000 );

test.equal( obj.x, -100 );
test.done();

},

'String values without a + or - sign should not be interpreted as relative': function(test) {

var obj = { x: 100 },
t = new TWEEN.Tween( obj );

t.to( { x: "100" }, 1000 );
t.start( 0 );
t.update( 1000 );

test.equal( obj.x, 100 );
test.done();

},

'Test TWEEN.Tween.start()': function(test) {

var obj = { },
Expand Down

0 comments on commit 6cb21f2

Please sign in to comment.