deterministic-injection.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = () => 0; // frameId * 16;
  13. window.Date.now = now;
  14. window.Date.prototype.getTime = now;
  15. window.performance.now = now;
  16. /* Deterministic RAF */
  17. window._renderStarted = false;
  18. window._renderFinished = false;
  19. window.requestAnimationFrame = function ( cb ) {
  20. if ( window._renderFinished === true ) return
  21. if ( window._renderStarted === false ) {
  22. const intervalId = setInterval( function () {
  23. if ( window._renderStarted === true ) {
  24. cb( now() );
  25. clearInterval( intervalId );
  26. window._renderFinished = true;
  27. }
  28. }, 100 );
  29. }
  30. };
  31. /* Semi-deterministic video */
  32. const play = HTMLVideoElement.prototype.play;
  33. HTMLVideoElement.prototype.play = async function () {
  34. play.call( this );
  35. this.addEventListener( 'timeupdate', () => this.pause() );
  36. function renew() {
  37. this.load();
  38. play.call( this );
  39. RAF( renew );
  40. }
  41. RAF( renew );
  42. };
  43. /* Additional variable for ~5 examples */
  44. window.TESTING = true;
  45. }() );
粤ICP备19079148号