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

  • SEARCH
  • Home
  • 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 9005351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:07:18+00:00 2026-06-16T01:07:18+00:00

I’m doing an HTML5 canvas painter, with options to draw, place rectangles, circles and

  • 0

I’m doing an HTML5 canvas painter, with options to draw, place rectangles, circles and different color options.
My problem is that i originally had a var tool with the default value “pencil”, and i wanted to change this variable to “rect” (didn’t start with the circle yet, since i wanted to make this work first) with clicking on the respective div so that i could have an if-statement for either drawing, rectangles or circles. This didn’t work though, and i singled the variable out, since i got an alert i placed in the onclick-function.
After that i tried using boolean variables (pencil = true, rect = false, would have been one “state”) but that doesn’t work either. I do have the if-statements already written down, so my conclusion right now is that i somehow have the booleans as constants (since pencil = true is my default state for the var), which seems unlikely. The more logical explanation is that i am not correctly changing the value of the variable with the div-click.

Here’s the part of my code:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var color = "black";
var drawing = false;
var pencil = true;
var rect = false;



//Pencil
var pencilDiv = document.getElementById("pencil");
pencilDiv.onclick = function () {
  pencil = true;
  rect = false;
}

//Rect
var rectDiv = document.getElementById("rect");
rectDiv.onclick = function () {
  rect = true;
  pencil = false;
}

if (pencil) {
  canvas.onmousedown = function (event) {
    drawing = true;
  }

  canvas.onmousemove = function (event) {
    if (drawing) {
      draw()
    }
  }

  canvas.onmouseup = function (event) {
    drawing = false;
  }
}

if (rect) {
  canvas.onmousedown = function (event) {
    rect()
  }
}

Both the draw and rect function work. I also tried if(pencil == true && rect == false), doesn’t work either.

Sorry if the answer is obvious and i just don’t see it, this is the second time im programming in Javascript.

Thanks in advance!

  • 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-06-16T01:07:20+00:00Added an answer on June 16, 2026 at 1:07 am

    You’ve written the code as if the assignment of the mouse event handlers is re-done implicitly each time conditions change. That’s not the way it works. You should simply re-assign the handlers inside event handlers for the drawing controls, because that code will be run on each “click” event.

    For example:

    pencilDiv.onclick = function () {
      canvas.onmousedown = function (event) {
        drawing = true;
      }
    
      canvas.onmousemove = function (event) {
        if (drawing) {
          draw()
        }
      }
    
      canvas.onmouseup = function (event) {
        drawing = false;
      }
    }
    
    rectDiv.onclick = function () {
      canvas.onmousedown = function (event) {
        rect()
      }
      canvas.onmousemove = canvas.onmouseup = null;
    }
    

    Alternatively, you could have the code of the mouse event handlers check the flags, thus avoiding the need to reassign the event handlers on each control “click” event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I know there's a lot of other questions out there that deal with this

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.