Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stage): recalculate mouse coordinates #1076

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stfujnkk
Copy link

@stfujnkk stfujnkk commented Oct 5, 2022

When the canvas scrolls or zooms, the coordinates of the mouse relative to the canvas are not updated unless you move or click the mouse.

When the canvas scrolls or zooms, the coordinates of the mouse relative to the canvas are not updated unless you move or click the mouse.
@stfujnkk
Copy link
Author

stfujnkk commented Oct 5, 2022

Added an exposed method(stage.recalculatePointerPosition) to update the mouse coordinates.

@stfujnkk
Copy link
Author

stfujnkk commented Oct 5, 2022

This is the test page that whether scrolling or zooming, mouseX and mouseY can be updated in time.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>EaselJS demo: hitTest</title>
    <style>
        body {
            margin: 0;
            padding: 7px;
            background-color: rgba(255, 255, 255, 0);
        }

        canvas {
            border: solid 1px rgba(0, 0, 0, 0.05);
        }
    </style>
    <script src="easeljs.min.js"></script>
</head>

<body>
    <div>
        <div style="width:200px;height:200px;background-color:#999999;overflow:auto;">
            <canvas id="demoCanvas" width="600" height="300"></canvas>
        </div>
    </div>
    <script>
        var stage, circle;
        function init() {
            stage = new createjs.Stage("demoCanvas");
            circle = stage.addChild(new createjs.Shape());
            circle.graphics.beginFill("red").drawCircle(50, 50, 50);
            circle.x = 0;
            circle.y = 0;
            createjs.Ticker.on("tick", tick);
        }

        function tick(event) {
            circle.alpha = 0.2;
            stage.recalculatePointerPosition();
            if (circle.hitTest(stage.mouseX, stage.mouseY)) {
                circle.alpha = 1;
            }
            stage.update(event);
        }
        init();
    </script>
</body>

</html>

@stfujnkk
Copy link
Author

stfujnkk commented Oct 5, 2022

Added an open method(stage.recalculatePointerPosition) to update the mouse coordinates.

If you do not want to add external interfaces, you can achieve the same effect by adding a listener for the resize and scroll events.

@danzen
Copy link
Contributor

danzen commented Sep 10, 2024

Hi @stfujnkk - sorry to get here late. How are these changes going for you? Still working out okay? I verified that it works - thanks. Just trying to figure out if we should be checking screen ratio every mouse move in order to know the mouse position if it has not moved.

As it is now, after zooming the browser, the mouse may be considered not hitting something or rolling over something until it is moved again. And perhaps the opposite too - all of a sudden it would seem to be hitting something or rolling over something that is not really under the mouse.

Your solution currently requires people to still add a test and call the recalculatePointerPosition() - like in a Ticker or an event. I think for now, you have shown how this can be solved and if people are worried about it, they can implement your change. We will leave the master without the update. Anyone reading this, is welcome to weigh in. And thanks again for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants