simulate a mouse down and up on a canvas html5 page

前端 未结 1 1157
小蘑菇
小蘑菇 2021-01-01 03:16

How can I simulate a mouse down and then followed by a mouse up event on a canvas hmtl5 section of an aspx page?

I searched on the web and found this... but i cannot

相关标签:
1条回答
  • 2021-01-01 04:18

    "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);
    

    See this old SO question for a pure javascript solution.

    0 讨论(0)
提交回复
热议问题