Jump to content

[SOLVED] How to continue moving camera after mouse outside of canvas?


ozRocker
 Share

Recommended Posts

Has anyone figured out how to still move the camera if the mouse pointer goes outside of the canvas?  I'm working with a canvas that is only a small part of the screen but I don't want to restrict dragging to just the canvas area.  It feels funny when you go outside the canvas then it stops moving

Link to comment
Share on other sites

53 minutes ago, JohnK said:

Will need a lot of work but an example anyway https://www.babylonjs-playground.com/#8JTMDX

Thank you!  That actually helped.  I used the directionX/Y, diff code from that to come up with my solution.

My canvas is sitting in an iFrame, so I used this code:

$(parent).on("pointermove", function(e) {
    if (mouseDown) {
        evt = e.originalEvent;
        var directionX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || 0;
        var directionY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || 0;

        diff = {x:-directionX /100, y:directionY /100}
    }
})

$(parent).on("pointerup", function(e) {
    mouseDown = false;
    triggerMouseEvent (canvas, "pointerup");
})

$(canvas).on("pointerdown", function(e) {
    mouseDown = true;
})

$(canvas).on("pointerup", function(e) {
    mouseDown = false;
})

then in the render loop

engine.runRenderLoop(function() {
   camera.alpha += diff.x;
   camera.beta -= diff.y;         
   diff.x *= 0.9;
   diff.y *= 0.9;
   scene.render();
});

Works nice and smooth regardless of the size of the iFrame

Link to comment
Share on other sites

  • ozRocker changed the title to [SOLVED] How to continue moving camera after mouse outside of canvas?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...