Files
ben-to/apps/web/src/ui/builder-flow/SpriteTile.tsx
T
2026-07-21 16:50:58 +02:00

47 lines
1.4 KiB
TypeScript
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.
import type { CoverSprite } from "@ben-to/builder/domain";
import { publicAssetHref } from "./public-asset";
/**
* Tuile dune planche de sprites via `<img>` + décalage (plus fiable que `background-image` avec le runtime Solid).
*/
export const SpriteTile = (props: Readonly<{ sprite: CoverSprite; size?: number; class?: string }>) => {
const size = () => props.size ?? 32;
const scale = () => size() / props.sprite.cell;
const imgW = () => Math.round(props.sprite.sheetWidth * scale());
const imgH = () => Math.round(props.sprite.sheetHeight * scale());
const offsetLeft = () => Math.round(-props.sprite.col * size());
const offsetTop = () => Math.round(-props.sprite.row * size());
return (
<span
class={props.class}
aria-hidden="true"
style={{
display: "block",
width: `${size()}px`,
height: `${size()}px`,
overflow: "hidden",
position: "relative",
"flex-shrink": "0"
}}
>
<img
src={publicAssetHref(props.sprite.sheet)}
alt=""
width={imgW()}
height={imgH()}
draggable={false}
decoding="async"
style={{
position: "absolute",
left: `${offsetLeft()}px`,
top: `${offsetTop()}px`,
"max-width": "none",
width: `${imgW()}px`,
height: `${imgH()}px`,
"image-rendering": "pixelated"
}}
/>
</span>
);
};