canvas.onmousedown = function (event) {
console.log(JSON.stringify(event)); // this logs {"isTrusted":true}
};
相同宽度
canvas.addEventListener("mousedown", function (event) {
console.log(JSON.stringify(event)); // this logs {"isTrusted":true}
});
当我尝试只记录event
而不解析它时,应用程序崩溃了
那个沙箱有很多代码,我只关注你在这里的内容......
鼠标事件对象很复杂,很可能JSON.stringify
是走捷径;并纠正如果您尝试输出整个对象,它会很慢或者它可能会像您描述的那样崩溃,但是那里还有其他属性,您可以在下面的示例中看到它。
var canvas = document.querySelector("canvas")
var ctx = canvas.getContext("2d")
ctx.fillStyle = "red"
for (let i = 10; i < 100; i += 10 )
ctx.rect(i, i, 50, 50);
ctx.stroke();
canvas.onmousedown = function (event) {
console.log(JSON.stringify(event));
console.log(event.x, event.y);
ctx.beginPath()
ctx.arc(event.x, event.y, 5, 0, 8)
ctx.fill();
};
body { margin: 0 }
<canvas id="canvas"></canvas>
你可以看到我event.x, event.y
在用户点击的地方画圆圈。
您可以在官方文档中阅读有关可用属性的更多信息:
https ://developer.mozilla.org/en-US/docs/Web/API/MouseEvent#properties
我想你的最终目标不仅仅是输出:
console.log(JSON.stringify(event))
你还需要这些事件来做更多的事情......
也许最好问一个新问题......
在那里解释你在这些事件中试图做什么,如果有的话,你会遇到什么错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句