[英]Can I detect if the mouse is currently moving inside a canvas draw function?
I am drawing a rectangle in HTML5 canvas. 我正在HTML5画布中绘制一个矩形。 I want it to be one color (transparent) when I am drawing it, and then another color (opaque) when I am done drawing it.
绘制时,我希望它是一种颜色(透明),绘制完后,我希望它是另一种颜色(不透明)。 Is there a way that I can say in my draw function, maybe using a conditional, to detect if the mouse is moving at this moment?
我可以在绘图函数中说出某种方式(也许使用条件)来检测此时鼠标是否在移动吗? ie
即
draw: function(ctx) {
ctx.beginPath();
ctx.rect(this.X, this.Y, this.Width, this.Height);
if(mouseisMoving) {
ctx.fillStyle = "rgba(0,0,0,1)";
}
else {
...
}
ctx.fill();
ctx.stroke();
}
Even better, change the color onleftmouseup. 更好的是,更改onleftmouseup的颜色。 However, because I am going through a framework and overriding a draw method, it would be better if I could do it inside the draw method.
但是,因为我要遍历一个框架并覆盖一个draw方法,所以如果可以在draw方法中进行操作会更好。 Itself.
本身。 Is this possible?
这可能吗?
You'll want to attach an onmousedown and onmouseup event listener to your canvas element that sets a boolean that's accessible by the scope of your draw function. 您需要将onmousedown和onmouseup事件侦听器附加到canvas元素,该元素设置一个布尔值,该布尔值可以通过draw函数的作用域访问。
Something like: 就像是:
var mouse = false;
canvas.onmousedown = function () {
mouse = true;
};
canvas.onmouseup = function () {
mouse = false;
};
var draw = function (ctx) {
if (mouse) {
ctx.fillStyle = "rgba(0,0,0,1)";
}
...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.