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
<script lang="ts">
const SIZE = 12
const COLORS = ['#f97316', '#facc15', '#22c55e', '#3b82f6', '#a855f7', '#ec4899', '#0a0a0a', '#ffffff', '#94a3b8']
const HEART = [
'............',
'..##....##..',
'.####..####.',
'##++########',
'#++#########',
'############',
'.##########.',
'..########..',
'...######...',
'....####....',
'.....##.....',
'............',
]
const start = HEART.join('').split('').map((c) => (c === '#' ? COLORS[0] : c === '+' ? COLORS[1] : null))
const pixels = state<(string | null)[]>(start, { history: 200 })
const color = state(COLORS[0])
const drawing = state(false)
const filled = derived(() => pixels.filter(Boolean).length)
function paint(i: number) {
const next = color === 'erase' ? null : color
if (pixels[i] === next) return
pixels.set(pixels.map((p, j) => (j === i ? next : p)))
}
init(() => {
const stop = () => drawing.set(false)
const keys = (e: KeyboardEvent) => {
if (!(e.metaKey || e.ctrlKey) || e.key.toLowerCase() !== 'z') return
e.preventDefault()
if (e.shiftKey) pixels.redo()
else pixels.undo()
}
window.addEventListener('pointerup', stop)
window.addEventListener('keydown', keys)
return () => {
window.removeEventListener('pointerup', stop)
window.removeEventListener('keydown', keys)
}
})
</script>
<div class="app">
<div class="grid" style={`grid-template-columns: repeat(${SIZE}, 1fr)`}>
{pixels.map((p, i) => (
<button
key={i}
class="px"
style={p ? `background:${p}` : ''}
aria-label={`Pixel ${i + 1}`}
onPointerdown={(e: PointerEvent) => {
e.preventDefault()
drawing.set(true)
paint(i)
}}
onPointerenter={() => drawing && paint(i)}
></button>
))}
</div>
<div class="tools">
<div class="palette">
{COLORS.map((c) => (
<button key={c} class={['swatch', color === c && 'active']} style={`background:${c}`} aria-label={c} onClick={() => color.set(c)}></button>
))}
<button class={['swatch', 'eraser', color === 'erase' && 'active']} aria-label="Eraser" onClick={() => color.set('erase')}>⌫</button>
</div>
<div class="actions">
<button disabled={!pixels.canUndo} onClick={() => pixels.undo()}>↶ Undo</button>
<button disabled={!pixels.canRedo} onClick={() => pixels.redo()}>↷ Redo</button>
<button disabled={filled === 0} onClick={() => pixels.set(Array(SIZE * SIZE).fill(null))}>Clear</button>
</div>
<p class="hint">{filled} pixels · drag to paint · ⌘/Ctrl+Z to undo</p>
</div>
</div>
<style>
.app {
display: flex;
flex-wrap: wrap;
gap: 1.25rem;
align-items: flex-start;
}
.grid {
display: grid;
width: min(100%, 288px);
aspect-ratio: 1;
gap: 1px;
padding: 1px;
background: var(--border);
border-radius: 0.75rem;
overflow: hidden;
touch-action: none;
}
.px {
min-height: 0;
padding: 0;
border: 0;
border-radius: 0;
background: var(--bg);
box-shadow: none;
transition: none;
}
.px:hover {
filter: brightness(0.9);
}
.tools {
display: grid;
gap: 0.75rem;
}
.palette {
display: grid;
grid-template-columns: repeat(5, 32px);
gap: 6px;
}
.swatch {
width: 32px;
height: 32px;
min-height: 0;
padding: 0;
border-radius: 0.5rem;
border: 1px solid color-mix(in srgb, var(--fg) 12%, transparent);
}
.swatch.active {
outline: 2px solid var(--fg);
outline-offset: 2px;
}
.eraser {
background: var(--surface);
color: var(--muted);
font-size: 14px;
}
.actions {
display: flex;
gap: 0.4rem;
}
.actions button {
font-size: 0.8rem;
}
.hint {
margin: 0;
font-size: 0.8rem;
color: var(--muted);
}
</style>