Files
gods-eye-view/src/layers/shared/render/imageAtlas.js
T
German Khokhlov 7abf74518f Cut duplicated and dead code across the engines, the layers and the QA scripts
- src/geo drops the ported debug argument checks and the tests that
  bitExact already covers; package boundaries accept a directory entry,
  so src/geo/ is listed once per group.
- Cesium no longer carries movers and lines nobody draws with: they are
  optional engine capabilities. The fake engine and the Cesium camera use
  the shared cameraMath; unused contract tables, clock.ticked, the
  'finest' surface detail and Navara click statistics are gone.
- ALPR keeps one overlay core and one presentation model for both
  engines, bikeshare one station bookkeeping, the Navara ground effects
  one definition helper; image atlases, view centres, mover banks and
  colours are shared.
- The legacy switch route, the clock offset and rate controls and other
  unused exports are removed.
- The acceptance runs the Navara draw, geo import and scope checks;
  scenarios use the harness switch and page helpers; the main chunk
  check keeps the size budget only and runs once in CI.
2026-11-05 13:32:39 +04:00

68 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Sprite atlases for the engine's mover sets: images packed into a grid of
// square cells, each `pad` px into its cell, with the [u0, v0, u1, v1]
// rectangle of every image.
/**
* The atlas layout (pure). Each entry is `{ key, width, height }`, the
* drawn size of its image.
* @param {{ key: string, width: number, height: number }[]} entries
* @param {{ cell: number, pad?: number, columns?: number }} options
* @returns {{ width: number, height: number,
* rects: { key: string, x: number, y: number, width: number, height: number }[],
* cells: Record<string, number[]> }}
*/
export function imageAtlasLayout(
entries,
{ cell, pad = 2, columns = Math.ceil(Math.sqrt(entries.length)) },
) {
const width = columns * cell;
const height = Math.ceil(entries.length / columns) * cell;
const cells = {};
const rects = entries.map((entry, index) => {
const x = (index % columns) * cell + pad;
const y = Math.floor(index / columns) * cell + pad;
cells[entry.key] = [
x / width,
y / height,
(x + entry.width) / width,
(y + entry.height) / height,
];
return { key: entry.key, x, y, width: entry.width, height: entry.height };
});
return { width, height, rects, cells };
}
/**
* Draws `{ key, image, width, height }` entries into one canvas at
* imageAtlasLayout's cells: at the image's own size, or scaled to
* width × height with `resize`.
* @returns {{ canvas: object, cells: Record<string, number[]> }}
*/
export function packImageAtlas(
entries,
{ canvas = document.createElement('canvas'), resize = false, ...options },
) {
const layout = imageAtlasLayout(entries, options);
canvas.width = layout.width;
canvas.height = layout.height;
const context = canvas.getContext('2d');
if (!context) throw new Error('Sprite atlas requires a canvas');
entries.forEach(({ image }, index) => {
const { x, y, width, height } = layout.rects[index];
if (resize) context.drawImage(image, x, y, width, height);
else context.drawImage(image, x, y);
});
return { canvas, cells: layout.cells };
}
/** Decodes image URLs (browser). */
export const decodeImages = (urls) =>
Promise.all(
urls.map(async (url) => {
const image = new Image();
image.src = url;
await image.decode();
return image;
}),
);