pixel-search / src /lib /components /SpriteModal.svelte
cfahlgren1's picture
cfahlgren1 HF Staff
open sprites in a modal with png downloads when clicked
7b4904e
Raw History Blame Contribute Delete
5.7 kB
<script lang="ts">
import { onMount } from 'svelte';
interface Props {
sprite: number;
caption: string;
subject: string;
/** Null until the sprite's page has been decoded. */
pixels: Uint8Array<ArrayBuffer> | null;
failed: boolean;
onClose: () => void;
onSearch: (subject: string) => void;
}
const SPRITE_SIZE = 16;
const UPSCALED_SIZE = 512;
let { sprite, caption, subject, pixels, failed, onClose, onSearch }: Props = $props();
let dialog = $state<HTMLDialogElement | undefined>();
let downloadError = $state(false);
onMount(() => dialog?.showModal());
function paint(canvas: HTMLCanvasElement, rgba: Uint8Array<ArrayBuffer>): void {
const context = canvas.getContext('2d');
if (!context) throw new Error('2d canvas is unavailable');
context.putImageData(new ImageData(new Uint8ClampedArray(rgba.buffer), SPRITE_SIZE, SPRITE_SIZE), 0, 0);
}
function fileName(size: number): string {
const slug = subject.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40);
return `${slug || 'sprite'}-${sprite}-${size}px.png`;
}
function download(size: number): void {
if (!pixels) return;
const source = document.createElement('canvas');
source.width = SPRITE_SIZE;
source.height = SPRITE_SIZE;
paint(source, pixels);
const output = document.createElement('canvas');
output.width = size;
output.height = size;
const context = output.getContext('2d');
if (!context) throw new Error('2d canvas is unavailable');
context.imageSmoothingEnabled = false;
context.drawImage(source, 0, 0, size, size);
output.toBlob((blob) => {
downloadError = !blob;
if (!blob) return;
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = fileName(size);
link.click();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}, 'image/png');
}
function searchSubject(): void {
dialog?.close();
onSearch(subject);
}
function closeOnBackdrop(event: PointerEvent): void {
if (event.target === dialog) dialog?.close();
}
</script>
<dialog bind:this={dialog} class="modal" aria-label="Sprite: {subject}" onclose={onClose} onpointerdown={closeOnBackdrop}>
<div class="body">
<div class="heading">
<h2>{subject}</h2>
<button class="close" type="button" aria-label="Close" onclick={() => dialog?.close()}>×</button>
</div>
<div class="preview">
{#if pixels}
<canvas use:paint={pixels} width={SPRITE_SIZE} height={SPRITE_SIZE}></canvas>
{:else if failed}
<p>This sprite could not be loaded.</p>
{/if}
</div>
<p class="caption">{caption}</p>
<div class="actions">
<button class="primary" type="button" disabled={!pixels} onclick={() => download(SPRITE_SIZE)}>Download PNG · 16×16</button>
<button type="button" disabled={!pixels} onclick={() => download(UPSCALED_SIZE)}>Download large PNG</button>
<button class="wide" type="button" onclick={searchSubject}>Search “{subject}”</button>
</div>
{#if downloadError}
<p class="error" role="alert">The PNG could not be created.</p>
{/if}
</div>
</dialog>
<style>
.modal {
width: min(420px, calc(100vw - 32px));
max-height: calc(100dvh - 32px);
padding: 0;
overflow: auto;
overscroll-behavior: contain;
border: 2px solid var(--gold, #ffcf4a);
border-radius: 10px;
background: var(--panel, #1a1536);
color: var(--lavender, #c9c0f5);
box-shadow:
0 0 0 3px rgba(18, 13, 38, 0.9),
0 18px 60px rgba(0, 0, 0, 0.6);
}
.modal::backdrop {
background: rgba(18, 13, 38, 0.72);
}
.body {
display: flex;
flex-direction: column;
gap: 12px;
padding: 14px 16px 16px;
}
.heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
h2 {
min-width: 0;
margin: 0;
padding-top: 6px;
font-family: 'Silkscreen', ui-monospace, monospace;
font-weight: 400;
font-size: 18px;
line-height: 1.25;
color: var(--gold, #ffcf4a);
overflow-wrap: anywhere;
}
.close {
flex: none;
width: 40px;
height: 40px;
border: none;
background: transparent;
color: var(--lavender, #c9c0f5);
font-size: 28px;
line-height: 1;
cursor: pointer;
}
.preview {
display: grid;
place-items: center;
width: min(100%, 45dvh);
aspect-ratio: 1;
margin: 0 auto;
border: 1px solid var(--panel-border, #3a3160);
background-color: #2a2350;
background-image: conic-gradient(#352c61 25%, transparent 0 50%, #352c61 0 75%, transparent 0);
background-size: 12.5% 12.5%;
}
.preview canvas {
display: block;
width: 100%;
height: 100%;
image-rendering: pixelated;
}
.preview p {
margin: 0;
padding: 16px;
text-align: center;
}
.caption {
margin: 0;
font-size: 17px;
line-height: 1.3;
overflow-wrap: anywhere;
}
.actions {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr));
gap: 8px;
}
.actions button {
min-height: 44px;
padding: 6px 12px;
border: 1px solid var(--panel-border, #3a3160);
border-radius: 8px;
background: transparent;
color: var(--lavender, #c9c0f5);
font-family: inherit;
font-size: 16px;
overflow-wrap: anywhere;
cursor: pointer;
}
.actions .primary {
border-color: var(--gold, #ffcf4a);
background: var(--gold, #ffcf4a);
color: var(--bg-deep, #120d26);
}
.actions .wide {
grid-column: 1 / -1;
}
.actions button:disabled {
opacity: 0.5;
cursor: default;
}
.error {
margin: 0;
color: #ffb3b3;
}
@media (hover: hover) {
.actions button:not(.primary, :disabled):hover {
border-color: var(--gold, #ffcf4a);
color: var(--gold, #ffcf4a);
}
.close:hover {
color: var(--gold, #ffcf4a);
}
}
</style>