-
Notifications
You must be signed in to change notification settings - Fork 919
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
How to handle window size, and cursor position on web targets? #1491
Comments
I don't think that's default behavior of winit, can you post a snippet of the code you're using? |
Sorry there's some context missing. -- My use case is a video game (ensure canvas is the same size as the client) and I'm resizing the canvas with CSS the typical way to achieve that effect: * {
margin:0;
padding:0;
overflow:hidden;
}
canvas {
width:100vw!important;
height:100vh!important;
} This resizes the canvas relative to the CSS layout, but it doesn't resize the canvases resolution, and Winit also doesn't throw a resize event on window resize, so I'm using let canvas = window.canvas();
let (w, h) = (canvas.client_width(), canvas.client_height());
canvas.set_width(w.try_into().unwrap());
canvas.set_height(h.try_into().unwrap()); After doing so I need to use the resulting The issue is resizing it like this is I'm not "always" using physical coordinates, because a canvas can have a higher or lower resolution then the physical screen, so when I go ahead and try to use the cursor event coordinates with my transforms they can offset incorrectly. It's a simple 2d game, so screen coordinates matching cursor coordinates exactly is important. The most notable time this occurs is when the user zooms in/out, which is how I discovered the issue:
For my use-case, this means that locking the page zoom at 100% and handling the resize from a WebGL transform would probably do the trick, otherwise one may calculate zoom factor, otherwise I'm still not entirely sure whether handling canvas resize this way is the best way to go about it. |
I looked into trying to get Winit to give me some form of dpi factor through I decided to solve my issue by applying the dpi with #[cfg(feature = "web")]
{
let canvas = window.canvas();
let (width, height) = (canvas.client_width(), canvas.client_height());
let factor = window.scale_factor();
let logical = LogicalSize { width, height };
let PhysicalSize { width, height } = logical.to_physical(factor);
canvas.set_width(width as u32);
canvas.set_height(height as u32);
(width, height)
} Looked into using
|
Why not listening window.addEventListener('resize', function() {
// Update canvas inner size
}) |
It seems like what's happening here is a breakdown of Winit's canvas DPI model. I don't think anyone accounted for using CSS to resize the canvas when we developed the web backend, so I'm not surprised that makes everything fall apart. I don't have the time this second to draw up a model of expected behavior when this occurs, but I think we should have one. |
A bit late, but since I just found this issue I should make some notes here:
The issue is that currently Having the Perhaps
It wasn't handled until recently with #1690 (only for the web-sys backend). |
Is there an actual solution to this? Would be nice if there was just an 'object' that can be called from the web/browser side that updates the canvas side based on the rust implementation.. like a simple hook or wrapper 🤔 |
Related: |
I think there are bunch of a different issues at play here:
I hope I correctly understood and summarized the problems of this issue. As far as I can tell they are all addressed or covered by other issues now, except the documentation, which I will address now. Please feel free to comment if I missed or misunderstood something! |
Following up on #2853, I will close this in favor of #1661 as well. |
I've ran into some pretty major issues when trying to handle window (aka: canvas) size on web targets. First i can't get window resize events, because CSS is quietly resizing the window to fit the screen, which means I need to call web-sys and see if it was resized every time before drawing, but now this means the cursors position is incorrect, because a canvas can have a higher or lower resolution then the actual window.
The text was updated successfully, but these errors were encountered: