- README: API docs, usage example, dev tool instructions - CLAUDE.md: architecture notes on Hono JSX, Pico CSS, implementation details - Image picker now uses Pico's <details> dropdown pattern 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
4.4 KiB
tiny-sprites
See 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/jsxfor the Sprite component (renders to HTML strings) - Client-side:
hono/jsx/domfor the dev tool UI (renders to DOM) - No hooks: The Sprite component is pure server-side JSX with zero client JavaScript. No
useState, nouseEffect. The dev tool uses vanilla JS with a simple mutablestateobject and manual DOM updates viarender()calls.
Dev Tool Styling
The dev tool uses Pico CSS 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:
@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 ofnode <file>orts-node <file> - Use
bun testinstead ofjestorvitest - Use
bun build <file.html|file.ts|file.css>instead ofwebpackoresbuild - Use
bun installinstead ofnpm installoryarn installorpnpm install - Use
bun run <script>instead ofnpm run <script>oryarn run <script>orpnpm run <script> - Use
bunx <package> <command>instead ofnpx <package> <command> - Bun automatically loads .env, so don't use dotenv.
APIs
Bun.serve()supports WebSockets, HTTPS, and routes. Don't useexpress.bun:sqlitefor SQLite. Don't usebetter-sqlite3.Bun.redisfor Redis. Don't useioredis.Bun.sqlfor Postgres. Don't usepgorpostgres.js.WebSocketis built-in. Don't usews.- Prefer
Bun.fileovernode:fs's readFile/writeFile - Bun.$
lsinstead of execa.
Testing
Use bun test to run tests.
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:
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>
<body>
<h1>Hello, world!</h1>
<script type="module" src="./frontend.tsx"></script>
</body>
</html>
With the following 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
bun --hot ./index.ts
For more information, read the Bun API docs in node_modules/bun-types/docs/**.mdx.