Clock.js 1001 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.start = function () {
  12. this.startTime = performance.now !== undefined ? performance.now() : Date.now();
  13. this.oldTime = this.startTime;
  14. this.running = true;
  15. };
  16. THREE.Clock.prototype.stop = function () {
  17. this.getElapsedTime();
  18. this.running = false;
  19. };
  20. THREE.Clock.prototype.getElapsedTime = function () {
  21. this.getDelta();
  22. return this.elapsedTime;
  23. };
  24. THREE.Clock.prototype.getDelta = function () {
  25. var diff = 0;
  26. if ( this.autoStart && ! this.running ) {
  27. this.start();
  28. }
  29. if ( this.running ) {
  30. var newTime = performance.now !== undefined ? performance.now() : Date.now();
  31. diff = 0.001 * ( newTime - this.oldTime );
  32. this.oldTime = newTime;
  33. this.elapsedTime += diff;
  34. }
  35. return diff;
  36. };