我想允许用户在画布上用Draw2D库绘制一个矩形。鼠标按下的画布位置将在左上角,鼠标向上的画布位置应该在右下角。但是,我无法在画布上捕获鼠标向上和鼠标按下事件。
以下是我正在尝试的代码,但没有输出:
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);
答案 0 :(得分:1)
在Draw2D-js中,如果你想做一些新的事情,你总是需要扩展Classes。 (你正在以错误的方式重新定义方法)
在你的例子中你可以创建:
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);
}
});
而不是像这样使用它:
var policy = new MyPolicy();
canvas.installEditPolicy(policy);
另一种方法是直接使用JQuery:
$('#draw2Did').click(function(ev) {
alert("Mouse click:" + ev.clientX + "," +ev.clientY); }
);
参考:
http://draw2d.org/draw2d_touch/jsdoc_5/#!/api/draw2d.policy.canvas.CanvasPolicy