Guest User

AnimatedValue Animated Value Javascript

a guest
Feb 14th, 2017
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function AnimatedValue(value,isAngle) {
  2.     this.write(value);
  3.     this.isAngle    = isAngle;
  4.     this.halfAngle  = Math.PI;
  5.     this.wholeAngle  = Math.PI * 2;
  6.     this.oneAndHalfAngle  = this.wholeAngle + this.halfAngle;
  7. }
  8.  
  9. AnimatedValue.prototype = {
  10.     get: function () {
  11.         if (this.timeout) {
  12.             var now = Date.now(),
  13.                 end = this.frTime + this.timeout;
  14.             if (now >= end) {
  15.                 this.timeout = 0;
  16.                 return this.toVal;
  17.             } else {
  18.                 if(this.isAngle){
  19.                     var shortest_angle=((((this.toVal - this.frVal) % this.wholeAngle) + this.oneAndHalfAngle) % this.wholeAngle) - this.halfAngle;
  20.                     var progress = shortest_angle * (1 - (end - now) / this.timeout);
  21.                     return progress + this.frVal;
  22.                 }
  23.                 return this.toVal - (this.toVal - this.frVal) * (end - now) / this.timeout;
  24.             }
  25.         } else {
  26.             return this.toVal;
  27.         }
  28.     },
  29.     set: function (value, timeout) {
  30.         if (value != this.toVal) {
  31.             this.frVal = this.get();
  32.             this.toVal = value;
  33.             this.timeout = timeout;
  34.             this.following = undefined;
  35.             this.frTime = Date.now();
  36.         }
  37.     },
  38.     follow: function (following, timeout) {
  39.         this.frVal = this.get();
  40.         this.following = following;
  41.         this.timeout = timeout;
  42.         this.frTime = Date.now();
  43.     },
  44.     write: function (value) {
  45.         this.frVal = value;
  46.         this.toVal = value;
  47.         this.timeout = 0;
  48.         this.frTime = Date.now(); // so end == now
  49.     }
  50. };
Advertisement
Add Comment
Please, Sign In to add comment