pixelsrc 0.2.0

Pixelsrc - GenAI-native pixel art format and compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Animation

Animations define sequences of sprites that play over time. Pixelsrc supports two animation formats:

- **CSS Keyframes** (recommended): Percentage-based keyframes with CSS timing functions
- **Frame Array** (legacy): Simple list of sprite names

Both formats support palette cycling, frame tags, and secondary motion (attachments).

---

## CSS Keyframes Format

The CSS keyframes format uses percentage-based keyframes, familiar to web developers and AI models. This is the recommended approach for new animations.

### Syntax

```json
{
  "type": "animation",
  "name": "string (required)",
  "keyframes": {
    "0%": { "sprite": "...", "opacity": 0.0 },
    "50%": { "sprite": "...", "opacity": 1.0 },
    "100%": { "sprite": "...", "opacity": 0.0 }
  },
  "duration": "500ms",
  "timing_function": "ease-in-out"
}
```

### Keyframe Fields

| Field | Required | Default | Description |
|-------|----------|---------|-------------|
| `type` | Yes | - | Must be `"animation"` |
| `name` | Yes | - | Unique identifier |
| `keyframes` | Yes | - | Map of percentage keys to keyframe objects |
| `duration` | No | `100` | Total animation duration (ms or CSS time string) |
| `timing_function` | No | `"linear"` | CSS timing function for easing |
| `loop` | No | `true` | Whether animation loops |

### Keyframe Object Fields

Each keyframe can specify any combination of these properties:

| Field | Description |
|-------|-------------|
| `sprite` | Sprite name to display at this keyframe |
| `opacity` | Opacity from 0.0 (transparent) to 1.0 (opaque) |
| `offset` | Position offset `[x, y]` in pixels |
| `transform` | CSS transform string (e.g., `"rotate(45deg)"`) |

### Percentage Keys

Keyframe keys are percentages of the total animation duration:

- `"0%"` - Start of animation
- `"50%"` - Halfway through
- `"100%"` - End of animation
- `"from"` - Alias for `"0%"`
- `"to"` - Alias for `"100%"`

### Duration Format

Duration accepts both raw milliseconds and CSS time strings:

```json
"duration": 500         // 500 milliseconds
"duration": "500ms"     // 500 milliseconds
"duration": "1s"        // 1000 milliseconds
"duration": "0.5s"      // 500 milliseconds
```

### Timing Functions

The `timing_function` field accepts CSS easing functions:

| Function | Description |
|----------|-------------|
| `linear` | Constant speed (default) |
| `ease` | Smooth acceleration and deceleration |
| `ease-in` | Slow start, fast end |
| `ease-out` | Fast start, slow end |
| `ease-in-out` | Slow start and end |
| `cubic-bezier(x1,y1,x2,y2)` | Custom bezier curve |
| `steps(n, position)` | Discrete steps |

### Examples

**Fade in animation:**

```json
{"type": "animation", "name": "fade_in", "keyframes": {"from": {"sprite": "dot", "opacity": 0.0}, "to": {"sprite": "dot", "opacity": 1.0}}, "duration": "1s", "timing_function": "ease"}
```

**Walk cycle with opacity:**

```json
{"type": "animation", "name": "fade_walk", "keyframes": {"0%": {"sprite": "walk_1", "opacity": 0.0}, "50%": {"sprite": "walk_2", "opacity": 1.0}, "100%": {"sprite": "walk_1", "opacity": 0.0}}, "duration": "500ms", "timing_function": "ease-in-out"}
```

**Rotating animation:**

```json
{"type": "animation", "name": "spin", "keyframes": {"0%": {"sprite": "star", "transform": "rotate(0deg)"}, "100%": {"sprite": "star", "transform": "rotate(360deg)"}}, "duration": 1000, "timing_function": "linear"}
```

**Pulsing scale effect:**

```json
{"type": "animation", "name": "pulse", "keyframes": {"0%": {"sprite": "star", "transform": "scale(1)", "opacity": 1.0}, "50%": {"sprite": "star", "transform": "scale(1.5)", "opacity": 0.5}, "100%": {"sprite": "star", "transform": "scale(1)", "opacity": 1.0}}, "duration": "2s", "timing_function": "ease-in-out"}
```

---

## Frame Array Format (Legacy)

<!-- DEMOS format/animation#basic_frames -->
<!-- /DEMOS -->

The frame array format provides a simple list of sprite names. Use this for straightforward frame-by-frame animations.

### Syntax

```json
{
  "type": "animation",
  "name": "string (required)",
  "frames": ["sprite_name", ...],
  "duration": number,
  "loop": boolean
}
```

### Fields

| Field | Required | Default | Description |
|-------|----------|---------|-------------|
| `type` | Yes | - | Must be `"animation"` |
| `name` | Yes | - | Unique identifier |
| `frames` | Yes | - | Array of sprite names in order |
| `duration` | No | 100 | Milliseconds per frame |
| `loop` | No | true | Whether animation loops |

### Example

```json
{"type": "animation", "name": "walk", "frames": ["walk_1", "walk_2", "walk_3", "walk_4"], "duration": 100, "loop": true}
```

## Frame References

Frames reference sprites by name. The sprites must be defined earlier in the file:

```json
{"type": "palette", "name": "hero", "colors": {...}}
{"type": "sprite", "name": "idle_1", "palette": "hero", "grid": [...]}
{"type": "sprite", "name": "idle_2", "palette": "hero", "grid": [...]}
{"type": "animation", "name": "idle", "frames": ["idle_1", "idle_2"], "duration": 500}
```

## Palette Cycling

Animate by rotating palette colors instead of changing sprites. This classic technique creates efficient water, fire, and energy effects.

```json
{
  "type": "animation",
  "name": "waterfall",
  "sprite": "water_static",
  "palette_cycle": {
    "tokens": ["{water1}", "{water2}", "{water3}", "{water4}"],
    "fps": 8,
    "direction": "forward"
  }
}
```

### Palette Cycle Fields

| Field | Required | Default | Description |
|-------|----------|---------|-------------|
| `sprite` | Yes* | - | Single sprite to cycle (*required if no `frames`) |
| `palette_cycle` | Yes | - | Cycle definition object or array |
| `palette_cycle.tokens` | Yes | - | Ordered list of tokens to rotate |
| `palette_cycle.fps` | No | 10 | Frames per second for cycling |
| `palette_cycle.direction` | No | `"forward"` | `"forward"` or `"reverse"` |

### Multiple Cycles

Run several palette cycles simultaneously:

```json
{
  "palette_cycle": [
    {"tokens": ["{water1}", "{water2}", "{water3}"], "fps": 8},
    {"tokens": ["{glow1}", "{glow2}"], "fps": 4}
  ]
}
```

## Frame Tags

<!-- DEMOS format/animation#frame_tags -->
<!-- /DEMOS -->

Mark frame ranges with semantic names for game engine integration:

```json
{
  "type": "animation",
  "name": "player",
  "frames": ["idle1", "idle2", "run1", "run2", "run3", "run4", "jump", "fall"],
  "fps": 10,
  "tags": {
    "idle": {"start": 0, "end": 1, "loop": true},
    "run": {"start": 2, "end": 5, "loop": true},
    "jump": {"start": 6, "end": 6, "loop": false},
    "fall": {"start": 7, "end": 7, "loop": false}
  }
}
```

### Tag Fields

| Field | Required | Default | Description |
|-------|----------|---------|-------------|
| `tags` | No | - | Map of tag name to tag definition |
| `tags.{name}.start` | Yes | - | Starting frame index (0-based) |
| `tags.{name}.end` | Yes | - | Ending frame index (inclusive) |
| `tags.{name}.loop` | No | true | Whether this segment loops |
| `tags.{name}.fps` | No | inherit | Override FPS for this tag |

Tags allow game engines to play specific sub-animations by name (e.g., "play the run tag").

## Per-Frame Metadata

Define hitboxes and metadata that vary across frames:

```json
{
  "type": "animation",
  "name": "attack",
  "frames": ["attack_1", "attack_2", "attack_3"],
  "frame_metadata": [
    {"boxes": {"hit": null}},
    {"boxes": {"hit": {"x": 20, "y": 8, "w": 20, "h": 16}}},
    {"boxes": {"hit": {"x": 24, "y": 4, "w": 24, "h": 20}}}
  ]
}
```

Frame 1 has no hitbox (`null`), while frames 2 and 3 have active hit regions.

## Secondary Motion (Attachments)

Animate attached elements like hair, capes, or tails that follow the parent animation with configurable delay:

```json
{
  "type": "animation",
  "name": "hero_walk",
  "frames": ["walk_1", "walk_2", "walk_3", "walk_4"],
  "duration": 100,
  "attachments": [
    {
      "name": "hair",
      "anchor": [12, 4],
      "chain": ["hair_1", "hair_2", "hair_3"],
      "delay": 1,
      "follow": "position"
    },
    {
      "name": "cape",
      "anchor": [8, 8],
      "chain": ["cape_top", "cape_mid", "cape_bottom"],
      "delay": 2,
      "follow": "velocity",
      "z_index": -1
    }
  ]
}
```

### Attachment Fields

| Field | Required | Default | Description |
|-------|----------|---------|-------------|
| `attachments` | No | - | Array of attachment definitions |
| `attachments[].name` | Yes | - | Identifier for this attachment |
| `attachments[].anchor` | Yes | - | Attachment point `[x, y]` on parent sprite |
| `attachments[].chain` | Yes | - | Array of sprite names forming the chain |
| `attachments[].delay` | No | 1 | Frame delay between chain segments |
| `attachments[].follow` | No | `"position"` | `"position"`, `"velocity"`, or `"rotation"` |
| `attachments[].damping` | No | 0.8 | Oscillation damping (0.0-1.0) |
| `attachments[].stiffness` | No | 0.5 | Spring stiffness (0.0-1.0) |
| `attachments[].z_index` | No | 0 | Render order (negative = behind parent) |

### Follow Modes

| Mode | Behavior |
|------|----------|
| `position` | Chain follows parent position directly |
| `velocity` | Chain responds to movement velocity |
| `rotation` | Chain responds to rotation changes |

## Duration vs FPS

<!-- DEMOS format/animation#timing -->
<!-- /DEMOS -->

You can specify timing using either `duration` (ms per frame) or `fps` (frames per second):

```json
{"type": "animation", "name": "fast", "frames": [...], "duration": 50}
{"type": "animation", "name": "fast", "frames": [...], "fps": 20}
```

Both examples create the same 20 FPS animation.

## Complete Examples

### CSS Keyframes Example (Recommended)

A blinking light with fade effect:

```json
{"type": "palette", "name": "blink", "colors": {"{_}": "#0000", "{on}": "#FF0", "{off}": "#880"}}

{"type": "sprite", "name": "light_on", "palette": "blink", "grid": [
  "{_}{on}{on}{_}",
  "{on}{on}{on}{on}",
  "{on}{on}{on}{on}",
  "{_}{on}{on}{_}"
]}

{"type": "sprite", "name": "light_off", "palette": "blink", "grid": [
  "{_}{off}{off}{_}",
  "{off}{off}{off}{off}",
  "{off}{off}{off}{off}",
  "{_}{off}{off}{_}"
]}

{"type": "animation", "name": "blink_fade", "keyframes": {"0%": {"sprite": "light_on", "opacity": 1.0}, "50%": {"sprite": "light_off", "opacity": 0.5}, "100%": {"sprite": "light_on", "opacity": 1.0}}, "duration": "1s", "timing_function": "ease-in-out"}
```

### Frame Array Example (Legacy)

A simple blinking light:

```json
{"type": "palette", "name": "blink", "colors": {"{_}": "#0000", "{on}": "#FF0", "{off}": "#880"}}

{"type": "sprite", "name": "light_on", "palette": "blink", "grid": [
  "{_}{on}{on}{_}",
  "{on}{on}{on}{on}",
  "{on}{on}{on}{on}",
  "{_}{on}{on}{_}"
]}

{"type": "sprite", "name": "light_off", "palette": "blink", "grid": [
  "{_}{off}{off}{_}",
  "{off}{off}{off}{off}",
  "{off}{off}{off}{off}",
  "{_}{off}{off}{_}"
]}

{"type": "animation", "name": "blink", "frames": ["light_on", "light_off"], "duration": 500, "loop": true}
```

### Try It

Click to render the first frame of the blinking animation:

<div class="pixelsrc-demo" data-pixelsrc-demo>
  <textarea id="animation-demo">{"type": "palette", "name": "blink", "colors": {"{_}": "#0000", "{on}": "#FFFF00", "{off}": "#888800"}}
{"type": "sprite", "name": "light_on", "palette": "blink", "grid": ["{_}{on}{on}{_}", "{on}{on}{on}{on}", "{on}{on}{on}{on}", "{_}{on}{on}{_}"]}
{"type": "sprite", "name": "light_off", "palette": "blink", "grid": ["{_}{off}{off}{_}", "{off}{off}{off}{off}", "{off}{off}{off}{off}", "{_}{off}{off}{_}"]}
{"type": "animation", "name": "blink", "frames": ["light_on", "light_off"], "duration": 500, "loop": true}</textarea>
  <button onclick="pixelsrcDemo.renderFromTextarea('animation-demo', 'animation-demo-preview', {spriteName: 'light_on'})">Show "On" Frame</button>
  <button onclick="pixelsrcDemo.renderFromTextarea('animation-demo', 'animation-demo-preview', {spriteName: 'light_off'})">Show "Off" Frame</button>
  <div class="preview" id="animation-demo-preview"></div>
</div>

Try changing the `{on}` color to `#00FF00` (green) or `#FF0000` (red).

## Rendering Animations

```bash
# Render as animated GIF
pxl render animation.pxl -o output.gif

# Render as spritesheet
pxl render animation.pxl --format spritesheet -o sheet.png

# Preview with onion skinning
pxl show animation.pxl --onion 2
```

---

## Migrating from Frames to Keyframes

Converting from the legacy frame array format to CSS keyframes is straightforward:

### Basic Migration

**Before (frames format):**

```json
{
  "type": "animation",
  "name": "walk",
  "frames": ["walk_1", "walk_2", "walk_3", "walk_4"],
  "duration": 100,
  "loop": true
}
```

**After (keyframes format):**

```json
{
  "type": "animation",
  "name": "walk",
  "keyframes": {
    "0%": {"sprite": "walk_1"},
    "25%": {"sprite": "walk_2"},
    "50%": {"sprite": "walk_3"},
    "75%": {"sprite": "walk_4"}
  },
  "duration": "400ms",
  "loop": true
}
```

### Key Differences

| Aspect | Frames Format | Keyframes Format |
|--------|--------------|------------------|
| Timing | `duration` is per-frame | `duration` is total animation time |
| Structure | Flat sprite array | Percentage-keyed objects |
| Properties | Sprite only | Sprite, opacity, offset, transform |
| Easing | N/A | `timing_function` for interpolation |

### Migration Steps

1. **Calculate total duration**: Multiply per-frame duration by frame count
   - 4 frames × 100ms = 400ms total

2. **Convert to percentages**: Divide frame index by total frames
   - Frame 0 → 0%
   - Frame 1 → 25% (1/4)
   - Frame 2 → 50% (2/4)
   - Frame 3 → 75% (3/4)

3. **Wrap sprites in keyframe objects**: `"walk_1"` becomes `{"sprite": "walk_1"}`

4. **Add timing function** (optional): Use `"timing_function": "linear"` for frame-accurate timing

### When to Migrate

Migrate to keyframes when you need:

- **Opacity changes**: Fade effects between frames
- **Position offsets**: Screen shake, bouncing
- **Transforms**: Rotation, scaling effects
- **CSS timing**: Easing curves for smoother motion

Keep using frames format for:

- Simple frame-by-frame animations with no interpolation
- Quick prototypes
- Backwards compatibility with existing files