Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function AnimatedValue(value,isAngle) {
- this.write(value);
- this.isAngle = isAngle;
- this.halfAngle = Math.PI;
- this.wholeAngle = Math.PI * 2;
- this.oneAndHalfAngle = this.wholeAngle + this.halfAngle;
- }
- AnimatedValue.prototype = {
- get: function () {
- if (this.timeout) {
- var now = Date.now(),
- end = this.frTime + this.timeout;
- if (now >= end) {
- this.timeout = 0;
- return this.toVal;
- } else {
- if(this.isAngle){
- var shortest_angle=((((this.toVal - this.frVal) % this.wholeAngle) + this.oneAndHalfAngle) % this.wholeAngle) - this.halfAngle;
- var progress = shortest_angle * (1 - (end - now) / this.timeout);
- return progress + this.frVal;
- }
- return this.toVal - (this.toVal - this.frVal) * (end - now) / this.timeout;
- }
- } else {
- return this.toVal;
- }
- },
- set: function (value, timeout) {
- if (value != this.toVal) {
- this.frVal = this.get();
- this.toVal = value;
- this.timeout = timeout;
- this.following = undefined;
- this.frTime = Date.now();
- }
- },
- follow: function (following, timeout) {
- this.frVal = this.get();
- this.following = following;
- this.timeout = timeout;
- this.frTime = Date.now();
- },
- write: function (value) {
- this.frVal = value;
- this.toVal = value;
- this.timeout = 0;
- this.frTime = Date.now(); // so end == now
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment