Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timescale #531

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/tween.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions examples/13_timescale.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tween.js / timeScale</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="info">
<h1><a href="http://github.com/sole/tween.js">tween.js</a></h1>
<h2>13 _ timeScale</h2>
<p>Demonstrating the timeScale() feature.</p>
<p><input type="range" min="0.1" max="2" step="0.1" value="1" /></p>
</div>
<div style="position: absolute; top: 100px; left: 400px; ">
<div id="target" data-rotation="0" data-y="0" class="box" style="left: 0px; top: 0px">
1x
</div>
</div>

<script src="../build/tween.min.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script>

(function() {
var target;
var tween;

init();
animate();

function init() {
target = document.getElementById( 'target' );
tween = new TWEEN.Tween( target.dataset )
.to( { rotation: 360, x: 500 }, 750 )
.repeat( Infinity )
.delay( 1000 )
.yoyo( true )
.easing( TWEEN.Easing.Cubic.InOut )
.onUpdate( function() {
updateBox( target, this );
});
var slider = document.querySelector('input[type=range]');

slider.addEventListener('input', onSliderChange);

tween.timeScale(1.0);
slider.value = 1.0;
tween.start();
}

function onSliderChange() {
var v = this.value * 1.0;
target.innerHTML = v + 'x';
tween.timeScale(v);
}

function animate( time ) {

requestAnimationFrame( animate );
TWEEN.update( time );

}

function updateBox( box, params ) {

var s = box.style,
transform = 'rotate(' + Math.floor( params.rotation ) + 'deg)';
s.webkitTransform = transform;
s.mozTransform = transform;
s.transform = transform;

box.style.top = params.y + 'px';
box.style.left = params.x + 'px';
}
})();

</script>

<style type="text/css">
.box {
display: block;
border-radius: 4rem;
position: absolute;
border: 1px solid black;
font-size: 3rem;
padding: 2rem;
vertical-align: center;
}
#target {
background: #fcc;
}
</style>
</body>
</html>
10 changes: 8 additions & 2 deletions src/Tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var TWEEN = TWEEN || ( function () {

return {

REVISION: '14',
REVISION: '15dev',

getAll: function () {

Expand Down Expand Up @@ -98,6 +98,7 @@ TWEEN.Tween = function ( object ) {
var _reversed = false;
var _delayTime = 0;
var _startTime = null;
var _timeScale = 1.0;
var _easingFunction = TWEEN.Easing.Linear.None;
var _interpolationFunction = TWEEN.Interpolation.Linear;
var _chainedTweens = [];
Expand Down Expand Up @@ -235,6 +236,11 @@ TWEEN.Tween = function ( object ) {

};

this.timeScale = function( scale ) {
_timeScale = scale;
return this;
};

this.chain = function () {

_chainedTweens = arguments;
Expand Down Expand Up @@ -292,7 +298,7 @@ TWEEN.Tween = function ( object ) {

}

var elapsed = ( time - _startTime ) / _duration;
var elapsed = ( time - _startTime ) / (_duration / _timeScale);
elapsed = elapsed > 1 ? 1 : elapsed;

var value = _easingFunction( elapsed );
Expand Down