by alfon · 4y ago · 0 comments
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.