Skip to content

Commit e5c7944

Browse files
committed
updated pixel art editor
1 parent a0c611b commit e5c7944

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

public/sw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const cacheName = "luizbills.litecanvas-editor-v1";
2-
const version = "2.41.2";
2+
const version = "2.42.0";
33

44
const precacheResources = [
55
"/",

public/tools/pixel-art-editor.html

+69-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- Inspired by https://github.com/xem/miniPixelArt -->
2-
<!DOCTYPE html>
2+
<!doctype html>
33
<html lang="en">
44
<head>
55
<meta charset="UTF-8" />
@@ -207,10 +207,11 @@ <h1>litepixel</h1>
207207
>
208208
<button id="clearButton">clear</button>
209209
<button id="exportButton">export</button>
210+
<button id="importButton">import</button>
210211
<button id="downloadButton">download</button>
211212
<textarea
212213
id="exported"
213-
rows="10"
214+
rows="20"
214215
readonly
215216
onclick="this.select()"
216217
></textarea>
@@ -436,7 +437,10 @@ <h1>litepixel</h1>
436437
return -1;
437438
}
438439
const color = pixels[x][y] == null ? -1 : Number(pixels[x][y]);
439-
return color >= 0 ? color : -1;
440+
if (Number.isNaN(color) || !Number.isFinite(color) || color < 0) {
441+
return -1;
442+
}
443+
return color;
440444
}
441445

442446
/**
@@ -485,7 +489,7 @@ <h1>litepixel</h1>
485489
clearButton.onclick = () => {
486490
if (
487491
confirm(
488-
"Are you sure you want to erase the canvas? This action cannot be undone."
492+
"Are you sure you want to erase the canvas? This action cannot be undone.",
489493
)
490494
) {
491495
pixels.length = 0;
@@ -498,25 +502,82 @@ <h1>litepixel</h1>
498502
// Export
499503
exportButton.onclick = function () {
500504
let output = "";
505+
const _pixels = [];
501506

502-
output += `paint(${W}, ${H}, [\n`;
507+
output += `const renameThisVar = paint(${W}, ${H}, [\n`;
503508

504509
for (let y = 0; y < H; y++) {
505510
output += " '";
506511
for (let x = 0; x < W; x++) {
507512
let color = getPixel(x, y);
508513
if (-1 === ~~color) color = ".";
509514
output += color >= 0 ? (color % colors.length).toString(16) : color;
515+
_pixels.push(color === "." ? "#" : color);
510516
}
511517
output += "',\n";
512518
}
513519

514520
output += "], {\n scale: 1\n})";
515521

522+
const importCode = `Art Code: ${W}x${H}/${_pixels.join(" ")}`;
523+
output = `// ${importCode}\n` + output;
524+
516525
exported.value = output;
526+
517527
copy.disabled = false;
518528
};
519529

530+
// Import
531+
importButton.onclick = (ev) => {
532+
const importCode = prompt("Paste the Art Code here:");
533+
let [_size, _pixels] = importCode.replace("//", "").split("/");
534+
535+
// validate the dimensions
536+
_size = _size
537+
.replace(/[^0-9]/g, " ")
538+
.trim()
539+
.split(" ");
540+
541+
if (_size.length !== 2) {
542+
alert("Invalid size: " + _size.join(" x "));
543+
console.error("Invalid art size: " + _size);
544+
return;
545+
} else {
546+
_size[0] = _size[0] < 1 ? 8 : ~~_size[0];
547+
_size[1] = _size[1] < 1 ? 8 : ~~_size[1];
548+
}
549+
550+
// validate the pixels
551+
_pixels = _pixels.trim().replace(/\#/g, ".").split(" ");
552+
553+
if (_pixels.length !== _size[0] * _size[1]) {
554+
alert("Invalid amount of pixels");
555+
return;
556+
}
557+
558+
console.log(
559+
"importing... size:",
560+
_size.join("x"),
561+
"colors:",
562+
_pixels.join(" "),
563+
);
564+
565+
// reset the canvas
566+
width.value = _size[0];
567+
height.value = _size[1];
568+
pixels.length = 0;
569+
prepare();
570+
571+
// set the pixels
572+
for (let y = 0; y < H; y++) {
573+
for (let x = 0; x < W; x++) {
574+
const i = y * H + x;
575+
const color = _pixels[i];
576+
setPixel(x, y, "." === color ? null : ~~color);
577+
}
578+
}
579+
};
580+
520581
// Download
521582
downloadButton.onclick = (ev) => {
522583
const offcanvas = new OffscreenCanvas(art.width, art.height);
@@ -536,7 +597,7 @@ <h1>litepixel</h1>
536597
copy.onclick = () => {
537598
if (!navigator.clipboard) {
538599
return alert(
539-
"Your browser not support this feature. Consider installing Firefox or Chrome."
600+
"Your browser not support this feature. Consider installing Firefox or Chrome.",
540601
);
541602
}
542603
if (!exported.value) return;
@@ -548,7 +609,7 @@ <h1>litepixel</h1>
548609
(err) => {
549610
alert("Error: Unable to generate your shareable url!");
550611
console.error("Error on copying text to clipboard:", err);
551-
}
612+
},
552613
);
553614
};
554615

@@ -573,7 +634,7 @@ <h1>litepixel</h1>
573634
const el = document.createElement("label");
574635
el.dataset.color = i;
575636
el.innerHTML = `<span style="background:${color}">${i.toString(
576-
16
637+
16,
577638
)}</span>`;
578639
palette.appendChild(el);
579640
el.title = "Select color #" + i;

0 commit comments

Comments
 (0)