I've just posted another blog post to provide context and answer some questions people had. Like why are we checking screenX === 0 in the first place? Why do we want different behaviour depending on keyboard or mouse inputs? And I've explain how I've refactored it to prevent more mishaps. I hope it's helpful.
https://www.joshtumath.uk/posts/2024-11-18-how-i-refactored-...