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

Detect retina displays and adjust tileSize accordingly #586

Merged
merged 5 commits into from
May 10, 2012
Merged

Detect retina displays and adjust tileSize accordingly #586

merged 5 commits into from
May 10, 2012

Conversation

Mithgol
Copy link
Contributor

@Mithgol Mithgol commented Mar 16, 2012

Should fix CloudMade/Leaflet#582.

Related external links:

@max-mapper
Copy link
Contributor

$ jake
Checking for JS errors...
src/layer/tile/TileLayer.js line 33 col 13   Bad line breaking before '&&'.
src/layer/tile/TileLayer.js line 34 col 13   Bad line breaking before '&&'.
src/layer/tile/TileLayer.js line 35 col 10   Missing space after ')'.
src/layer/tile/TileLayer.js line 36 col 35   Unexpected use of '>>='.
src/layer/tile/TileLayer.js line 38 col 38   Expected '{' and instead saw 'options'.
5 error(s) found.

jake aborted.
Error
    at /usr/local/lib/node_modules/jake/lib/api.js:228:13

@max-mapper
Copy link
Contributor

@Mithgol this passes lint (and should be fine unless tileSize / 2 isn't adequate):

// detecting retina displays, adjusting tileSize and zoom levels
if (this.options.detectRetina && window.devicePixelRatio > 1 && options.maxZoom > 0) {
    this.options.tileSize = this.options.tileSize / 2;
    options.zoomOffset++;
    if (options.minZoom > 0) {
        options.minZoom--;
    }
    options.maxZoom--;
}

basically if you can avoid bitwise operators in JS it is better since they are a) obscure and b) inefficient

@max-mapper
Copy link
Contributor

@Mithgol also when testing this (I merged it into leaflet's current master, here is the leaflet-src.js I compiled) I believe it is generating incorrect tile urls.

using:

var tiles ="http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg"
var layer = new L.TileLayer(tiles, {maxZoom: 16, minZoom: 3, detectRetina: true})

leaflet generates requests for tiles at URLs such as:

http://tile.stamen.com/terrain/12/1310/3165.jpg

which isn't a valid tile URL, though up one zoom level is:

http://tile.stamen.com/terrain/13/1310/3165.jpg

any ideas on why this is happening?

@max-mapper
Copy link
Contributor

figured it out - you were using options instead of this.options. I tested this on a retina device and can confirm it works (and lints)

// detecting retina displays, adjusting tileSize and zoom levels
if (this.options.detectRetina && window.devicePixelRatio > 1 && options.maxZoom > 0) {
    this.options.tileSize = this.options.tileSize / 2;
    this.options.zoomOffset++;
    if (this.options.minZoom > 0) {
        this.options.minZoom--;
    }
    this.options.maxZoom--;
}

@max-mapper
Copy link
Contributor

additionally we might want to check for window.devicePixelRatio === 2 specifically since certain devices have pixel ratios that aren't even integers

@Mithgol
Copy link
Contributor Author

Mithgol commented Apr 8, 2012

certain devices have pixel ratios that aren't even integers

Are there any devices with non-integer window.devicePixelRatio values? (I haven't yet heard of any.)

And if there are, what are their window.devicePixelRatio values? (I currently believe that any window.devicePixelRatio values above 1 are worthy of retina mode, but I'd consider switching to window.devicePixelRatio >= 1.5 approach if there are devices with window.devicePixelRatio values between 1 and 1.5).

@max-mapper
Copy link
Contributor

I believe android devices come in flavors of 0.75px, 1.0px, 1.5px and 2.0px (ratio 3:4:6:8) and iOS comes in 1.0px or 2.0px (ratio 1:2) so since tiles aren't available in 192px or 384px how would you support non integer pixel ratios?

@Mithgol
Copy link
Contributor Author

Mithgol commented Apr 8, 2012

how would you support non integer pixel ratios?

I'd just double tile pixel ratio for window.devicePixelRatio == 2 (that one is obvious) and for window.devicePixelRatio == 1.5 also (because downscaled tiles look better than upscaled), but won't do anything for the other two ratios — window.devicePixelRatio == 1 (that one is obvious) and window.devicePixelRatio == 0.75 (that won't benefit from high quality tiles anyway).

And my pull request already does all that (it activates only when window.devicePixelRatio > 1).

@max-mapper
Copy link
Contributor

@Mithgol that makes sense -- I think I was overthinking the problem :D

@max-mapper
Copy link
Contributor

@mourner +1'ing this pull req

@mourner
Copy link
Member

mourner commented Apr 17, 2012

Sorry for such a delay, I'll certainly merge this into master in the next couple of days.

@Mithgol
Copy link
Contributor Author

Mithgol commented Apr 17, 2012

While you are at it, please check whether I should have taken zoomReverse into account better. I am not 100% sure that I was right to ignore it entirely.

@Mithgol
Copy link
Contributor Author

Mithgol commented May 10, 2012

That's not a couple of days — more like a couple of dozens. Please provide a new estimated date of landing.

mourner added a commit that referenced this pull request May 10, 2012
Detect retina displays and adjust tileSize accordingly
@mourner mourner merged commit 1de934e into Leaflet:master May 10, 2012
@capncodewash
Copy link

Hi there,
There was an indentation error in this code - I've raised pull request #678 to sort it out. It was just a single character though, so you could just commit the same change if you like.

@mourner
Copy link
Member

mourner commented May 10, 2012

Thanks! I'll do a proper build with all the recently merged pulls today (needed to sort out some things in the code before).

@mourner
Copy link
Member

mourner commented May 10, 2012

Updated the build.

@mourner
Copy link
Member

mourner commented May 10, 2012

@Mithgol thanks again!

@Mithgol
Copy link
Contributor Author

Mithgol commented May 11, 2012

Завсегда пожалуйста.

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.

4 participants