R.js 1.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
export function R (e) {
  this._drag = e
  this._dragLog = Math.log(e)
  this._x = 0
  this._v = 0
  this._startTime = 0
}

R.prototype.set = function (e, t) {
  this._x = e
  this._v = t
  this._startTime = (new Date()).getTime()
}
R.prototype.setVelocityByEnd = function (e) {
  this._v = (e - this._x) * this._dragLog / (Math.pow(this._drag, 100) - 1)
}
R.prototype.x = function (e) {
  if (e === undefined) {
    e = ((new Date()).getTime() - this._startTime) / 1e3
  }
  var t
  t = e === this._dt && this._powDragDt ? this._powDragDt : this._powDragDt = Math.pow(this._drag, e)
  this._dt = e
  return this._x + this._v * t / this._dragLog - this._v / this._dragLog
}
R.prototype.dx = function (e) {
  if (e === undefined) {
    e = ((new Date()).getTime() - this._startTime) / 1e3
  }
  var t
  t = e === this._dt && this._powDragDt ? this._powDragDt : this._powDragDt = Math.pow(this._drag, e)
  this._dt = e
  return this._v * t
}
R.prototype.done = function () {
  return Math.abs(this.dx()) < 3
}
R.prototype.reconfigure = function (e) {
  var t = this.x()
  var n = this.dx()
  this._drag = e
  this._dragLog = Math.log(e)
  this.set(t, n)
}
R.prototype.configuration = function () {
  var e = this
  return [{
    label: 'Friction',
    read: function () {
      return e._drag
    },
    write: function (t) {
      e.reconfigure(t)
    },
    min: 0.001,
    max: 0.1,
    step: 0.001
  }]
}