单击后如何获取更新以停止运行?

ree

这是我试图使更新停止运行的代码段,以便可以在画布上放置一个点。当我尝试putPoint返回return时clicked = true,无论是否单击,它都使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>


下面是该问题的较小复制。基本上,当我单击元素时,我试图将变量更新为true。但是当我在测试函数中返回true或什至将clicked设置为true时,无论是否单击,它仍显示为true。它不会动态改变。也许我使用了错误的事件?我不确定。

var clicked = false;

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

clicked = test();

console.log(clicked);

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

我根据第一段的使用和未使用(例如,拖动变量)部分的线索进行推断,但是在我看来,您正在尝试绘制一个用鼠标跟踪的点,然后单击一下发生事件后,您想在鼠标之后开始拖动拖动点,直到释放该单击。

首先,您的“点击”跟踪问题

我认为您在执行不同的语句时会误解。在第二个代码段中,“测试”事件处理程序功能之外的所有语句仅执行一次。每次单击鼠标都会调用“测试”功能,但仅返回true且不会更改“已单击”的值。因此,该语句:

var clicked = false;

...以及以下声明:

clicked = test();

...每个只执行一次。这是一个简单的示例,向您展示如何跟踪该值的切换。尝试一次简单的单击,然后在释放前先按住一下第二秒钟以了解其想法。

  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);
  });
<div id="clickable">Click Me</div>

我认为您要使用画布渲染实现什么:

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

  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
<canvas id="myCanvas">
</canvas>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章