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

Improved canvas size getter to support high-dpi #9

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

Conversation

jiakuan
Copy link

@jiakuan jiakuan commented Dec 8, 2022

In order to support high-dpi in HTML canvas, we need to scale the canvas size by devicePixelRatio first, and then use CSS style to configure the actual size, please see:
https://www.kirupa.com/canvas/canvas_high_dpi_retina.htm

The Canvas scaling part can be handled outside of this library, but in the get_size function, we need to get the size from the computed style, not the canvas, which may have been scaled.

.get_computed_style(&self.canvas)
.unwrap().unwrap();
let width = style.get_property_value("width").unwrap().replace("px", "");
let height = style.get_property_value("height").unwrap().replace("px", "");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can handle it better if the CSS uses a different unit, like pt or rem.
Also, error handlings can be improved.

Please let me know if there is an interest in merging the pull request, then I will improve it further. Otherwise, this code is good enough for my use case at the moment.

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.

1 participant