deterministic-injection.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ( function () {
  2. /* Deterministic random */
  3. window.Math._random = window.Math.random;
  4. let seed = Math.PI / 4;
  5. window.Math.random = function () {
  6. const x = Math.sin( seed ++ ) * 10000;
  7. return x - Math.floor( x );
  8. };
  9. /* Deterministic timer */
  10. window.performance._now = performance.now;
  11. let frameId = 0;
  12. const now = () => frameId * 16;
  13. window.Date.now = now;
  14. window.Date.prototype.getTime = now;
  15. window.performance.now = now;
  16. /* Deterministic RAF */
  17. const RAF = window.requestAnimationFrame;
  18. window._renderStarted = false;
  19. window._renderFinished = false;
  20. const maxFrameId = 2;
  21. window.requestAnimationFrame = function ( cb ) {
  22. if ( ! window._renderStarted ) {
  23. setTimeout( function () {
  24. requestAnimationFrame( cb );
  25. }, 50 );
  26. } else {
  27. RAF( function () {
  28. if ( frameId ++ < maxFrameId ) {
  29. cb( now() );
  30. } else {
  31. window._renderFinished = true;
  32. }
  33. } );
  34. }
  35. };
  36. /* Semi-determitistic video */
  37. const play = HTMLVideoElement.prototype.play;
  38. HTMLVideoElement.prototype.play = async function () {
  39. play.call( this );
  40. this.addEventListener( 'timeupdate', () => this.pause() );
  41. function renew() {
  42. this.load();
  43. play.call( this );
  44. RAF( renew );
  45. }
  46. RAF( renew );
  47. };
  48. /* Additional variable for ~5 examples */
  49. window.TESTING = true;
  50. }() );