Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop drawing when mouse is released outside of the canvas #37

Merged
merged 1 commit into from
Jul 31, 2021

Conversation

jelenamilanovic
Copy link
Contributor

I noticed that when a user draws on the canvas and clicks mouse button on any element outside of it, mousemove event still continues triggering drawing action on the canvas (since mouseup event isn't called on it).

Steps to reproduce (example):
Draw something on the canvas and without releasing a mouse, try to change the color. Color input will open and the app will continue drawing simultaneously.

Some possible solutions:

  • Set current mouseup event listener on the whole document (as done here in the code).
  • Keep the mouseup event defined on the canvas and write another one on the whole document. This event will set isPressed variable to false anytime the user releases the mouse button outside of the canvas:
document.addEventListener('mouseup', (e) => {
    if (e.target !== canvas) {
        isPressed = false;
    }
}); 

@bradtraversy bradtraversy merged commit af3b536 into bradtraversy:master Jul 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants