opensourceBIM / BIMsurfer

The latest version of the BIM Surfer WebGL viewer for IFC
MIT License
384 stars 132 forks source link

mouse position problem when scrolling #38

Open wisoo opened 5 years ago

wisoo commented 5 years ago

Hello Rubendel,

I noticed a problem in cameracontrol.js i've been using bimsurfer, i inserted the canvas in a custom webpage when scrolling the scroll offset was not taken into account so i had to click above or below the objects to be able to select them.

a quick fix to this is using your getCanvasPosFromEvent() function before executing pick()

    canvasMouseUp(e) {
        this.camera.orbitting = false;
        this.viewer.overlay.update();

this.getCanvasPosFromEvent(e, this.mousePos);

        var canvasMousePos = this.getCanvasPosFromEvent(e, this.mousePos);

        let dt = e.timeStamp - this.mouseDownTime;
        this.mouseDown = false;

        switch (e.which) {
            case 1:
                if (dt < 500. && this.closeEnoughCanvas(this.mouseDownPos, this.mousePos)) {
                  console.log(this.mousePos)
                    var viewObject = this.viewer.

canvasPos: this.mousePos,

                        canvasPos: canvasMousePos,
                        // Changed: for multiselect is used Ctrl key
                        shiftKey: e.ctrlKey
                    });
                    if (viewObject && viewObject.object) {
                        console.log("Picked", viewObject.object);
                    }
                    this.viewer.drawScene();
                }
                break;
        }
        e.preventDefault();
    } 

Hope this helps keep up the good work !

anhle1476 commented 2 years ago

At the current version (0.0.6 beta - 8/8/2022), the above fix no longer works.

this.getCanvasPosFromEvent(e, this.mousePos) is modified this.mousePos and then return that exact value => so the new var canvasMousePos will be the same as this.mousePos.

The problem here is in CameraControl.getCanvasPosFromEvent(), we're using event.pageX and event.pageY subtract the offset from event.target.getBoundingClientRect() to get the click position on the canvas. -> But getBoundingClientRect is provides the information of the canvas related to the viewport, not to the page -> My fix is to change any pageX to clientX and pageY to clientY in that getCanvasPosFromEvent method.

Thanks ^^