Clock.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 = self.performance !== undefined && self.performance.now !== undefined
  15. ? self.performance.now()
  16. : Date.now();
  17. this.oldTime = this.startTime;
  18. this.running = true;
  19. },
  20. stop: function () {
  21. this.getElapsedTime();
  22. this.running = false;
  23. },
  24. getElapsedTime: function () {
  25. this.getDelta();
  26. return this.elapsedTime;
  27. },
  28. getDelta: function () {
  29. var diff = 0;
  30. if ( this.autoStart && ! this.running ) {
  31. this.start();
  32. }
  33. if ( this.running ) {
  34. var newTime = self.performance !== undefined && self.performance.now !== undefined
  35. ? self.performance.now()
  36. : Date.now();
  37. diff = 0.001 * ( newTime - this.oldTime );
  38. this.oldTime = newTime;
  39. this.elapsedTime += diff;
  40. }
  41. return diff;
  42. }
  43. };
粤ICP备19079148号