This is a bit of a shot in the dark (and its too much to put in a comment), but from what I can tell, while you are recording the mouse position in the mousemove
event, you are not doing so in the click
event. I believe its not so much that the picture's X and Y have changed, it is probably that the last recorded mouse's X and Y have changed due to the mousemove
event firing after the click.
A general solution (because it can be solved many different ways) might be to record the mouse's row and column in the click
event, and then suppress any mouse position updates in the mousemove
event if your clicker
variable is true
. So maybe something like the following:
canvas.addEventListener('mousemove', function(evt) {
if(!clicker){
// happily record the mouse position, because user hasn't clicked anything
In the paint
method, you can check if clicker
is true
and do the text thing using the last known mouse position previously recorded in the click
event, and then reset it back to false
when you're done processing. Your paint
method is fairly large, and clicker
seems to be used throughout so I won't post a full blown code block in the interests of brevity, but I think you get the idea.
ALSO: I notice in your if
statements, you're checking the clicker
variable using && clicker = true
. Notice the single equal sign? That's extremely bad! Instead of checking its value, you're assigned its value. Remember single equals is for assignment, double equals is for equivalence.
You can solve that error a couple of ways; the simplest is to simply make sure you change it to ==
instead, and anywhere else you might have made that error. Or, since its a boolean, you can simple do && clicker
or && !clicker
, depending on the condition.
Alternatively, from the looks of your logic, since every if
and if else
is dependent on whether clicker
is true, you can get rid of the redundant checking in each condition and simply wrap the entire thing in one
if`, like so:
if(clicker){
if (melXref == mR && melYref == mC) {
ctx.fillStyle = "black";
ctx.fillText("It works!!!!!", 200, 200);
}
if (melX < w / 3) { // First Column (Mel)
... other conditions
}