简体   繁体   English

如何在HTML5 Canvas中的对象上获得鼠标的第一个和最后一个位置?

[英]How to get the first and last position of a mouse on object in HTML5 Canvas?

I have a bar drew in Canvas. 我在Canvas中画了一个酒吧。
When user clicks anywhere in the bar I want to get the start position. 当用户单击栏中的任意位置时,我想获得开始位置。 Then, the user drags the mouse to any position and releases the mouse and I would get the last position. 然后,用户将鼠标拖动到任何位置并释放鼠标,我将获得最后一个位置。

I've been doing that for some time but I couldn't get the right events. 我这样做已经有一段时间了,但是我没有得到正确的事件。

Here's my code. 这是我的代码。

<canvas id="demoCanvas" width="500" height="300"></canvas>
var stage = new createjs.Stage("demoCanvas");

var rect = new createjs.Shape();
rect.graphics.beginFill("#000").drawRect(0, 20, 200, 50);

rect.on('mousedown', function (mousedownEvent) {
    var startX = mousedownEvent.rawX;
    console.log('mousedown');
});

rect.on('mouseup', function(mouseupEvent) {
   var stopX = mouseupEvent.rawX;
    console.log('mouseup');
    console.log(stopX);
});

stage.addChild(rect);
stage.update();

http://jsfiddle.net/noppanit/x0bdq3aa/ http://jsfiddle.net/noppanit/x0bdq3aa/

Check this code once, Here you will get x and y position continously. 一次检查此代码,在这里您将连续获得x和y位置。

  function writeMessage(canvas, message) {
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.font = '18pt Calibri';
    context.fillStyle = 'white';
    context.fillText(message, 10, 25);
  }
  function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();


    return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
    };
  }
  var canvas = document.getElementById('demoCanvas');
  var context = canvas.getContext('2d');

  canvas.addEventListener('mousemove', function(evt) {
    var mousePos = getMousePos(canvas, evt);
    var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y;
    writeMessage(canvas, message);
  }, false);

check this fiddle-> http://jsfiddle.net/x0bdq3aa/3/ 检查这个小提琴-> http://jsfiddle.net/x0bdq3aa/3/

This is to get the mouse coordinates relative to Canvas,getMousePos() method which returns the mouse coordinates based on the position of the client mouse and the position of the canvas obtained from the getBoundingClientRect() method of the window object. 这是为了获得相对于Canvas,getMousePos()方法的鼠标坐标,该方法根据客户端鼠标的位置和从窗口对象的getBoundingClientRect()方法获得的画布的位置返回鼠标坐标。

The event you are looking for is 'pressup' , you may also need 'mouseleave' and 'mouseout' . 您要查找的事件是'pressup'您可能还需要'mouseleave''mouseout'

Strangely, this easeljs tutorial explicitly says that you can use the 'mouseup' event as you just did. 奇怪的是,这个easyljs教程明确指出,您可以像刚才那样使用'mouseup'事件。

However, when you look to the docs about events attached to the Stage class and the ones attached to the DisplayObject class , there is no mention of this 'mouseup' . 但是,当您查看有关'mouseup' 到Stage类的事件'mouseup'DisplayObject类的 事件的文档时,没有提到此'mouseup'

About the 'pressup' event : 关于'pressup'事件:

After a mousedown occurs on a display object, a pressup event will be generated on that object when that mouse press is released. 在显示对象上按下鼠标后,在释放该鼠标按下时将在该对象上生成按下事件。 This can be useful for dragging and similar operations. 这对于拖动和类似操作很有用。

 var stage = new createjs.Stage("demoCanvas"); var rect = new createjs.Shape(); rect.graphics.beginFill("#000").drawRect(0, 20, 200, 50); rect.on('mousedown', function (mousedownEvent) { var startX = mousedownEvent.rawX; snippet.log('mousedown'); }); rect.on('pressup', function(mouseupEvent) { var stopX = mouseupEvent.rawX; snippet.log('mouseup'); snippet.log(stopX); }); stage.addChild(rect); stage.update(); 
 <script src="https://code.createjs.com/easeljs-0.8.1.min.js"></script> <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <canvas id="demoCanvas" width="500" height="70"></canvas> 

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

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