Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:16:01+00:00 2026-05-29T22:16:01+00:00

The code below draws a rectangle on every mouse move after the mouse button

  • 0

The code below draws a rectangle on every mouse move after the mouse button is pressed. When the user releases the mouse it should stop drawing.

I am trying to figure out how to make sure that drawing stops if the user releases the mouse outside the canvas element.

I thought that I could accomplish this by setting onmouseup event handler on my event inside onmousedown like this:

canvas.onmousedown = function (e) {
  drawing = true;
  e.onmouseup = function (v) { drawing = false; }
};


but it did not work because e.onmouseup is never called. So I ended up setting window.onmouseup instead.

Questions:

  1. why was my e.onmouseup never called?
  2. is my final solution the best way to do it?

<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script type="text/javascript">
      drawing = false;
function on_load(e) { var canvas = document.getElementById("canvas");
canvas.onmousedown = function (e) { drawing = true; };
canvas.onmousemove = function (e) { if (drawing) { var x = e.pageX - canvas.offsetLeft; var y = e.pageY - canvas.offsetTop; var context = canvas.getContext("2d"); context.strokeRect(x, y, 10, 10); } };
window.onmouseup = function (e) { drawing = false; }; } </script> </head> <body onload="on_load()"> <canvas id="canvas" width="500" height="500" style="border: 1px solid black;"> </canvas> </body> </html>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:16:02+00:00Added an answer on May 29, 2026 at 10:16 pm
    1. Why was my e.onmouseup never called?

    e is an Event object. It doesn’t have a defined behaviour for an onmouseup property

      2. Is my final solution the best way to do it?

    Yes, but with some adjustments. First, consider if you really need the global variable. The same effect can be achieved without the global variable, but it might useful to keep it global for debugging purposes.

    Second, your not-working code is not needed. It’s better to have one always-existing event listener which only changes a harmless variable, than constructing a new event listener for each mouseup event. Besides, in your “wanted” code, the previous mouseup event is never explicitly removed.

     function on_load(e) {
        var drawing = false; // <-- Always declare variables
        var canvas = document.getElementById("canvas");
        canvas.onmousedown = function (e) { drawing = true; };
        canvas.onmousemove = function (e) {
          if (drawing) {
            var x = e.pageX - canvas.offsetLeft;
            var y = e.pageY - canvas.offsetTop;
            var context = canvas.getContext("2d");
            context.strokeRect(x, y, 10, 10);
          }
        };
        window.onmouseup = function (e) { drawing = false; };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm using this code below to let a user draw their signature on
Simple code fragment below draws an ellipse on a VB.NET panel. I now need
I have a snippet of code below that draws a string to a UIView
I tried the code below, which draws a good approximation of a circle if
I have some code below that creates a RelativeLayout and adds a button to
In the code below I have simply used a mouse listener to get the
I'm trying to draw the pages of a PDF using the code below. Some
I draw the ellipse with the code as shown below. How can I make
Code below is working well as long as I have class ClassSameAssembly in same
Code below is used to save PostgreSql database backup from browser in Apache Mono

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.