Repository navigation
Expand file tree
/
Copy pathpixel.html
More file actions
565 lines (492 loc) · 32.8 KB
/
Copy pathpixel.html
File metadata and controls
565 lines (492 loc) · 32.8 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixel Animator</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- React & ReactDOM -->
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<!-- Babel for in-browser JSX compilation -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- LZ-String for URL Compression -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.5.0/lz-string.min.js"></script>
<style>
/* Custom scrollbar for timeline */
.scrollbar-thin::-webkit-scrollbar { height: 6px; }
.scrollbar-thin::-webkit-scrollbar-track { background: transparent; }
.scrollbar-thin::-webkit-scrollbar-thumb { background-color: #cbd5e1; border-radius: 20px; }
.dark .scrollbar-thin::-webkit-scrollbar-thumb { background-color: #475569; }
/* Toast Animation */
@keyframes slideUp {
from { transform: translate(-50%, 100%); opacity: 0; }
to { transform: translate(-50%, 0); opacity: 1; }
}
.toast-animate {
animation: slideUp 0.3s ease-out forwards;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const { useState, useEffect, useRef } = React;
const GRID_SIZE = 16;
const FPS = 12;
const createEmptyFrame = () => Array(GRID_SIZE * GRID_SIZE).fill('transparent');
const hexAlphaToRgba = (hex, alpha) => {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (!result) return 'rgba(0,0,0,1)';
const r = parseInt(result[1], 16);
const g = parseInt(result[2], 16);
const b = parseInt(result[3], 16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};
const Icons = {
Pen: () => <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 19l7-7 3 3-7 7-3-3z"></path><path d="M18 13l-1.5-7.5L2 2l3.5 14.5L13 18l5-5z"></path><path d="M2 2l7.586 7.586"></path><circle cx="11" cy="11" r="2"></circle></svg>,
Eraser: () => <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m7 21-4.3-4.3c-1-1-1-2.5 0-3.4l9.6-9.6c1-1 2.5-1 3.4 0l5.6 5.6c1 1 1 2.5 0 3.4L13 21"></path><path d="M22 21H7"></path><path d="m5 11 9 9"></path></svg>,
Fill: () => <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m19 11-8-8-8.6 8.6a2 2 0 0 0 0 2.8l5.2 5.2c.8.8 2 .8 2.8 0L19 11Z"></path><path d="m5 2 5 5"></path><path d="M2 13h15"></path><path d="M22 20a2 2 0 1 1-4 0c0-1.6 1.7-2.4 2-4 .3 1.6 2 2.4 2 4Z"></path></svg>,
Play: () => <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>,
Pause: () => <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>,
Plus: () => <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>,
Copy: () => <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>,
Trash: () => <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>,
ArrowLeft: () => <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>,
ArrowRight: () => <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>,
Download: () => <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>,
Upload: () => <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>,
Video: () => <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polygon points="23 7 16 12 23 17 23 7"></polygon><rect x="1" y="5" width="15" height="14" rx="2" ry="2"></rect></svg>,
Share: () => <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="18" cy="5" r="3"></circle><circle cx="6" cy="12" r="3"></circle><circle cx="18" cy="19" r="3"></circle><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"></line><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"></line></svg>,
};
function App() {
const fileInputRef = useRef(null);
const [frames, setFrames] = useState([createEmptyFrame()]);
const [activeFrameIdx, setActiveFrameIdx] = useState(0);
const [isPlaying, setIsPlaying] = useState(false);
const [tool, setTool] = useState('pen');
const [isDrawing, setIsDrawing] = useState(false);
const [toastMsg, setToastMsg] = useState('');
const [colorHex, setColorHex] = useState('#000000');
const [colorAlpha, setColorAlpha] = useState(1);
const [palette, setPalette] = useState([
'rgba(0, 0, 0, 1)', 'rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)',
'rgba(0, 0, 255, 1)', 'rgba(255, 255, 0, 1)', 'rgba(255, 255, 255, 1)'
]);
const currentColor = tool === 'eraser' ? 'transparent' : hexAlphaToRgba(colorHex, colorAlpha);
// Toast Message System
const showToast = (msg) => {
setToastMsg(msg);
setTimeout(() => setToastMsg(''), 3000);
};
// Initialize from URL Share Link
useEffect(() => {
const hash = window.location.hash.replace('#data=', '');
if (hash) {
try {
const decompressed = LZString.decompressFromEncodedURIComponent(hash);
const data = JSON.parse(decompressed);
if (data && data.frames) {
setFrames(data.frames);
if (data.palette) setPalette(data.palette);
showToast("Loaded shared project!");
}
} catch (e) {
console.error("Failed to parse URL data");
showToast("Failed to load shared project");
}
}
}, []);
// Playback Loop
useEffect(() => {
let intervalId;
if (isPlaying) {
intervalId = setInterval(() => {
setActiveFrameIdx((prev) => (prev + 1) % frames.length);
}, 1000 / FPS);
}
return () => clearInterval(intervalId);
}, [isPlaying, frames.length]);
// --- SAVE, LOAD, EXPORT, SHARE FUNCTIONS ---
const handleSaveFile = () => {
const projectData = { frames, palette };
const blob = new Blob([JSON.stringify(projectData)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'pixel-animation.json';
a.click();
URL.revokeObjectURL(url);
showToast("Project saved!");
};
const handleLoadFile = (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
try {
const data = JSON.parse(event.target.result);
if (data.frames) setFrames(data.frames);
if (data.palette) setPalette(data.palette);
setActiveFrameIdx(0);
setIsPlaying(false);
showToast("Project loaded!");
} catch (err) {
showToast("Error reading file");
}
};
reader.readAsText(file);
e.target.value = ''; // Reset input
};
const handleShare = () => {
const projectData = { frames, palette };
const jsonStr = JSON.stringify(projectData);
const compressed = LZString.compressToEncodedURIComponent(jsonStr);
const url = window.location.origin + window.location.pathname + '#data=' + compressed;
// Fallback copy mechanism
const copyToClipboard = (text) => {
const textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
showToast("Shareable link copied to clipboard!");
} catch (err) {
showToast("Failed to copy link.");
}
document.body.removeChild(textArea);
};
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(url)
.then(() => showToast("Shareable link copied to clipboard!"))
.catch(() => copyToClipboard(url));
} else {
copyToClipboard(url);
}
};
const handleExportVideo = () => {
showToast("Recording video...");
const canvas = document.createElement('canvas');
const size = 512;
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext('2d');
canvas.style.display = 'none';
document.body.appendChild(canvas);
const stream = canvas.captureStream(FPS);
const recorder = new MediaRecorder(stream, { mimeType: 'video/webm' });
const chunks = [];
recorder.ondataavailable = e => { if(e.data.size > 0) chunks.push(e.data) };
recorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'pixel-animation.webm';
a.click();
URL.revokeObjectURL(url);
document.body.removeChild(canvas);
showToast("Export complete!");
};
recorder.start();
let frameIndex = 0;
const loops = 3; // Record 3 loops so video isn't overly short
const totalFrames = frames.length * loops;
const drawNextFrame = () => {
if (frameIndex >= totalFrames) {
recorder.stop();
return;
}
const frame = frames[frameIndex % frames.length];
// Fill background
ctx.fillStyle = '#ffffff'; // White bg for transparent areas in video
ctx.fillRect(0, 0, size, size);
const cellSize = size / GRID_SIZE;
for(let j=0; j<frame.length; j++) {
const color = frame[j];
if(color !== 'transparent') {
ctx.fillStyle = color;
const x = (j % GRID_SIZE) * cellSize;
const y = Math.floor(j / GRID_SIZE) * cellSize;
ctx.fillRect(x, y, cellSize, cellSize);
}
}
frameIndex++;
setTimeout(drawNextFrame, 1000 / FPS);
};
drawNextFrame();
};
// --- DRAWING LOGIC ---
const floodFill = (grid, startIndex, replacementColor) => {
const targetColor = grid[startIndex];
if (targetColor === replacementColor) return grid;
const newGrid = [...grid];
const queue = [startIndex];
while (queue.length > 0) {
const idx = queue.shift();
if (newGrid[idx] === targetColor) {
newGrid[idx] = replacementColor;
const x = idx % GRID_SIZE;
const y = Math.floor(idx / GRID_SIZE);
if (x > 0) queue.push(idx - 1); // left
if (x < GRID_SIZE - 1) queue.push(idx + 1); // right
if (y > 0) queue.push(idx - GRID_SIZE); // up
if (y < GRID_SIZE - 1) queue.push(idx + GRID_SIZE); // down
}
}
return newGrid;
};
const handlePointerDown = (index) => {
if (isPlaying) return;
setIsDrawing(true);
applyTool(index);
};
const handlePointerEnter = (index) => {
if (isDrawing && tool !== 'fill') applyTool(index);
};
const handlePointerUp = () => setIsDrawing(false);
const applyTool = (index) => {
setFrames(prevFrames => {
const newFrames = [...prevFrames];
const currentGrid = [...newFrames[activeFrameIdx]];
if (tool === 'pen' || tool === 'eraser') {
currentGrid[index] = currentColor;
} else if (tool === 'fill') {
newFrames[activeFrameIdx] = floodFill(currentGrid, index, currentColor);
return newFrames;
}
newFrames[activeFrameIdx] = currentGrid;
return newFrames;
});
};
// --- FRAME MANAGEMENT ---
const addFrame = () => {
setFrames([...frames, createEmptyFrame()]);
setActiveFrameIdx(frames.length);
};
const duplicateFrame = (e, index) => {
e.stopPropagation();
const newFrames = [...frames];
newFrames.splice(index + 1, 0, [...frames[index]]);
setFrames(newFrames);
setActiveFrameIdx(index + 1);
};
const deleteFrame = (e, index) => {
e.stopPropagation();
if (frames.length === 1) {
setFrames([createEmptyFrame()]);
return;
}
const newFrames = frames.filter((_, i) => i !== index);
setFrames(newFrames);
if (activeFrameIdx >= newFrames.length) setActiveFrameIdx(newFrames.length - 1);
else if (activeFrameIdx > index) setActiveFrameIdx(activeFrameIdx - 1);
};
const moveFrame = (e, index, direction) => {
e.stopPropagation();
if ((direction === -1 && index === 0) || (direction === 1 && index === frames.length - 1)) return;
const newFrames = [...frames];
const temp = newFrames[index];
newFrames[index] = newFrames[index + direction];
newFrames[index + direction] = temp;
setFrames(newFrames);
if (activeFrameIdx === index) setActiveFrameIdx(index + direction);
else if (activeFrameIdx === index + direction) setActiveFrameIdx(index);
};
// --- PALETTE MANAGEMENT ---
const saveColorToPalette = () => {
const rgba = hexAlphaToRgba(colorHex, colorAlpha);
if (!palette.includes(rgba)) setPalette([rgba, ...palette].slice(0, 24));
};
const selectColorFromPalette = (rgba) => {
if (rgba === 'transparent') {
setTool('eraser');
return;
}
setTool('pen');
const rgbaMatch = rgba.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
if (rgbaMatch) {
const r = parseInt(rgbaMatch[1]).toString(16).padStart(2, '0');
const g = parseInt(rgbaMatch[2]).toString(16).padStart(2, '0');
const b = parseInt(rgbaMatch[3]).toString(16).padStart(2, '0');
setColorHex(`#${r}${g}${b}`);
setColorAlpha(rgbaMatch[4] !== undefined ? parseFloat(rgbaMatch[4]) : 1);
}
};
const FrameThumbnail = ({ grid, isActive, index }) => (
<div
className={`relative flex-shrink-0 cursor-pointer border-2 transition-all rounded-md overflow-hidden bg-gray-100 dark:bg-gray-800 ${
isActive ? 'border-blue-500 scale-105 shadow-md' : 'border-gray-300 dark:border-gray-700 hover:border-gray-400'
}`}
style={{
width: '80px', height: '80px',
backgroundImage: 'conic-gradient(#ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc)',
backgroundSize: '10px 10px',
backgroundPosition: '0 0, 5px 5px'
}}
onClick={() => { setIsPlaying(false); setActiveFrameIdx(index); }}
>
<div
className="w-full h-full"
style={{
display: 'grid', gridTemplateColumns: `repeat(${GRID_SIZE}, 1fr)`, gridTemplateRows: `repeat(${GRID_SIZE}, 1fr)`
}}
>
{grid.map((color, i) => <div key={i} style={{ backgroundColor: color === 'transparent' ? 'transparent' : color }} />)}
</div>
{isActive && !isPlaying && (
<div className="absolute inset-0 bg-black/40 opacity-0 hover:opacity-100 flex flex-col justify-between p-1 transition-opacity">
<div className="flex justify-between">
<button onClick={(e) => deleteFrame(e, index)} className="p-1 bg-red-500 text-white rounded hover:bg-red-600 shadow-sm"><Icons.Trash /></button>
<button onClick={(e) => duplicateFrame(e, index)} className="p-1 bg-blue-500 text-white rounded hover:bg-blue-600 shadow-sm"><Icons.Copy /></button>
</div>
<div className="flex justify-between">
<button onClick={(e) => moveFrame(e, index, -1)} className={`p-1 bg-gray-700 text-white rounded hover:bg-gray-600 shadow-sm ${index === 0 ? 'invisible' : ''}`}><Icons.ArrowLeft /></button>
<div className="text-white text-xs font-bold drop-shadow-md">{index + 1}</div>
<button onClick={(e) => moveFrame(e, index, 1)} className={`p-1 bg-gray-700 text-white rounded hover:bg-gray-600 shadow-sm ${index === frames.length - 1 ? 'invisible' : ''}`}><Icons.ArrowRight /></button>
</div>
</div>
)}
{!isActive && (
<div className="absolute bottom-1 right-1 bg-black/60 text-white text-[10px] px-1 rounded">{index + 1}</div>
)}
</div>
);
return (
<div className="min-h-screen bg-neutral-100 text-neutral-800 dark:bg-neutral-900 dark:text-neutral-100 font-sans flex flex-col selection:bg-blue-200">
{/* Toast Notification */}
{toastMsg && (
<div className="fixed bottom-6 left-1/2 -translate-x-1/2 bg-neutral-800 text-white px-6 py-3 rounded-full shadow-2xl z-50 flex items-center gap-3 toast-animate border border-neutral-700">
<span className="w-2 h-2 rounded-full bg-blue-400 animate-pulse"></span>
<span className="text-sm font-medium">{toastMsg}</span>
</div>
)}
{/* Hidden file input for loading projects */}
<input type="file" accept=".json" ref={fileInputRef} onChange={handleLoadFile} className="hidden" />
<header className="bg-white dark:bg-neutral-800 shadow-sm p-4 flex flex-col sm:flex-row gap-4 items-center justify-between border-b dark:border-neutral-700">
<div className="flex w-full sm:w-auto items-center justify-between">
<div>
<h1 className="text-xl font-bold flex items-center gap-2">
<span className="text-blue-500 text-2xl">▣</span> Pixel Animator
</h1>
</div>
</div>
<div className="flex items-center gap-2 w-full sm:w-auto overflow-x-auto pb-2 sm:pb-0 hide-scrollbar">
{/* File & Export Actions */}
<div className="flex items-center bg-neutral-100 dark:bg-neutral-700 rounded-lg p-1 mr-2 border border-neutral-200 dark:border-neutral-600">
<button onClick={() => fileInputRef.current.click()} title="Open Project" className="p-2 hover:bg-white dark:hover:bg-neutral-600 rounded-md text-neutral-600 dark:text-neutral-300 transition-colors"><Icons.Upload /></button>
<button onClick={handleSaveFile} title="Save Project" className="p-2 hover:bg-white dark:hover:bg-neutral-600 rounded-md text-neutral-600 dark:text-neutral-300 transition-colors"><Icons.Download /></button>
<div className="w-px h-6 bg-neutral-300 dark:bg-neutral-500 mx-1"></div>
<button onClick={handleExportVideo} title="Export Video" className="p-2 hover:bg-white dark:hover:bg-neutral-600 rounded-md text-blue-600 dark:text-blue-400 transition-colors"><Icons.Video /></button>
<button onClick={handleShare} title="Copy Share Link" className="p-2 hover:bg-white dark:hover:bg-neutral-600 rounded-md text-green-600 dark:text-green-400 transition-colors flex items-center gap-1"><Icons.Share /></button>
</div>
{/* Playback Actions */}
<span className="text-sm font-medium bg-neutral-200 dark:bg-neutral-700 px-3 py-2 rounded-lg text-neutral-600 dark:text-neutral-300 whitespace-nowrap">
{FPS} FPS
</span>
<button
onClick={() => setIsPlaying(!isPlaying)}
className={`flex items-center gap-2 px-5 py-2 rounded-lg font-bold transition-all shadow-sm whitespace-nowrap ${
isPlaying ? 'bg-red-100 text-red-600 hover:bg-red-200 dark:bg-red-900/30 dark:text-red-400' : 'bg-blue-500 text-white hover:bg-blue-600'
}`}
>
{isPlaying ? <><Icons.Pause /> Pause</> : <><Icons.Play /> Play</>}
</button>
</div>
</header>
<main className="flex-1 flex flex-col md:flex-row overflow-hidden">
<aside className="w-full md:w-64 bg-white dark:bg-neutral-800 border-r dark:border-neutral-700 p-4 flex flex-col gap-6 overflow-y-auto">
<section>
<h2 className="text-xs uppercase font-bold text-neutral-400 mb-3 tracking-wider">Tools</h2>
<div className="flex gap-2">
{[{ id: 'pen', icon: Icons.Pen, label: 'Pen' }, { id: 'fill', icon: Icons.Fill, label: 'Fill' }, { id: 'eraser', icon: Icons.Eraser, label: 'Eraser' }].map(t => (
<button
key={t.id} onClick={() => setTool(t.id)} title={t.label}
className={`flex-1 p-3 flex justify-center items-center rounded-xl transition-all ${
tool === t.id ? 'bg-blue-100 text-blue-600 shadow-inner border border-blue-200 dark:bg-blue-900/40 dark:text-blue-400 dark:border-blue-800' : 'bg-neutral-50 text-neutral-600 hover:bg-neutral-200 border border-transparent dark:bg-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-600'
}`}
><t.icon /></button>
))}
</div>
</section>
<section>
<h2 className="text-xs uppercase font-bold text-neutral-400 mb-3 tracking-wider">Color & Transparency</h2>
<div className="flex flex-col gap-3">
<div className="flex items-center gap-3">
<div
className="w-12 h-12 rounded-lg border-2 border-neutral-300 shadow-sm overflow-hidden flex-shrink-0"
style={{ backgroundImage: 'conic-gradient(#eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee)', backgroundSize: '8px 8px' }}
><div className="w-full h-full" style={{ backgroundColor: currentColor }}></div></div>
<div className="flex-1 relative">
<input type="color" value={colorHex} onChange={(e) => { setColorHex(e.target.value); setTool('pen'); }} className="w-full h-10 cursor-pointer opacity-0 absolute inset-0 z-10" />
<div className="w-full h-10 rounded-lg bg-neutral-100 dark:bg-neutral-700 border border-neutral-200 dark:border-neutral-600 flex items-center px-3 font-mono text-sm">{colorHex}</div>
</div>
</div>
<div className="flex flex-col gap-1">
<div className="flex justify-between text-xs text-neutral-500"><span>Opacity (Alpha)</span><span>{Math.round(colorAlpha * 100)}%</span></div>
<input type="range" min="0" max="1" step="0.05" value={colorAlpha} onChange={(e) => { setColorAlpha(parseFloat(e.target.value)); setTool('pen'); }} className="w-full accent-blue-500 cursor-pointer" />
</div>
<button onClick={saveColorToPalette} className="mt-2 w-full py-2 bg-neutral-100 hover:bg-neutral-200 dark:bg-neutral-700 dark:hover:bg-neutral-600 text-sm font-medium rounded-lg transition-colors border border-neutral-200 dark:border-neutral-600">Save Color to Palette</button>
</div>
</section>
<section className="flex-1">
<h2 className="text-xs uppercase font-bold text-neutral-400 mb-3 tracking-wider">Saved Palette</h2>
<div className="grid grid-cols-4 sm:grid-cols-6 md:grid-cols-4 gap-2">
<button
onClick={() => setTool('eraser')} title="Transparent / Eraser"
className={`w-full aspect-square rounded-md border-2 overflow-hidden relative ${tool === 'eraser' ? 'border-blue-500 scale-110 shadow-md' : 'border-neutral-200 dark:border-neutral-700 hover:scale-105'}`}
style={{ backgroundImage: 'conic-gradient(#ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc)', backgroundSize: '8px 8px', backgroundPosition: '0 0, 4px 4px' }}
/>
{palette.map((colorStr, i) => (
<button
key={i} onClick={() => selectColorFromPalette(colorStr)} title={colorStr}
className={`w-full aspect-square rounded-md border-2 overflow-hidden transition-transform ${currentColor === colorStr && tool !== 'eraser' ? 'border-blue-500 scale-110 shadow-md' : 'border-neutral-200 dark:border-neutral-700 hover:scale-105'}`}
style={{ backgroundImage: 'conic-gradient(#eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee)', backgroundSize: '8px 8px' }}
><div className="w-full h-full" style={{ backgroundColor: colorStr }} /></button>
))}
</div>
</section>
</aside>
<section
className="flex-1 flex items-center justify-center bg-neutral-200 dark:bg-neutral-900 p-4 sm:p-8 overflow-hidden touch-none relative"
onPointerUp={handlePointerUp} onPointerLeave={handlePointerUp}
>
{isPlaying && <div className="absolute top-4 right-4 bg-black/60 text-white px-4 py-2 rounded-full font-bold animate-pulse backdrop-blur-sm z-10">Playing Frame {activeFrameIdx + 1}</div>}
<div
className="shadow-2xl ring-4 ring-white/50 dark:ring-black/50 bg-white"
style={{ width: '100%', maxWidth: '600px', aspectRatio: '1/1', backgroundImage: 'conic-gradient(#e5e5e5 25%, transparent 25%, transparent 75%, #e5e5e5 75%, #e5e5e5)', backgroundSize: '20px 20px', backgroundPosition: '0 0, 10px 10px', touchAction: 'none' }}
>
<div className="w-full h-full border border-neutral-300 dark:border-neutral-600" style={{ display: 'grid', gridTemplateColumns: `repeat(${GRID_SIZE}, 1fr)`, gridTemplateRows: `repeat(${GRID_SIZE}, 1fr)` }}>
{frames[activeFrameIdx].map((color, index) => (
<div
key={index} onPointerDown={() => handlePointerDown(index)} onPointerEnter={() => handlePointerEnter(index)}
className="border-[0.5px] border-neutral-300/30 dark:border-neutral-600/30 cursor-crosshair select-none"
style={{ backgroundColor: color === 'transparent' ? 'transparent' : color }} draggable="false"
/>
))}
</div>
</div>
</section>
</main>
<footer className="h-36 bg-white dark:bg-neutral-800 border-t dark:border-neutral-700 p-4 flex flex-col gap-2 shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.05)]">
<div className="flex justify-between items-center text-xs font-bold text-neutral-500 uppercase tracking-wider">
<span>Frames ({frames.length})</span>
<span className="text-neutral-400 font-normal normal-case hidden sm:inline">Drag frame arrows to reorder • Dupe/Trash on active frame</span>
</div>
<div className="flex-1 flex items-center gap-4 overflow-x-auto pb-2 scrollbar-thin scrollbar-thumb-neutral-300 dark:scrollbar-thumb-neutral-600">
{frames.map((frame, index) => <FrameThumbnail key={`frame-${index}`} grid={frame} index={index} isActive={activeFrameIdx === index} />)}
<button onClick={addFrame} className="flex-shrink-0 w-20 h-20 flex flex-col items-center justify-center gap-2 border-2 border-dashed border-neutral-300 dark:border-neutral-600 rounded-md text-neutral-500 hover:text-blue-500 hover:border-blue-500 hover:bg-blue-50 dark:hover:bg-blue-900/20 transition-colors">
<Icons.Plus /><span className="text-xs font-bold">New</span>
</button>
</div>
</footer>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
</script>
</body>
</html>