1

我对触摸处理程序有一个小问题......它有时在触摸时工作有时不工作,并且在我绘制它之后它无法读取我的数据,并且它以直线绘制,所以我想知道什么问题以及我做错了什么?请帮助我..我已经将我的代码放入 jsfiddle..请帮助我(http://jsfiddle.net/Frebu/1/)

function touchHandler(event) {
        var touches = event.changedTouches,
        first = touches[0],
        type = "";
        switch (event.type) {
            case "touchstart": type = "mousedown"; break;
            case "touchmove": type = "mousemove"; break;
            case "touchend": type = "mouseup"; break;
            default: return;
        }

        var simulatedEvent = document.createEvent("MouseEvent");
        simulatedEvent.initMouseEvent(type, true, true, window, 1,
                      first.screenX, first.screenY,
                      first.clientX, first.clientY, false,
                      false, false, false, 0/*left*/, null);

        first.target.dispatchEvent(simulatedEvent);
        event.preventDefault();
    }

    function init(id) {
        document.getElementById(id).addEventListener("touchstart", touchHandler, true);
        document.getElementById(id).addEventListener("touchmove", touchHandler, true);
        document.getElementById(id).addEventListener("touchend", touchHandler, true);
    }
$(document).ready(function() {
        init('myCanvas');
});​
4

1 回答 1

1

我的建议是使用hammer.js,它是一个很棒的触摸库(可以在浏览器上使用鼠标)。

http://eightmedia.github.com/hammer.js/

除此之外,我在模拟触摸事件的 Chrome 上测试小提琴没有任何问题。虽然很奇怪,但代码似乎很好。

于 2012-06-25T03:47:10.190 回答