如何在Chrome中创建TouchEvent?

时间:2015-06-26 17:34:46

标签: javascript google-chrome javascript-events touch

W3C specification声明initTouchEvent如下:

void initTouchEvent (in DOMString    type,
                     in boolean      canBubble,
                     in boolean      cancelable,
                     in AbstractView view,
                     in long         detail,
                     in boolean      ctrlKey,
                     in boolean      altKey,
                     in boolean      shiftKey,
                     in boolean      metaKey,
                     in TouchList    touches,
                     in TouchList    targetTouches,
                     in TouchList    changedTouches);

但是,当我在Chrome 44中尝试时:

var e = document.createEvent('TouchEvent');
e.initTouchEvent("touchstart", true, true, window, 1,
                 false, false, false, false, touches, null, null);

其中touches是有效的TouchList,这就是Chrome创建的内容:

> TouchEvent {}
    altKey: false
    bubbles: true
    cancelBubble: false
    cancelable: true
    changedTouches: null
    charCode: 0
    ctrlKey: true
    currentTarget: null
    defaultPrevented: false
    detail: 0
    eventPhase: 0
    keyCode: 0
    layerX: 0
    layerY: 0
    metaKey: false
    pageX: 0
    pageY: 0
    path: Array[0]
    returnValue: true
    shiftKey: false
    srcElement: null
    target: null
    targetTouches: null
    timeStamp: 1435339572699
    touches: null
    type: "[object Window]"
    view: null
    which: 0

仔细查看type字段。好像Chrome没有遵循第4个参数转换为类型而不是第1个参数的规范。

这给我们带来了一个问题,即如何在Chrome中实际创建TouchEvent,因为它不符合规范?

1 个答案:

答案 0 :(得分:1)

通过查看Chromium sourceQiita (in Japanese),这看起来就像是如何安排参数:

initTouchEvent (TouchList touches,
                TouchList targetTouches,
                TouchList changedTouches,
                String type,
                Window view,
                number screenX,
                number screenY,
                number clientX,
                number clientY,
                boolean ctrlKey, 
                boolean altKey,
                boolean shiftKey,
                boolean metaKey);

注意Chrome不符合W3C规范。

Chromium来源中的相关部分:

TouchEvent.cpp第63行:

void TouchEvent::initTouchEvent(ScriptState* scriptState, TouchList* touches, TouchList* targetTouches,
        TouchList* changedTouches, const AtomicString& type,
        PassRefPtrWillBeRawPtr<AbstractView> view,
        int, int, int, int,
        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
{
    if (dispatched())
        return;

    if (scriptState->world().isIsolatedWorld())
        UIEventWithKeyState::didCreateEventInIsolatedWorld(ctrlKey, altKey, shiftKey, metaKey);

    bool cancelable = true;
    if (type == EventTypeNames::touchcancel)
        cancelable = false;

    initUIEvent(type, true, cancelable, view, 0);

    m_touches = touches;
    m_targetTouches = targetTouches;
    m_changedTouches = changedTouches;
    m_ctrlKey = ctrlKey;
    m_altKey = altKey;
    m_shiftKey = shiftKey;
    m_metaKey = metaKey;
}