At a score of 1,619 the ball is moving so fast it no longer works! I switched to a new tab (hoping that the event loop would be sent to the background/context switch/whatever) and ta-da - it misclicked. High score of 1,637.
At a score of 1,619 the ball is moving so fast it no longer works! I switched to a new tab (hoping that the event loop would be sent to the background/context switch/whatever) and ta-da - it misclicked. High score of 1,637.
function intersectRect(rectA, rectB) { return !( rectB.left >= rectA.right || rectB.right <= rectA.left || rectB.top >= rectA.bottom || rectB.bottom <= rectA.top ); }
const arc = document.getElementById("arc"); const ball = document.getElementById("ball");
setInterval(() => { if (intersectRect(arc.getBoundingClientRect(), ball.getBoundingClientRect())) { window.dispatchEvent(new KeyboardEvent('keypress', { keyCode: 32, })); } }, 10);
My code seems to go on forever, closed the tab after it reached 60K
Of course, for how delightfully simple this one is, your approach is definitely faster/better!
Just adding this information for the ones who don't know and like to know more about browser performance :)
EDIT: Looked down thread and basically saw a bunch of examples of just that!
So calling it 100 times in a loop should be fast, but calling it 100 times in a loot that also updates the DOM should be cripplingly slow.
loopTapApp.score = Number.MAX_SAFE_INTEGER;loopTapApp.score = Infinity
I didn't peek and ended up with the same solution as you, so for an extra bit of fun I tried to golf it to be as short as possible. There's probably room for much improvement: (94 chars)
setInterval("l=loopTapApp;b=l.getBallAngle();a=l.arc;b+6>a[0]&b-6<a[1]&&l.tap(new Event(1))")
setInterval("l=loopTapApp;b=l.getBallAngle();[a,c]=l.arc;b+6>a&b-6<c&&l.tap(new Event(1))")Also, in retrospect, I probably should have read the code before pasting it into my console.
function autoPlay() {
const fakeClick = {
preventDefault: function() {},
stopPropagation: function() {}
}
const ballAngle = loopTapApp.getBallAngle();
if (ballAngle + 6 > loopTapApp.arc[0] && ballAngle - 6 < loopTapApp.arc[1]) {
loopTapApp.tap(fakeClick)
}
}
const autoPlayer = setInterval(autoPlay, 10) setInterval(() => {
loopTapApp.arc = [ -Infinity, Infinity ]
loopTapApp.tap({ preventDefault: () => {}, stopPropagation: () => {} })
})Safari Version 15.2 (16612.3.6.1.8, 16612) macOS Big Sur 11.6.2 (20G314) iMac Pro 2017
Are you usually able to do this when e.g. it's more of a SPA and you have to drill down more to get to the state of some deeply nested component?
loopTapApp.startPlay();
setInterval(() => {
if(loopTapApp.getBallAngle() > loopTapApp.arc[0] && loopTapApp.getBallAngle() < loopTapApp.arc[1]) {
loopTapApp.tap({preventDefault: ()=>{}, stopPropagation: ()=>{}});
}
}, 50); loopTapApp.setArc=function(){this.arc=[0,359];}