79512615

Date: 2025-03-16 13:39:24
Score: 0.5
Natty:
Report link

In this article I described in detail how to optimize the loading of a large number of images into Mapbox using a sprite file.

In short, my algorithm of actions was as follows:

- load the sprite and its metadata (a JSON file with information about where each image is located in the file), which we previously generated and stored on a server as static resources

- create an OffscreenCanvas and add the loaded sprite (image) to it

    const canvas = new OffscreenCanvas(width, height);
    const ctx = canvas.getContext('2d', { willReadFrequently: true });
    ctx.drawImage(image, 0, 0);

- for each required image, get it from the canvas as an image and add it to the mapbox

    const imageData = ctx.getImageData(x, y, width, height);
    map.addImage(imageId, imageData)
Reasons:
  • Blacklisted phrase (1): this article
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Igor Motovilov