Compare commits
10 Commits
fc44f61993
...
90c812636b
| Author | SHA1 | Date | |
|---|---|---|---|
| 90c812636b | |||
| a8717733ba | |||
| 311b91d0c5 | |||
| 6dcfcdce2a | |||
| 6f67b133fc | |||
| 52e6adc995 | |||
| b1a87662fb | |||
| d3e1eb00bd | |||
| 746528f6ab | |||
| a83f4ece7a |
148
CLAUDE.md
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
# tiny-sprites
|
||||
|
||||
See [README.md](./README.md) for the API and usage documentation.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Hono JSX (not React)
|
||||
|
||||
This project uses Hono's JSX implementation, not React. Key differences:
|
||||
|
||||
- **Server-side**: `hono/jsx` for the Sprite component (renders to HTML strings)
|
||||
- **Client-side**: `hono/jsx/dom` for the dev tool UI (renders to DOM)
|
||||
- **No hooks**: The Sprite component is pure server-side JSX with zero client JavaScript. No `useState`, no `useEffect`. The dev tool uses vanilla JS with a simple mutable `state` object and manual DOM updates via `render()` calls.
|
||||
|
||||
### Dev Tool Styling
|
||||
|
||||
The dev tool uses [Pico CSS](https://picocss.com) for styling:
|
||||
|
||||
- Classless styling - semantic HTML gets styled automatically
|
||||
- `<details class="dropdown">` for the image picker (native open/close, no JS needed)
|
||||
- CSS variables like `var(--pico-primary-background)` for theming
|
||||
|
||||
### Interesting Implementation Details
|
||||
|
||||
**CSS Variables in @keyframes**: Instead of generating unique keyframes per sprite, we define one global keyframe that uses CSS custom properties:
|
||||
|
||||
```css
|
||||
@keyframes sprite {
|
||||
from { background-position: var(--x) var(--y) }
|
||||
to { background-position: var(--ex) var(--y) }
|
||||
}
|
||||
```
|
||||
|
||||
Each sprite just sets `--x`, `--y`, `--ex` as inline styles. This avoids `dangerouslySetInnerHTML` entirely.
|
||||
|
||||
**Frame Detection Algorithm**: The analyzer (`src/dev/analyze.ts`) scans pixel columns to find content regions. It requires a minimum 10px gap between regions to count as separate frames - this handles sprites that have internal empty columns within a frame.
|
||||
|
||||
**Crop Calculation**: For each frame, we find the bounding box of non-transparent pixels, then union all frames' bounds. This gives a consistent crop that works across all animation frames without the sprite "jumping".
|
||||
|
||||
**Canvas Pixel Analysis**: We draw the image to a canvas and use `getImageData()` to read pixel alpha values. The alpha byte is at index `(y * width + x) * 4 + 3` in the flat pixel array.
|
||||
|
||||
---
|
||||
|
||||
Default to using Bun instead of Node.js.
|
||||
|
||||
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
|
||||
- Use `bun test` instead of `jest` or `vitest`
|
||||
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
|
||||
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
|
||||
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
|
||||
- Use `bunx <package> <command>` instead of `npx <package> <command>`
|
||||
- Bun automatically loads .env, so don't use dotenv.
|
||||
|
||||
## APIs
|
||||
|
||||
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
|
||||
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
|
||||
- `Bun.redis` for Redis. Don't use `ioredis`.
|
||||
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
|
||||
- `WebSocket` is built-in. Don't use `ws`.
|
||||
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
|
||||
- Bun.$`ls` instead of execa.
|
||||
|
||||
## Testing
|
||||
|
||||
Use `bun test` to run tests.
|
||||
|
||||
```ts#index.test.ts
|
||||
import { test, expect } from "bun:test";
|
||||
|
||||
test("hello world", () => {
|
||||
expect(1).toBe(1);
|
||||
});
|
||||
```
|
||||
|
||||
## Frontend
|
||||
|
||||
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
|
||||
|
||||
Server:
|
||||
|
||||
```ts#index.ts
|
||||
import index from "./index.html"
|
||||
|
||||
Bun.serve({
|
||||
routes: {
|
||||
"/": index,
|
||||
"/api/users/:id": {
|
||||
GET: (req) => {
|
||||
return new Response(JSON.stringify({ id: req.params.id }));
|
||||
},
|
||||
},
|
||||
},
|
||||
// optional websocket support
|
||||
websocket: {
|
||||
open: (ws) => {
|
||||
ws.send("Hello, world!");
|
||||
},
|
||||
message: (ws, message) => {
|
||||
ws.send(message);
|
||||
},
|
||||
close: (ws) => {
|
||||
// handle close
|
||||
}
|
||||
},
|
||||
development: {
|
||||
hmr: true,
|
||||
console: true,
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
|
||||
|
||||
```html#index.html
|
||||
<html>
|
||||
<body>
|
||||
<h1>Hello, world!</h1>
|
||||
<script type="module" src="./frontend.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
With the following `frontend.tsx`:
|
||||
|
||||
```tsx#frontend.tsx
|
||||
import React from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
|
||||
// import .css files directly and it works
|
||||
import './index.css';
|
||||
|
||||
const root = createRoot(document.body);
|
||||
|
||||
export default function Frontend() {
|
||||
return <h1>Hello, world!</h1>;
|
||||
}
|
||||
|
||||
root.render(<Frontend />);
|
||||
```
|
||||
|
||||
Then, run index.ts
|
||||
|
||||
```sh
|
||||
bun --hot ./index.ts
|
||||
```
|
||||
|
||||
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.mdx`.
|
||||
78
README.md
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
# tiny-sprites
|
||||
|
||||
A tiny CSS sprite animation component for Hono JSX. Pure CSS animations with zero runtime JavaScript.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
bun add tiny-sprites
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```tsx
|
||||
import { Sprite, SpriteStyles } from "tiny-sprites"
|
||||
|
||||
export default function Page() {
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<SpriteStyles />
|
||||
</head>
|
||||
<body>
|
||||
<Sprite
|
||||
src="/sprites/player.png"
|
||||
frames={4}
|
||||
frameDuration={100}
|
||||
sheetWidth={128}
|
||||
sheetHeight={32}
|
||||
/>
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `<SpriteStyles />`
|
||||
|
||||
Renders a global `@keyframes` rule. Include once in your `<head>`.
|
||||
|
||||
### `<Sprite />`
|
||||
|
||||
Renders an animated sprite.
|
||||
|
||||
| Prop | Type | Required | Default | Description |
|
||||
|------|------|----------|---------|-------------|
|
||||
| `src` | `string` | Yes | | URL to the spritesheet image |
|
||||
| `frames` | `number` | Yes | | Number of frames in the animation |
|
||||
| `frameDuration` | `number` | Yes | | Duration of each frame in milliseconds |
|
||||
| `sheetWidth` | `number` | Yes | | Width of the spritesheet in pixels |
|
||||
| `sheetHeight` | `number` | Yes | | Height of the spritesheet in pixels |
|
||||
| `crop` | `Crop` | No | | Crop each frame to a smaller region |
|
||||
| `scale` | `number` | No | `1` | Scale factor for rendering |
|
||||
| `class` | `string` | No | | CSS class name |
|
||||
| `style` | `string` | No | | Additional inline styles |
|
||||
| `playing` | `boolean` | No | `true` | Whether the animation is playing |
|
||||
|
||||
#### Crop
|
||||
|
||||
```ts
|
||||
type Crop = {
|
||||
x: number // X offset within each frame
|
||||
y: number // Y offset within each frame
|
||||
width: number // Cropped width
|
||||
height: number // Cropped height
|
||||
}
|
||||
```
|
||||
|
||||
## Dev Tool
|
||||
|
||||
A dev server is included to help tune sprite animations and auto-detect frame counts and crop bounds.
|
||||
|
||||
```bash
|
||||
bunx tiny-sprites ./path/to/assets
|
||||
```
|
||||
|
||||
Then open http://localhost:3000 to select spritesheets and configure animations. The tool generates the `<Sprite>` code for you.
|
||||
BIN
assets/Buildings/Black Buildings/Archery.png
Normal file
|
After Width: | Height: | Size: 9.9 KiB |
BIN
assets/Buildings/Black Buildings/Barracks.png
Normal file
|
After Width: | Height: | Size: 9.7 KiB |
BIN
assets/Buildings/Black Buildings/Castle.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
assets/Buildings/Black Buildings/House1.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
assets/Buildings/Black Buildings/House2.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/Buildings/Black Buildings/House3.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
assets/Buildings/Black Buildings/Monastery.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
assets/Buildings/Black Buildings/Tower.png
Normal file
|
After Width: | Height: | Size: 6.3 KiB |
BIN
assets/Buildings/Blue Buildings/Archery.png
Normal file
|
After Width: | Height: | Size: 9.9 KiB |
BIN
assets/Buildings/Blue Buildings/Barracks.png
Normal file
|
After Width: | Height: | Size: 9.7 KiB |
BIN
assets/Buildings/Blue Buildings/Castle.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
assets/Buildings/Blue Buildings/House1.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
assets/Buildings/Blue Buildings/House2.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/Buildings/Blue Buildings/House3.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
assets/Buildings/Blue Buildings/Monastery.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
assets/Buildings/Blue Buildings/Tower.png
Normal file
|
After Width: | Height: | Size: 6.4 KiB |
BIN
assets/Buildings/Purple Buildings/Archery.png
Normal file
|
After Width: | Height: | Size: 9.9 KiB |
BIN
assets/Buildings/Purple Buildings/Barracks.png
Normal file
|
After Width: | Height: | Size: 9.7 KiB |
BIN
assets/Buildings/Purple Buildings/Castle.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
assets/Buildings/Purple Buildings/House1.png
Normal file
|
After Width: | Height: | Size: 5.3 KiB |
BIN
assets/Buildings/Purple Buildings/House2.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/Buildings/Purple Buildings/House3.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
assets/Buildings/Purple Buildings/Monastery.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
assets/Buildings/Purple Buildings/Tower.png
Normal file
|
After Width: | Height: | Size: 6.3 KiB |
BIN
assets/Buildings/Red Buildings/Archery.png
Normal file
|
After Width: | Height: | Size: 9.8 KiB |
BIN
assets/Buildings/Red Buildings/Barracks.png
Normal file
|
After Width: | Height: | Size: 9.6 KiB |
BIN
assets/Buildings/Red Buildings/Castle.png
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
assets/Buildings/Red Buildings/House1.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
assets/Buildings/Red Buildings/House2.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/Buildings/Red Buildings/House3.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
assets/Buildings/Red Buildings/Monastery.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
assets/Buildings/Red Buildings/Tower.png
Normal file
|
After Width: | Height: | Size: 6.3 KiB |
BIN
assets/Buildings/Yellow Buildings/Archery.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
assets/Buildings/Yellow Buildings/Barracks.png
Normal file
|
After Width: | Height: | Size: 9.7 KiB |
BIN
assets/Buildings/Yellow Buildings/Castle.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
assets/Buildings/Yellow Buildings/House1.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
assets/Buildings/Yellow Buildings/House2.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/Buildings/Yellow Buildings/House3.png
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
assets/Buildings/Yellow Buildings/Monastery.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
assets/Buildings/Yellow Buildings/Tower.png
Normal file
|
After Width: | Height: | Size: 6.4 KiB |
BIN
assets/Particle FX/Dust_01.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
assets/Particle FX/Dust_02.png
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
assets/Particle FX/Explosion_01.png
Normal file
|
After Width: | Height: | Size: 5.1 KiB |
BIN
assets/Particle FX/Explosion_02.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
assets/Particle FX/Fire_01.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
assets/Particle FX/Fire_02.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
assets/Particle FX/Fire_03.png
Normal file
|
After Width: | Height: | Size: 3.4 KiB |
BIN
assets/Particle FX/Particle FX.aseprite
Normal file
BIN
assets/Particle FX/Water Splash.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
BIN
assets/Terrain/Decorations/Bushes/Bushe1.png
Normal file
|
After Width: | Height: | Size: 3.9 KiB |
BIN
assets/Terrain/Decorations/Bushes/Bushe2.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
assets/Terrain/Decorations/Bushes/Bushe3.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
BIN
assets/Terrain/Decorations/Bushes/Bushe4.png
Normal file
|
After Width: | Height: | Size: 3.8 KiB |
BIN
assets/Terrain/Decorations/Bushes/Bushes.aseprite
Normal file
BIN
assets/Terrain/Decorations/Clouds/Clouds.aseprite
Normal file
BIN
assets/Terrain/Decorations/Clouds/Clouds_01.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_02.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_03.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_04.png
Normal file
|
After Width: | Height: | Size: 1.5 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_05.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_06.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_07.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
assets/Terrain/Decorations/Clouds/Clouds_08.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
assets/Terrain/Decorations/Rocks in the Water/Water Rocks_01.png
Normal file
|
After Width: | Height: | Size: 3.4 KiB |
BIN
assets/Terrain/Decorations/Rocks in the Water/Water Rocks_02.png
Normal file
|
After Width: | Height: | Size: 4.8 KiB |
BIN
assets/Terrain/Decorations/Rocks in the Water/Water Rocks_03.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
assets/Terrain/Decorations/Rocks in the Water/Water Rocks_04.png
Normal file
|
After Width: | Height: | Size: 5.7 KiB |
BIN
assets/Terrain/Decorations/Rocks/Rock1.png
Normal file
|
After Width: | Height: | Size: 577 B |
BIN
assets/Terrain/Decorations/Rocks/Rock2.png
Normal file
|
After Width: | Height: | Size: 805 B |
BIN
assets/Terrain/Decorations/Rocks/Rock3.png
Normal file
|
After Width: | Height: | Size: 606 B |
BIN
assets/Terrain/Decorations/Rocks/Rock4.png
Normal file
|
After Width: | Height: | Size: 900 B |
BIN
assets/Terrain/Decorations/Rubber Duck/Rubber Duck.aseprite
Normal file
BIN
assets/Terrain/Decorations/Rubber Duck/Rubber duck.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Resource/Gold_Resource.png
Normal file
|
After Width: | Height: | Size: 682 B |
|
After Width: | Height: | Size: 1.9 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stone 1.png
Normal file
|
After Width: | Height: | Size: 802 B |
|
After Width: | Height: | Size: 2.1 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stone 2.png
Normal file
|
After Width: | Height: | Size: 797 B |
|
After Width: | Height: | Size: 2.1 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stone 3.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stone 4.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 3.8 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stone 5.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 6.0 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stone 6.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
|
After Width: | Height: | Size: 5.7 KiB |
BIN
assets/Terrain/Resources/Gold/Gold Stones/Gold Stones.aseprite
Normal file
BIN
assets/Terrain/Resources/Meat/Meat Resource/Meat Resource.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
assets/Terrain/Resources/Meat/Sheep/Sheep.aseprite
Normal file
BIN
assets/Terrain/Resources/Meat/Sheep/Sheep_Grass.png
Normal file
|
After Width: | Height: | Size: 4.8 KiB |
BIN
assets/Terrain/Resources/Meat/Sheep/Sheep_Idle.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
assets/Terrain/Resources/Meat/Sheep/Sheep_Move.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |