vagran / dxf-viewer

DXF 2D viewer written in JavaScript
Mozilla Public License 2.0
290 stars 86 forks source link

How to get clientX and clientY on pointerdown event? #83

Closed sdsoldi closed 5 months ago

sdsoldi commented 10 months ago

I need to get the mouse click positions to find and select nearlest object. Searching in google threejs clientX and clientY are only in mouse (click, mousedown, mouseup) events.

My event has not clientX but has a

event {
    detail {
        "domEvent": {
            "isTrusted": true
        },
        "canvasCoord": {
            "x": 1061,
            "y": 104
        },
        "position": {
            "x": 6.269714285714287,
            "y": 9.19257142857143
        }
    }
}

Is event.detail.position.x the real click coordinates?

vagran commented 10 months ago

Is event.detail.position.x the real click coordinates?

This is scene coordinate, i.e. Three.js objects coordinate space. It can be converted to DXF model space by adding scene origin, which can be obtained by viewer.GetOrigin() method.

sdsoldi commented 10 months ago

Using viewer.GetOrigin() response is always

{
    "x": 0,
    "y": 0
}

I think I need the coordinates of the scene because I need to get the nearest mesh or line segments to select and hide if action required

vagran commented 10 months ago

Origin at [0;0] might happen if your DXF has some point at this position and is selected as origin. It may differ with other files. It will be quite difficult to determine DXF primitive from Three.js scene object since they are compiled rendering batches. In general, this feature was not really designed in current viewer version. There are plans to support such use cases in a future version, but currently the development is a bit stuck due to my busyness, so you will probably need a deep modification of the library to implement it. Some additional info can be found in this issue.