Skip to content

Commit 798f00d

Browse files
committed
fix: refactor _updateTextures to remove code duplication
1 parent 113e8cd commit 798f00d

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

src/webgl/quadFeature.js

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -272,53 +272,41 @@ var webgl_quadFeature = function (arg) {
272272
* update them.
273273
*/
274274
this._updateTextures = function () {
275-
var texture;
276-
277275
$.each(m_quads.imgQuads, function (idx, quad) {
278-
if (!quad.image && !quad.imageTexture) {
276+
// pick source (imageTexture has priority)
277+
const source = quad.imageTexture || quad.image;
278+
if (!source) {
279279
return;
280280
}
281-
282-
// Handle custom imageTexture (from texture property)
281+
282+
// use cached texture if it exists
283+
if (source._texture) {
284+
quad.texture = source._texture;
285+
return;
286+
}
287+
288+
// create a new texture
289+
const texture = new vgl.texture();
283290
if (quad.imageTexture) {
284-
if (quad.imageTexture._texture) {
285-
quad.texture = quad.imageTexture._texture;
286-
} else {
287-
texture = new vgl.texture();
288-
texture.setTexture(quad.imageTexture);
289-
let nearestPixel = m_this.nearestPixel();
290-
if (nearestPixel !== undefined) {
291-
if (nearestPixel !== true && util.isNonNullFinite(nearestPixel)) {
292-
const curZoom = m_this.layer().map().zoom();
293-
nearestPixel = curZoom >= nearestPixel;
294-
}
295-
}
296-
if (nearestPixel) {
297-
texture.setNearestPixel(true);
298-
}
299-
quad.texture = quad.imageTexture._texture = texture;
300-
}
301-
} else if (quad.image) {
302-
// Handle regular image
303-
if (quad.image._texture) {
304-
quad.texture = quad.image._texture;
305-
} else {
306-
texture = new vgl.texture();
307-
texture.setImage(quad.image);
308-
let nearestPixel = m_this.nearestPixel();
309-
if (nearestPixel !== undefined) {
310-
if (nearestPixel !== true && util.isNonNullFinite(nearestPixel)) {
311-
const curZoom = m_this.layer().map().zoom();
312-
nearestPixel = curZoom >= nearestPixel;
313-
}
314-
}
315-
if (nearestPixel) {
316-
texture.setNearestPixel(true);
317-
}
318-
quad.texture = quad.image._texture = texture;
291+
texture.setTexture(source);
292+
} else {
293+
texture.setImage(source);
294+
}
295+
296+
// handle nearest pixel logic
297+
let nearestPixel = m_this.nearestPixel();
298+
if (nearestPixel !== undefined) {
299+
if (nearestPixel !== true && util.isNonNullFinite(nearestPixel)) {
300+
const curZoom = m_this.layer().map().zoom();
301+
nearestPixel = curZoom >= nearestPixel;
319302
}
320303
}
321-
});
304+
if (nearestPixel) {
305+
texture.setNearestPixel(true);
306+
}
307+
308+
quad.texture = source._texture = texture;
309+
});
322310
};
323311

324312
/**

0 commit comments

Comments
 (0)