var Damper = function(fullValue, restoreCountdownMS) { this._fullValue = fullValue; // 'Constant' this._restoreCountdownMS = restoreCountdownMS; // 'Constant' // Mutable state. this._countdownMS = 0; // Always at full strength initially. this._lastTimeMS = Date.now(); this._isPaused = false; }; Damper.prototype.update = function() { var deltaMS = Date.now() - this._lastTimeMS; if (!this._isPaused) { this._countdownMS -= deltaMS; this._countdownMS = Math.max(0, this._countdownMS); } this._lastTimeMS = Date.now(); }; function lerp(v0, v1, t) { return v0*(1-t)+v1*t } Damper.prototype.pause = function() { this._isPaused = true; } Damper.prototype.resume = function() { this._isPaused = false; } Damper.prototype.dampen = function() { this._countdownMS = this._restoreCountdownMS; // Reset the countdown 'clock'. }; Damper.prototype.getValue = function() { if (this._countdownMS > 0) { var t = this._countdownMS / this._restoreCountdownMS; return lerp(this._fullValue, 0, t); } else { return this._fullValue; } }; /* States: - stopped (default) - Date.now() // Milliseconds function damper(fullValue, restoreDuration) { this._fullValue = fullValue; this._restoreDuration = restoreDuration; this._clock = new THREE.Clock(false); this._clock.stop(); this._resumeTime = this._clock.elapsedTime; }; damper.prototype.pause = function() { this._clock.stop(); // this._isPaused = true; }; damper.prototype.resume = function() { this._clock.start(); // this._isPaused = false; this._resumeTime = this._clock.elapsedTime; }; damper.prototype.getValue = function() { var currentTime = this._clock.elapsedTime; var = (currentTime - this._resumeTime) / this._restoreDuration; if (this._isPaused) { return 0; } else if (currentTime - this._restoreDuration > this._resumeTime) { // Still restoring. return lerp(0, this._fullValue, ) } else { } // body... }; damper.prototype.update = function() { clock.getDelta(); }; */