Skip to content

Commit

Permalink
Use correct pixel ratio when creating image buffer from map.addImage() (
Browse files Browse the repository at this point in the history
#4645)

For images added using Mp.addImage - default to using pixel ratio : 1. When providing an Image HTMLElement, the pixel ratio can be adjusted by setting the img element's height and width accordingly. When providing an ArrayBuffer, we need to know the pixel ratio explicitly as already defined by the API.
  • Loading branch information
Asheem Mamoowala authored Apr 27, 2017
1 parent 495a695 commit 9ddd3cc
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 13 deletions.
18 changes: 7 additions & 11 deletions src/symbol/sprite_atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SpriteAtlas extends Evented {
this.data = false;
this.texture = 0; // WebGL ID
this.filter = 0; // WebGL ID
this.pixelRatio = 1;
this.pixelRatio = browser.devicePixelRatio > 1 ? 2 : 1;
this.dirty = true;
}

Expand Down Expand Up @@ -52,11 +52,11 @@ class SpriteAtlas extends Evented {
width = pixels.width;
height = pixels.height;
pixels = browser.getImageData(pixels);
pixelRatio = this.pixelRatio;
pixelRatio = 1;
} else {
width = options.width;
height = options.height;
pixelRatio = options.pixelRatio || this.pixelRatio;
pixelRatio = options.pixelRatio || 1;
}

if (ArrayBuffer.isView(pixels)) {
Expand All @@ -81,7 +81,7 @@ class SpriteAtlas extends Evented {
width: width / pixelRatio,
height: height / pixelRatio,
sdf: false,
pixelRatio: 1
pixelRatio: pixelRatio / this.pixelRatio
};
this.images[name] = image;

Expand Down Expand Up @@ -195,13 +195,9 @@ class SpriteAtlas extends Evented {
}

setSprite(sprite) {
if (sprite) {
this.pixelRatio = browser.devicePixelRatio > 1 ? 2 : 1;

if (this.canvas) {
this.canvas.width = this.width * this.pixelRatio;
this.canvas.height = this.height * this.pixelRatio;
}
if (sprite && this.canvas) {
this.canvas.width = this.width * this.pixelRatio;
this.canvas.height = this.height * this.pixelRatio;
}
this.sprite = sprite;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[
"addImage",
"marker",
"./image/marker.png"
"./sprites/1x.png"
],
[
"addLayer",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64,
"pixelRatio": 2,
"operations": [
[
"addImage",
"marker",
"./sprites/1x.png"
],
[
"addLayer",
{
"id": "geometry",
"type": "symbol",
"source": "geometry",
"layout": {
"icon-image": "marker"
}
}
],
[
"wait"
]
]
}
},
"sources": {
"geometry": {
"type": "geojson",
"data": {
"type": "Point",
"coordinates": [0, 0]
}
}
},
"layers": []
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64,
"pixelRatio": 1,
"operations": [
[
"addImage",
"marker",
"./sprites/2x.png",
2
],
[
"addLayer",
{
"id": "geometry",
"type": "symbol",
"source": "geometry",
"layout": {
"icon-image": "marker"
}
}
],
[
"wait"
]
]
}
},
"sources": {
"geometry": {
"type": "geojson",
"data": {
"type": "Point",
"coordinates": [0, 0]
}
}
},
"layers": []
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64,
"pixelRatio" :2,
"operations": [
[
"addImage",
"marker",
"./sprites/2x.png",
2
],
[
"addLayer",
{
"id": "geometry",
"type": "symbol",
"source": "geometry",
"layout": {
"icon-image": "marker"
}
}
],
[
"wait"
]
]
}
},
"sources": {
"geometry": {
"type": "geojson",
"data": {
"type": "Point",
"coordinates": [0, 0]
}
}
},
"layers": []
}
Binary file not shown.
3 changes: 2 additions & 1 deletion test/suite_implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ function applyOperations(map, operations, callback) {

} else if (operation[0] === 'addImage') {
const img = PNG.sync.read(fs.readFileSync(path.join(__dirname, './integration', operation[2])));
map.addImage(operation[1], img.data, {height: img.height, width: img.width, pixelRatio: 1});
const pixelRatio = operation.length > 3 ? operation[3] : 1;
map.addImage(operation[1], img.data, {height: img.height, width: img.width, pixelRatio: pixelRatio});
applyOperations(map, operations.slice(1), callback);

} else {
Expand Down

0 comments on commit 9ddd3cc

Please sign in to comment.