Skip to content

Commit 9ddd3cc

Browse files
author
Asheem Mamoowala
authored
Use correct pixel ratio when creating image buffer from map.addImage() (#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.
1 parent 495a695 commit 9ddd3cc

File tree

11 files changed

+135
-13
lines changed

11 files changed

+135
-13
lines changed

src/symbol/sprite_atlas.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SpriteAtlas extends Evented {
2121
this.data = false;
2222
this.texture = 0; // WebGL ID
2323
this.filter = 0; // WebGL ID
24-
this.pixelRatio = 1;
24+
this.pixelRatio = browser.devicePixelRatio > 1 ? 2 : 1;
2525
this.dirty = true;
2626
}
2727

@@ -52,11 +52,11 @@ class SpriteAtlas extends Evented {
5252
width = pixels.width;
5353
height = pixels.height;
5454
pixels = browser.getImageData(pixels);
55-
pixelRatio = this.pixelRatio;
55+
pixelRatio = 1;
5656
} else {
5757
width = options.width;
5858
height = options.height;
59-
pixelRatio = options.pixelRatio || this.pixelRatio;
59+
pixelRatio = options.pixelRatio || 1;
6060
}
6161

6262
if (ArrayBuffer.isView(pixels)) {
@@ -81,7 +81,7 @@ class SpriteAtlas extends Evented {
8181
width: width / pixelRatio,
8282
height: height / pixelRatio,
8383
sdf: false,
84-
pixelRatio: 1
84+
pixelRatio: pixelRatio / this.pixelRatio
8585
};
8686
this.images[name] = image;
8787

@@ -195,13 +195,9 @@ class SpriteAtlas extends Evented {
195195
}
196196

197197
setSprite(sprite) {
198-
if (sprite) {
199-
this.pixelRatio = browser.devicePixelRatio > 1 ? 2 : 1;
200-
201-
if (this.canvas) {
202-
this.canvas.width = this.width * this.pixelRatio;
203-
this.canvas.height = this.height * this.pixelRatio;
204-
}
198+
if (sprite && this.canvas) {
199+
this.canvas.width = this.width * this.pixelRatio;
200+
this.canvas.height = this.height * this.pixelRatio;
205201
}
206202
this.sprite = sprite;
207203
}
Loading

test/integration/render-tests/runtime-styling/image-add/style.json renamed to test/integration/render-tests/runtime-styling/image-add-1x-image-1x-screen/style.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[
99
"addImage",
1010
"marker",
11-
"./image/marker.png"
11+
"./sprites/1x.png"
1212
],
1313
[
1414
"addLayer",
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": 8,
3+
"metadata": {
4+
"test": {
5+
"width": 64,
6+
"height": 64,
7+
"pixelRatio": 2,
8+
"operations": [
9+
[
10+
"addImage",
11+
"marker",
12+
"./sprites/1x.png"
13+
],
14+
[
15+
"addLayer",
16+
{
17+
"id": "geometry",
18+
"type": "symbol",
19+
"source": "geometry",
20+
"layout": {
21+
"icon-image": "marker"
22+
}
23+
}
24+
],
25+
[
26+
"wait"
27+
]
28+
]
29+
}
30+
},
31+
"sources": {
32+
"geometry": {
33+
"type": "geojson",
34+
"data": {
35+
"type": "Point",
36+
"coordinates": [0, 0]
37+
}
38+
}
39+
},
40+
"layers": []
41+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": 8,
3+
"metadata": {
4+
"test": {
5+
"width": 64,
6+
"height": 64,
7+
"pixelRatio": 1,
8+
"operations": [
9+
[
10+
"addImage",
11+
"marker",
12+
"./sprites/2x.png",
13+
2
14+
],
15+
[
16+
"addLayer",
17+
{
18+
"id": "geometry",
19+
"type": "symbol",
20+
"source": "geometry",
21+
"layout": {
22+
"icon-image": "marker"
23+
}
24+
}
25+
],
26+
[
27+
"wait"
28+
]
29+
]
30+
}
31+
},
32+
"sources": {
33+
"geometry": {
34+
"type": "geojson",
35+
"data": {
36+
"type": "Point",
37+
"coordinates": [0, 0]
38+
}
39+
}
40+
},
41+
"layers": []
42+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": 8,
3+
"metadata": {
4+
"test": {
5+
"width": 64,
6+
"height": 64,
7+
"pixelRatio" :2,
8+
"operations": [
9+
[
10+
"addImage",
11+
"marker",
12+
"./sprites/2x.png",
13+
2
14+
],
15+
[
16+
"addLayer",
17+
{
18+
"id": "geometry",
19+
"type": "symbol",
20+
"source": "geometry",
21+
"layout": {
22+
"icon-image": "marker"
23+
}
24+
}
25+
],
26+
[
27+
"wait"
28+
]
29+
]
30+
}
31+
},
32+
"sources": {
33+
"geometry": {
34+
"type": "geojson",
35+
"data": {
36+
"type": "Point",
37+
"coordinates": [0, 0]
38+
}
39+
}
40+
},
41+
"layers": []
42+
}
Binary file not shown.

test/suite_implementation.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function applyOperations(map, operations, callback) {
104104

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

110111
} else {

0 commit comments

Comments
 (0)