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

Use correct pixel ratio when creating image buffer from map.addImage() #4645

Merged
merged 3 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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