在画布html5页面上向下和向上模拟鼠标 [英] simulate a mouse down and up on a canvas html5 page
本文介绍了在画布html5页面上向下和向上模拟鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在aspx页面的画布hmtl5部分上模拟鼠标,然后是鼠标按下事件?
How can I simulate a mouse down and then followed by a mouse up event on a canvas hmtl5 section of an aspx page?
我在网上搜索并找到了这...但我无法将此与我的画布html5元素相关联,任何人都可以帮忙吗?
I searched on the web and found this... but i cannot correlate this to my canvas html5 element, Can anyone help ?
dispatchMouseEvent(element, 'mouseover', true, true);
dispatchMouseEvent(element, 'mousedown', true, true);
dispatchMouseEvent(element, 'click', true, true);
dispatchMouseEvent(element, 'mouseup', true, true);
推荐答案
模拟事件非常简单,事实上,你可以简单地触发它们。使用jQuery,这成为了孩子的游戏(参见这个jsfiddle 的工作示例):
"Simulating" events is very easy, in fact, you can simply trigger them. Using jQuery, this becomes child's play (see this jsfiddle for working example):
$('#canvas_element').on("mousedown mouseup", function(e) {
console.log(e.type + " event fired at coords: " + e.pageX + ", " + e.pageY);
});
x_coord = 1;
y_coord = 1;
var e = jQuery.Event( "mousedown", { pageX: x_coord, pageY: y_coord } );
$('#canvas_element').trigger(e);
// execute more code
x_coord = 255;
y_coord = 255;
var e = jQuery.Event( "mouseup", { pageX: x_coord, pageY: y_coord } );
$('#canvas_element').trigger(e);
参见这个旧的SO问题,用于纯粹的JavaScript解决方案。
See this old SO question for a pure javascript solution.
这篇关于在画布html5页面上向下和向上模拟鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文