Skip to content

Instantly share code, notes, and snippets.

@captDaylight
Created August 31, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save captDaylight/b6b907a919892720545d to your computer and use it in GitHub Desktop.
Save captDaylight/b6b907a919892720545d to your computer and use it in GitHub Desktop.
Simple Tween
function tween(finalPos, prevPos, tweenRatio) {
if (finalPos === prevPos) {
return prevPos;
} else {
let dist = Math.abs(finalPos - prevPos);
if (dist < 0.005) {
// set to final position when getting close
return prevPos;
} else {
// tween
let approach = dist * tweenRatio;
if (prevPos < finalPos)
return prevPos + approach;
else
return prevPos - approach;
}
}
};
module.exports = tween;
@captDaylight
Copy link
Author

Using this in my threejs animation cycle like so:

        mesh.rotation.y = tween(state.origRotX + state.ratioX, mesh.rotation.y, 0.08);
        mesh.rotation.x = tween(state.origRotY + state.ratioY, mesh.rotation.x, 0.08);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment