-
-
Notifications
You must be signed in to change notification settings - Fork 705
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
Allow setting a custom pixel ratio #769
Comments
I'm not sure this should be a map property and not a global property like base url and access token were...? |
I'm not sure... Having it as a global property would be a lot less useful in my opinion, since we wouldn't be able to set it on a per-map basis. It can be useful to create a separate map for exporting to an image, and having it as a global property would unnecessarily affect all maps. |
FWIW, this seems to have been requested for some time in Mapbox GL's issue tracker: mapbox/mapbox-gl-js#1953. Someone even proposed a similar solution: mapbox/mapbox-gl-js#1953 (comment). |
Another possibility could be to have a global setting, but also allow it to be overridden on a per-map basis. |
I see you point, both global and per map is a bit confusing I think. |
Thank you! |
Thank you very much @vanilla-lake for this PR. I like the possibility to set However, it is also the case that one has to be careful when changing the Should we provide a fallback mechanism for these? I know that the value 9 for devicePixelRatio is quite high. But as it is at the moment, it can simply be entered. You can even use 100 as a value. In the latter case, I am also shown a map section that is too small in Chrome without seeing an error in the browser console. If we leave it like this, then I would integrate the function with the note "at your own risk", like #574.
|
Great catch @astridx! Thanks for pointing that out. I can't really think of a fallback beyond rendering into multiple smaller canvases, but please let me know if you have any suggestions. However, since not all browsers display a warning, maybe we could display one ourselves when the canvas' width or height exceed the value returned by |
Is it ok if I close the issue because we have a PR #772 ? |
@astridx That is fine with me. |
As I mentioned in #769 (comment), we should add this feature. The problems I had there have nothing to do with this PR directly. Since others have also commented positively here and no one has anything against it, I am now merging the PR. I hope this is fine. Thank you very much @vanilla-lake
Motivation
When exporting a map to an image, there are many cases where we might want the image to have a different size in pixels than the in-app map all while keeping the same extent and zoom level. In other words, we want to scale the map. One reason to do this is to allow exporting at a certain DPI, like https://github.com/watergis/maplibre-gl-export does.
Design Alternatives
The way maplibre-gl-export addresses this problem is by overriding the
Window#devicePixelRatio
property: https://github.com/watergis/maplibre-gl-export/blob/3977f96e829bf589d9988270da26444fdab807d9/lib/map-generator.ts#L148-L151While it works, it relies on changing some global state, which could easily have some unintended consequences, especially when the code is asynchronous.
Design
We could add a new
MapOptions#pixelRatio
property along with a newMap#setPixelRatio
method. Instead of using theWindow#devicePixelRatio
property, maps would now keep their own pixel ratio value. In addition to making the above use case much less brittle, this would make testing easier as there would be no more need to override theWindow#devicePixelRatio
property.One drawback is that it adds a bit more complexity to the
Map
interface. However, since maps' pixel ratio would default to the value stored in theWindow#devicePixelRatio
property whenMapOptions#pixelRatio
is not specified andMap#setPixelRatio
has not been called, users would not have to think about this unless they need the new functionality, so existing code could remain unchanged.Mock-Up
As an example, maplibre-gl-export could get rid of the code snippet linked above and do something like the following at https://github.com/watergis/maplibre-gl-export/blob/3977f96e829bf589d9988270da26444fdab807d9/lib/map-generator.ts#L162-L175:
Concepts
Some documentation will have to be written for the above additions to the public interface. The terminology we should use is "pixel ratio" as that will already be familiar to Web developers because of the
Window#devicePixelRatio
property. It is also already used internally by MapLibre GL JS.Implementation
We could add a new
MapOptions#pixelRatio
property, along with some newMap#setPixelRatio
andMap#getPixelRatio
methods.Internally, the code would need to keep its own pixel ratio value instead of reading the
Window#devicePixelRatio
property.The text was updated successfully, but these errors were encountered: