简体   繁体   English

如何在Draw2D库中的画布上侦听鼠标事件?

[英]How to listen to mouse events on canvas in Draw2D library?

I want to allow the user to draw a rectangle on the canvas with Draw2D library. 我想允许用户使用Draw2D库在画布上绘制一个矩形。 The canvas location of mouse-down will be top-left and the canvas location of mouse-up should become the bottom right. 鼠标向下的画布位置将在左上方,而鼠标向上的画布位置应在右下方。 However, I am unable to catch the mouse-up and mouse-down events on the canvas. 但是,我无法捕获画布上的向上和向下鼠标事件。

Here is the code I am trying, but there is no output: 这是我正在尝试的代码,但是没有输出:

var canvas = new draw2d.Canvas("canvas-div");
var policy = new draw2d.policy.canvas.CanvasPolicy();
policy.onClick = function(canvas, mouseX, mouseY) {
    console.log("Mouse click:" + mouseX + "," + mouseY);
}
canvas.installEditPolicy(policy); 

In Draw2D-js you need always to extend Classes if you want to do something new. 在Draw2D-js中,如果要执行新操作,则始终需要扩展类。 ( you are redefining method with the wrong way ) 您正在用错误的方式重新定义方法

in your exemple you can create this: 在您的示例中,您可以创建以下代码:

  var MyPolicy = draw2d.policy.canvas.CanvasPolicy.extend({
  NAME: 'MyPolicy',
  init: function() {
    this._super();
    alert("done");
  },
  onClick: function(the, mouseX, mouseY, shiftKey, ctrlKey) {
    this._super(the, mouseX, mouseY, shiftKey, ctrlKey);
    alert("MyPolicy click:" + mouseX + "," + mouseY);
  }
 });

and than use it like this: 而不是像这样使用它:

var policy = new MyPolicy();
canvas.installEditPolicy(policy); 

another method is to use JQuery directly: 另一种方法是直接使用JQuery:

$('#draw2Did').click(function(ev) { 
      alert("Mouse click:" + ev.clientX + "," +ev.clientY); }
);

Reference: 参考:

http://draw2d.org/draw2d_touch/jsdoc_5/#!/api/draw2d.policy.canvas.CanvasPolicy http://draw2d.org/draw2d_touch/jsdoc_5/#!/api/draw2d.policy.canvas.CanvasPolicy

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

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