【问题标题】:Programmatically triggering mouse move event in Javascript在Javascript中以编程方式触发鼠标移动事件
【发布时间】:2012-03-17 12:15:39
【问题描述】:

我有外部 html5 画布,您可以使用鼠标在某些线条上进行绘制。我想以编程方式在这个画布上绘制一些东西,例如圆圈,但以动画的形式(不是一次绘画,而是模仿人类的方式,画圆圈让我们说 1 秒的持续时间。

外部代码不是我的,并且使用 GWT,这种方式是高度压缩和混淆的。这就是为什么我考虑触发一系列mousedown, mousemove, sleep, mousemove, mouseup 事件。我知道可以触发鼠标向下和向上事件,但是特定位置的鼠标移动事件呢?理想情况下使用 jQuery。

【问题讨论】:

  • 你能告诉我们你失败的尝试吗?
  • 不幸的是,我被困在如何制作鼠标移动事件上
  • 你真的需要触发鼠标事件吗?不使用 lineTo 和 moveTo 以编程方式绘制的原因是什么?在画布上绘图的教程 - html5canvastutorials.com/tutorials/html5-canvas-paths
  • 正如我所说,画布属于使用 GWT 制作的外部代码。画布上的那些画也执行一些其他代码。生成的 GWT 代码很难修改,这就是以编程方式触发事件的想法的原因。
  • 这个要点可能有用:gist.github.com/3517765

标签: javascript html5-canvas dom-events


【解决方案1】:

你看过initMouseEventdispatchEvent吗?

这是一个链接https://developer.mozilla.org/en/Document_Object_Model_%28DOM%29/event.initMouseEvent

【讨论】:

  • 现在已弃用。
【解决方案2】:

新的(非弃用)方法是使用MouseEvent 构造函数。

这里有一些示例代码,您可以根据自己的用例进行调整:

var gestureTimeoutID;
var periodicGesturesTimeoutID;

window.simulateRandomGesture = function (doneCallback) {
    var target = document.querySelector('canvas');

    var rect = target.getBoundingClientRect();

    var simulateMouseEvent = function simulateMouseEvent(type, point) {
        var event = new MouseEvent(type, {
            'view': window,
            'bubbles': true,
            'cancelable': true,
            'clientX': rect.left + point.x,
            'clientY': rect.top + point.y,
            // you can pass any other needed properties here
        });
        target.dispatchEvent(event);
    };

    var t = 0;

    /* Simple circle:
    var getPointAtTime = (t) => {
        return {
            x: 300 + Math.sin(t / 50) * 150,
            y: 300 + Math.cos(t / 50) * 150,
        };
    };
    */

    // More fun:
    var cx = Math.random() * rect.width;
    var cy = Math.random() * rect.height;
    var gestureComponents = [];
    var numberOfComponents = 5;
    for (var i = 0; i < numberOfComponents; i += 1) {
        gestureComponents.push({
            rx: Math.random() * Math.min(rect.width, rect.height) / 2 / numberOfComponents,
            ry: Math.random() * Math.min(rect.width, rect.height) / 2 / numberOfComponents,
            angularFactor: Math.random() * 5 - Math.random(),
            angularOffset: Math.random() * 5 - Math.random()
        });
    }
    var getPointAtTime = function getPointAtTime(t) {
        var point = { x: cx, y: cy };
        for (var i = 0; i < gestureComponents.length; i += 1) {
            var c = gestureComponents[i];
            point.x += Math.sin(Math.PI * 2 * (t / 100 * c.angularFactor + c.angularOffset)) * c.rx;
            point.y += Math.cos(Math.PI * 2 * (t / 100 * c.angularFactor + c.angularOffset)) * c.ry;
        }
        return point;
    };


    simulateMouseEvent('mousedown', getPointAtTime(t));
    var move = function move() {
        t += 1;
        if (t > 50) {
            simulateMouseEvent('mouseup', getPointAtTime(t));
            if (doneCallback) {
                doneCallback();
            }
        } else {
            simulateMouseEvent('mousemove', getPointAtTime(t));
            gestureTimeoutID = setTimeout(move, 10);
        }
    };
    move();
};

window.simulateRandomGesturesPeriodically = function (delayBetweenGestures) {
    delayBetweenGestures = delayBetweenGestures !== undefined ? delayBetweenGestures : 50;

    var waitThenGo = function waitThenGo() {
        periodicGesturesTimeoutID = setTimeout(function () {
            window.simulateRandomGesture(waitThenGo);
        }, delayBetweenGestures);
    };
    window.simulateRandomGesture(waitThenGo);
};

window.stopSimulatingGestures = function () {
    clearTimeout(gestureTimeoutID);
    clearTimeout(periodicGesturesTimeoutID);
};

【讨论】:

  • 你可能不应该对 new mouseEvent 的第二个参数的键进行字符串化。
  • @MathijsSegers 在功能上并不重要,但是是的,它们不需要在那里。我确定我从某个地方复制了那部分,然后只留下了引号。
  • VM2345:55 Uncaught ReferenceError: t is not defined
  • @AlexeySh。固定。
猜你喜欢
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
相关资源
最近更新 更多