点击后,如何让更新停止运行?

时间:2018-05-11 13:16:25

标签: javascript canvas

这是一个代码段,我试图使更新停止运行,以便我可以在画布上放置一个点。当我尝试让putPoint返回clicked = true时,无论我是否点击,它都会使点击等于真。当且仅当我点击时,我才想要返回true。



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

//canvas.width = window.innerWidth;
//canvas.height = window.innerHeight;

var radius = 2;
var dragging = false; // wether or not the mouse button is held down
ctx.lineWidth = radius * 2;
var canvasPos = getPosition(canvas);
var mouseX = 0;
var mouseY = 0;
var clicked = false;
// here is where I declare clicked = true globally

function update() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  ctx.arc(mouseX, mouseY, radius, 0, 2 * Math.PI, true);
  ctx.fill();

  requestAnimationFrame(update);
}

canvas.addEventListener("mousemove", setMousePosition, false);

function setMousePosition(e) {
  mouseX = e.clientX;
  mouseY = e.clientY;
}

//console.log("before update " + clicked);

if (clicked != true) {
  update();
  //console.log("inside update " +clicked);
}
// here is the code I want to stop when I click

//console.log("after update " + clicked);

function putPoint() {
  //e.clientX and e.clientY get the mouse position
  ctx.beginPath();
  //e.clientX and e.clientY get the mouse position
  ctx.arc(mouseX - 10, mouseY - 10, radius, 0, Math.PI * 2);
  //ctx.arc(e.offsetX, e.offsetY, radius, 0, Math.PI * 2);
  ctx.fill();
  //console.log("inside putPoint " + clicked);
}

//putPoint puts a dot on the canvas at the mouse position. But it wont fire unless
//I stop update, which tracks my dot.

//console.log("after putPoint " + clicked);


canvas.addEventListener("mousedown", putPoint);
//console.log(putPoint());

//console.log(clicked);

function getPosition(el) {
  var xPosition = 0;
  var yPosition = 0;

  while (el) {
    xPosition += (el.offsetLeft - el.scrollLeft + el.clientLeft);
    yPosition += (el.offsetTop - el.scrollTop + el.clientTop);
    el = el.offsetParent;
  }
  return {
    x: xPosition,
    y: yPosition
  };
}

<canvas id=myCanvas>
</canvas>
&#13;
&#13;
&#13;

下面的

是问题的较小复制品。基本上我在点击元素时尝试将我的变量更新为true。但是当我在测试函数中返回true或甚至设置click to true时,它仍然是真的,我点击或不点击。它没有动态变化。也许我使用错误的事件?我不确定。

var clicked = false;

console.log(clicked);
function test () {
    return true;
}

clicked = test();

console.log(clicked);

document.getElementsByTagName("h1")[0].addEventListener("mousedown", test);

1 个答案:

答案 0 :(得分:1)

我根据你的第一个片段的已使用和未使用(例如拖动变量)部分的线索推断了一点,但在我看来,你似乎想要用你的鼠标绘制一个跟踪的点,然后,一旦发生了单击事件,您就想要开始在鼠标之后拖动绘制点,直到释放该单击。

首先,您点击“&#39;跟踪

我认为你在执行不同的陈述时会产生误解。在您的第二个片段中,“测试”之外的所有陈述都包含在内。事件处理函数只执行一次。 &#39;测试&#39;每次单击鼠标都会调用函数,但只返回true并且不会更改&#39; clicked&#39;的值。声明:

var clicked = false;

......声明:

clicked = test();

...每次只执行一次。这是一个快速示例,向您展示如何跟踪该值的切换。尝试一个简单的点击,并在发布之前按住点击一秒钟以获得想法。

&#13;
&#13;
  var clicked = false;
  var clickableArea = document.getElementById("clickable");
  clickableArea.addEventListener('mousedown', function() {
    clicked = true;
    console.log('Clicked, value of clicked var: ' + clicked);
  });

  clickableArea.addEventListener('mouseup', function() {
    clicked = false;
    console.log('Released, value of clicked var: ' + clicked);
  });
&#13;
<div id="clickable">Click Me</div>
&#13;
&#13;
&#13;

我认为您使用画布渲染的目的是什么:

移动鼠标,然后单击并拖动鼠标。

&#13;
&#13;
  var canvas, ctx;
  var radius = 2;
  var mouseX = 0;
  var mouseY = 0;
  var clicked = false;
  var dragging = false;


  // manages the drawing cycle
  function putPoint() {

    // clear the canvas if not dragging, or just before the first draw of a dragging cycle
    if(!dragging) {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    }

    dragging = clicked;

    // draw
    var offset = dragging ? 10 : 0;
    ctx.beginPath();
    ctx.arc(mouseX-offset, mouseY-offset, radius, 0, 2 * Math.PI, true);
    ctx.fill();

    // kick off another cycle
    requestAnimationFrame(putPoint);
  }


  // event handlers

  function trackMouse(e) {
    mouseX = e.clientX;
    mouseY = e.clientY;
  }

  function startedDragging() {
    clicked = true;
  }

  function quitDragging() {
    clicked = false;
    dragging = false;
  }


  // only runs once when called below, sets things up, starts the drawing cycle
  function start() {
    canvas = document.getElementById("myCanvas");
    ctx = canvas.getContext("2d");
    ctx.lineWidth = radius * 2;

    // attach events to handlers
    canvas.addEventListener("mousemove", trackMouse, false);
    canvas.addEventListener("mousedown", startedDragging);
    canvas.addEventListener("mouseup", quitDragging);

    requestAnimationFrame(putPoint);
  }

  start(); // calling start to kick things off
&#13;
<canvas id="myCanvas">
</canvas>
&#13;
&#13;
&#13;