deterministic-injection.js 1.3 KB

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