如何在Forge查看器的2D绘图中获取X,Y坐标

时间:2018-01-08 00:11:21

标签: autodesk-forge autodesk-viewer

我想在鼠标点击时获得2D绘图的X和Y坐标。然后我可以在这个2D绘图上绘制一些东西。这是获取鼠标点击坐标的代码,但它不正确。

viewer.impl.canvas.addEventListener('mousedown', function (e) {
    // Get 2D drawing dimension
    var layoutBox = viewer.impl.getVisibleBounds();
    var width = layoutBox.max.x - layoutBox.min.x;
    var height = layoutBox.max.y - layoutBox.min.y;

    var viewport = viewer.impl.clientToViewport(e.clientX, e.clientY);
    var point = [viewport.x * width, viewport.y * height, viewport.z];

    // Show the 2D drawing X, Y coordinates on mouse click
    console.log(point);
});

1 个答案:

答案 0 :(得分:1)

使用custom viewer tool

尝试此操作
handleSingleClick (e, button) {

    var hitTest = this.viewer.clientToWorld(e.canvasX, e.canvasY, true)

    if (hitTest) {

        console.log(hitTest.point)
    }
}