diff --git a/src/dev/app.tsx b/src/dev/app.tsx index 000ca33..86e4601 100644 --- a/src/dev/app.tsx +++ b/src/dev/app.tsx @@ -3,21 +3,32 @@ import { render } from "hono/jsx/dom" const App = () => { const [spriteUrl, setSpriteUrl] = useState("") - const [width, setWidth] = useState(32) - const [height, setHeight] = useState(32) + const [imageWidth, setImageWidth] = useState(0) + const [imageHeight, setImageHeight] = useState(0) const [frames, setFrames] = useState(4) const [columns, setColumns] = useState() const [frameDuration, setFrameDuration] = useState(100) + const [scale, setScale] = useState(1) const handleFileChange = (e: Event) => { const file = (e.target as HTMLInputElement).files?.[0] if (!file) return - setSpriteUrl(URL.createObjectURL(file)) + const url = URL.createObjectURL(file) + const img = new Image() + img.onload = () => { + setImageWidth(img.width) + setImageHeight(img.height) + setSpriteUrl(url) + } + img.src = url } + // Auto-calculate frame dimensions from image size and frame count const isGrid = columns !== undefined const cols = isGrid ? columns : frames const rows = isGrid ? Math.ceil(frames / columns!) : 1 + const width = imageWidth > 0 ? Math.floor(imageWidth / cols) : 32 + const height = imageHeight > 0 ? Math.floor(imageHeight / rows) : 32 const sheetWidth = cols * width const sheetHeight = rows * height const totalDuration = frames * frameDuration @@ -41,10 +52,10 @@ const App = () => { } const previewStyle = { - width: `${width}px`, - height: `${height}px`, + width: `${width * scale}px`, + height: `${height * scale}px`, backgroundImage: `url('${spriteUrl}')`, - backgroundSize: `${sheetWidth}px ${sheetHeight}px`, + backgroundSize: `${sheetWidth * scale}px ${sheetHeight * scale}px`, animation: `${keyframeId} ${totalDuration}ms steps(${frames}) infinite`, } @@ -66,14 +77,12 @@ const App = () => { Spritesheet - - + {spriteUrl && ( +
+ Spritesheet ({imageWidth} x {imageHeight}) + +
+ )} + + {spriteUrl && ( + Frame size: {width} x {height} px + )}