Skip to content

Commit

Permalink
Allow using tileSize: 512 as a switch to trade retina support for 512…
Browse files Browse the repository at this point in the history
…px raster tiles
  • Loading branch information
yhahn authored and jfirebaugh committed Feb 11, 2016
1 parent c33f075 commit e752a88
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ RasterTileSource.prototype = util.inherit(Evented, {
getTile: Source._getTile,

_loadTile: function(tile) {
var url = normalizeURL(tile.coord.url(this.tiles), this.url);
var url = normalizeURL(tile.coord.url(this.tiles), this.url, this.tileSize);

tile.request = ajax.getImage(url, done.bind(this));

Expand Down
8 changes: 6 additions & 2 deletions js/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ module.exports.normalizeSpriteURL = function(url, format, ext, accessToken) {
return normalizeURL('mapbox://' + user + '/' + style + draft + '/sprite' + format + ext, '/styles/v1/', accessToken);
};

module.exports.normalizeTileURL = function(url, sourceUrl) {
module.exports.normalizeTileURL = function(url, sourceUrl, tileSize) {
if (!sourceUrl || !sourceUrl.match(/^mapbox:\/\//))
return url;

// The v4 mapbox tile API supports 512x512 image tiles only when @2x
// is appended to the tile URL. If `tileSize: 512` is specified for
// a Mapbox raster source force the @2x suffix even if a non hidpi
// device.
url = url.replace(/([?&]access_token=)tk\.[^&]+/, '$1' + config.ACCESS_TOKEN);
var extension = browser.supportsWebp ? 'webp' : '$1';
return url.replace(/\.((?:png|jpg)\d*)(?=$|\?)/, browser.devicePixelRatio >= 2 ? '@2x.' + extension : '.' + extension);
return url.replace(/\.((?:png|jpg)\d*)(?=$|\?)/, browser.devicePixelRatio >= 2 || tileSize === 512 ? '@2x.' + extension : '.' + extension);
};
8 changes: 8 additions & 0 deletions test/js/util/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ test("mapbox", function(t) {
t.end();
});

t.test('inserts @2x when tileSize == 512', function(t) {
t.equal(mapbox.normalizeTileURL('http://path.png/tile.png', mapboxSource, 512), 'http://path.png/[email protected]');
t.equal(mapbox.normalizeTileURL('http://path.png/tile.png32', mapboxSource, 512), 'http://path.png/[email protected]');
t.equal(mapbox.normalizeTileURL('http://path.png/tile.jpg70', mapboxSource, 512), 'http://path.png/[email protected]');
t.equal(mapbox.normalizeTileURL('http://path.png/tile.png?access_token=foo', mapboxSource, 512), 'http://path.png/[email protected]?access_token=foo');
t.end();
});

t.test('replaces img extension with webp on supporting devices', function(t) {
browser.supportsWebp = true;
t.equal(mapbox.normalizeTileURL('http://path.png/tile.png', mapboxSource), 'http://path.png/tile.webp');
Expand Down

0 comments on commit e752a88

Please sign in to comment.