Clock.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.Clock = function ( autoStart ) {
  5. this.autoStart = ( autoStart !== undefined ) ? autoStart : true;
  6. this.startTime = 0;
  7. this.oldTime = 0;
  8. this.elapsedTime = 0;
  9. this.running = false;
  10. };
  11. THREE.Clock.prototype = {
  12. constructor: THREE.Clock,
  13. start: function () {
  14. this.startTime = this._getNow();
  15. this.oldTime = this.startTime;
  16. this.running = true;
  17. },
  18. stop: function () {
  19. this.getElapsedTime();
  20. this.running = false;
  21. },
  22. getElapsedTime: function () {
  23. this.getDelta();
  24. return this.elapsedTime;
  25. },
  26. getDelta: function () {
  27. var diff = 0;
  28. if ( this.autoStart && ! this.running ) {
  29. this.start();
  30. }
  31. if ( this.running ) {
  32. var newTime = this._getNow();
  33. diff = 0.001 * ( newTime - this.oldTime );
  34. this.oldTime = newTime;
  35. this.elapsedTime += diff;
  36. }
  37. return diff;
  38. },
  39. _getNow: function () {
  40. var now = self.performance !== undefined && self.performance.now !== undefined
  41. ? self.performance.now()
  42. : Date.now();
  43. return now;
  44. }
  45. };
粤ICP备19079148号