How to Create a TouchEvent in Chrome?

陌路散爱 提交于 2019-12-12 13:33:26

问题


The W3C specification declares initTouchEvent as following:

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

However, when I try it in Chrome 44:

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

where touches is a valid TouchList, this is what Chrome created:

> 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

Look closely at the type field. Seems like Chrome is not following the spec where the 4th parameter turned into the type instead of the 1st parameter.

This brings us the question, that how do I actually create a TouchEvent in Chrome since it does not follow the spec?


回答1:


By looking at the Chromium source and Qiita (in Japanese), this seems like how its parameters are arranged:

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

Notice Chrome does not follow the W3C spec.


Relevant part in the Chromium source:

TouchEvent.cpp Line 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;
}


来源:https://stackoverflow.com/questions/31079014/how-to-create-a-touchevent-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!