简体   繁体   English

JS布尔值var不影响if语句

[英]JS Boolean var doesn't affect if-statements

I'm doing an HTML5 canvas painter, with options to draw, place rectangles, circles and different color options. 我正在做一个HTML5画布画家,它具有绘制,放置矩形,圆形和不同颜色选项的选项。 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. 我的问题是,我最初使用的是var工具,其默认值为“ pencil”,并且我想将此变量更改为“ rect”(还没有从圆圈开始,因为我想先完成此工作),然后点击在相应的div上,这样我就可以为图纸,矩形或圆形设置if语句。 This didn't work though, and i singled the variable out, since i got an alert i placed in the onclick-function. 但是,这没有用,我将变量单挑出来,因为我收到了警报,并将其放置在onclick函数中。 After that i tried using boolean variables (pencil = true, rect = false, would have been one "state") but that doesn't work either. 之后,我尝试使用布尔变量(pencil = true,rect = false,本来是一个“状态”),但这也不起作用。 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. 我确实已经写下了if语句,所以我现在的结论是我以某种方式将布尔值作为常量(因为pencil = true是我的var的默认状态),这似乎不太可能。 The more logical explanation is that i am not correctly changing the value of the variable with the div-click. 更合乎逻辑的解释是,我没有通过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. draw和rect函数均起作用。 I also tried if(pencil == true && rect == false), doesn't work either. 我也尝试过if(pencil == true && rect == false),也不起作用。

Sorry if the answer is obvious and i just don't see it, this is the second time im programming in Javascript. 抱歉,答案很明显,我只是看不到,这是我第二次使用Java编程。

Thanks in advance! 提前致谢!

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. 另外,您可以让鼠标事件处理程序的代码检查标志,从而避免需要在每个控件“ click”事件上重新分配事件处理程序。

Variable & Method name are same rect & rect(). 变量和方法的名称与rect和rect()相同。 JavaScript does n't support same name variable and function that's why the value of rect variable override by the rect() function and always returns true but your expectation is false. JavaScript不支持相同的名称变量和函数,这就是rect()函数覆盖rect变量的值并始终返回true的原因,但您的期望是false。 Change either variable or function name of rect. 更改rect的变量或函数名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM