Simulating native touch events

I am having a system which recives data samples of sensors in WebSocket and translates it into touch events.
The data is irelevant to the question, Eventually I have a x,y samples I need to simulate a touch event from.
I wrote a simple code to simulate the gestures using dispatchEvent

I wrote a code which simply stores the point detected in an object, and if it’s close to an existing point (within radius, TOW_POINT_DISTANCE_THRESHOLD) it declares it as an initial point for the later click recognition (distance of start and end points is smaller than a constant).
end point is determined if there are no other points for duration of MOUSE_INACTIVITY_THRESHOLD (80ms)

//simple code to simulate touch events from point in a loop
//point = {x:someX, y:someU}
let isFirst = true;
const startPointKey = Object.keys(activeTouchPoints.current).find((key) => {
    if (Math.hypot(activeTouchPoints.current[key].x - point.x, activeTouchPoints.current[key].y - point.y) <= TOW_POINT_DISTANCE_THRESHOLD) {
        isFirst = false;
        return true;
    }
}) || `${point.x},${point.y}`;

const theElement = (anyParent.ownerDocument || anyParent).elementFromPoint(point.x, point.y) as HTMLElement;
if (!theElement || !anyParent.contains(theElement)) return;

if (isFirst) {
    theElement.focus();
    activeTouchPoints.current[startPointKey] = {
        x: point.x,
        y: point.y,
        downX: point.x,
        downY: point.y,
    };


    const touch = new Touch({
        identifier: Date.now(),
        target: theElement,
        clientX: point.x,
        clientY: point.y,

    });
    const pointerOverEvent = new PointerEvent('pointerover', {
        bubbles: true,
        pointerId: touch.identifier,
        pointerType: 'touch',

        clientX: point.x,
        clientY: point.y,
    });

    theElement.dispatchEvent(pointerOverEvent);

    const pointerEnterEvent = new PointerEvent('pointerenter', {
        bubbles: true,
        pointerId: touch.identifier,
        pointerType: 'touch',
        clientX: point.x,
        clientY: point.y,
    });

    theElement.dispatchEvent(pointerEnterEvent);


    const pointerEvent = new PointerEvent('pointerdown', {
        bubbles: true,

        pointerId: touch.identifier,
        pointerType: 'touch',
        clientX: point.x,
        clientY: point.y,
    });

    theElement.dispatchEvent(pointerEvent);

    const touchEvent = new TouchEvent('touchstart', {
        bubbles: true,

        touches: [touch],
        targetTouches: [touch],
        changedTouches: [touch],
    });

    theElement.dispatchEvent(touchEvent);

    const gotPointerCaptureEvent = new PointerEvent('gotpointercapture', {
        bubbles: true,

        pointerId: touch.identifier,
        pointerType: 'touch',
        clientX: point.x,
        clientY: point.y,
    });

    theElement.dispatchEvent(gotPointerCaptureEvent);

    const mouseDownEvent = new MouseEvent('mousedown', {
        bubbles: true,


        clientX: point.x,
        clientY: point.y,
    });


    theElement.dispatchEvent(mouseDownEvent);




} else {
    // Touch move

    activeTouchPoints.current[startPointKey].x = point.x;
    activeTouchPoints.current[startPointKey].y = point.y;

    // Dispatch touchmove and mousemove events
    const touch = new Touch({
        identifier: Date.now(),
        target: theElement,
        clientX: point.x,
        clientY: point.y,

    });

    const pointerEvent = new PointerEvent('pointermove', {
        bubbles: true,


        pointerId: touch.identifier,
        pointerType: 'touch',
        clientX: point.x,
        clientY: point.y,
    });


    theElement.dispatchEvent(pointerEvent);



    const touchEvent = new TouchEvent('touchmove', {
        bubbles: true,

        touches: [touch],
        targetTouches: [touch],
        changedTouches: [touch],
    });



    theElement.dispatchEvent(touchEvent);


    // Reset inactivity timer
    if (activeTouchPoints.current[startPointKey].timerId) {
        clearTimeout(activeTouchPoints.current[startPointKey].timerId);
    }
}

// Set up inactivity timer for mouseup
activeTouchPoints.current[startPointKey].timerId = setTimeout(() => {

    const touch = new Touch({
        identifier: Date.now(),
        target: theElement,
        clientX: activeTouchPoints.current[startPointKey].x,
        clientY: activeTouchPoints.current[startPointKey].y,
    });


    const pointerEvent = new PointerEvent('pointerup', {
        bubbles: true,

        pointerId: touch.identifier,
        pointerType: 'touch',
        clientX: point.x,
        clientY: point.y,
        isPrimary: activeTouchPoints.current[startPointKey].isPrimary,
    });


    theElement.dispatchEvent(pointerEvent);




    const touchEvent = new TouchEvent('touchend', {
        bubbles: true,

        touches: [touch], //?
        targetTouches: [touch], //?
        changedTouches: [touch],
    });

    theElement.dispatchEvent(touchEvent);


    const mouseUpEvent = new MouseEvent('mouseup', {
        bubbles: true,
        clientX: point.x,
        clientY: point.y,
    });

    theElement.dispatchEvent(mouseUpEvent);
    // Check for click based on distance


    const distance = Math.hypot(
        activeTouchPoints.current[startPointKey].x - activeTouchPoints.current[startPointKey].downX,
        activeTouchPoints.current[startPointKey].y - activeTouchPoints.current[startPointKey].downY
    );

    if (distance <= CLICK_DISTANCE_THRESHOLD) {

        const clickEvent = new MouseEvent('click', {
            bubbles: true,

            clientX: activeTouchPoints.current[startPointKey].x,
            clientY: activeTouchPoints.current[startPointKey].y,
        });
        theElement.dispatchEvent(clickEvent);

    }

    // Clean up
    delete activeTouchPoints.current[startPointKey];

}, MOUSE_INACTIVITY_THRESHOLD);
}

I thought using it at the app level (aka using dispatchEvent) and not in the browser/system level, and that’s where the problem start.

The problem is that it doesn’t work for simple native components like Sliders which requires change event and don’t response to mouse/touch events, or advanced external components like SwipperJS for example which doesn’t response to those events also (even to clicks), and I am not even talking about scrolling a text on swipe if it’s long like in real device.

TL;DR: the problem is the simulating level, simulating directly at web level is problematic and not native (unless you have a clever general solution that will work everywhere).

I guess I have to simulate at the browser level (I originally didn’t mean to, because I wanted it to support also an external browser), luckily I can choose the main browser and I can use electron (which I currently use) or any other framework for running it.

I am looking for soluton to do this part the best way, is there any library (testing one maybe) that simulates those events at the user level? Or any general way to do it in electron (contolling the touch like it’s System’s)? / faking events at the system level for only this app?

My electron code eventally is very simple, it jsut open’s the app in a BrowserWindow with some args, I don’t insists in using electorn, if there is a better and easier way of doing it in other platform like CefSharp or anything else I would be glad to hear.

Any hint and help is appricated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật