fix: improve previews for small sprites (#2370)

# fix: improve previews for small sprites

## Description

Small sprites appeared at their original display size in the sprite
picker, making them difficult to recognize when choosing an icon or
pattern.

- Render previews in a fixed 36 × 36 CSS pixel canvas. Scale and center
each sprite while preserving its aspect ratio.
- Prefer matching `@2x.json` metadata and `@2x.png` atlas for previews.
Use the base atlas when `@2x` metadata is unavailable.
- Size the canvas backing store for the display's device pixel ratio.
Preserve query strings and fragments when building sprite URLs.

Follow-up to [#2364](https://github.com/maplibre/maputnik/pull/2364),
which extended sprite previews to all sprite-backed fields as part of
[#1294](https://github.com/maplibre/maputnik/issues/1294).

## Visuals

### Before

Small sprites appear at their original display size in the picker.

![Small oneway sprite before the
change](https://github.com/user-attachments/assets/91c353fc-2ea0-4550-9554-94dcf83ad4fa)

### After

The same sprites are scaled to fit the fixed-size preview using the
`@2x` atlas.

![Larger oneway sprite after the
change](https://github.com/user-attachments/assets/aab062a8-13b4-4e2a-8d59-8652be7d731f)

## Verification

All checks for this change passed.

- Playwright covers the `@2x` atlas, the centered 36 × 36 preview for a
small rectangular sprite, the 72 × 72 canvas at device pixel ratio 2,
and the base atlas fallback. Existing coverage exercises SDF, sprite
selection, image failures, and catalog updates.
- `npm run lint`
- `npm run lint-css`
- `npm run build`
- `npm run test-unit -- --run` (55 passed)
- `npm run test -- e2e/sprite-picker.spec.ts --workers=2` (21 passed)
- Confirmed that deliberately changing the expected CSS width, backing
canvas width, and fallback pixel color fails the corresponding tests.
Restored the correct assertions.

## Launch Checklist

- [x] Briefly describe the changes in this PR.
- [x] Link to related issues.
- [x] Include before/after visuals or GIFs if this PR includes visual
changes.
- [x] Write tests for all new functionality.
- [x] Add an entry to `CHANGELOG.md` under the `## main` section.

Co-authored-by: FantasticSPM <asd507172@gmail.com>
This commit is contained in:
FantasticSPMandFantasticSPM authored and GitHub committed 2026-11-26 23:09:35 +02:00
1 parent 2b2b433189
commit 648831ac67
11 files changed
+111 -30

No files matched your search

+2 -3
View File
@@ -2,7 +2,7 @@ import React from "react";
import classnames from "classnames";
import { useCombobox } from "downshift";
import { SpriteCatalogContext } from "./SpriteCatalogContext";
import { drawSprite, type Sprite } from "../libs/sprites";
import { drawSprite, SPRITE_PREVIEW_SIZE, type Sprite } from "../libs/sprites";
function SpritePreview({ sprite, atlas }: { sprite: Sprite; atlas?: HTMLImageElement }) {
const canvas = React.useRef<HTMLCanvasElement>(null);
@@ -15,10 +15,9 @@ function SpritePreview({ sprite, atlas }: { sprite: Sprite; atlas?: HTMLImageEle
canvas.current.width = 0;
}
}, [atlas, sprite]);
const scale = Math.min(1 / sprite.pixelRatio, 32 / Math.max(sprite.width, sprite.height));
return <span className="maputnik-sprite-preview" aria-hidden="true">
{atlas && <canvas ref={canvas} data-wd-key={`sprite-preview:${sprite.id}`}
style={{ width: sprite.width * scale, height: sprite.height * scale }} />}
style={{ width: SPRITE_PREVIEW_SIZE, height: SPRITE_PREVIEW_SIZE }} />}
</span>;
}