1
1
<!-- Inspired by https://github.com/xem/miniPixelArt -->
2
- <!DOCTYPE html>
2
+ <!doctype html>
3
3
< html lang ="en ">
4
4
< head >
5
5
< meta charset ="UTF-8 " />
@@ -207,10 +207,11 @@ <h1>litepixel</h1>
207
207
>
208
208
< button id ="clearButton "> clear</ button >
209
209
< button id ="exportButton "> export</ button >
210
+ < button id ="importButton "> import</ button >
210
211
< button id ="downloadButton "> download</ button >
211
212
< textarea
212
213
id ="exported "
213
- rows ="10 "
214
+ rows ="20 "
214
215
readonly
215
216
onclick ="this.select() "
216
217
> </ textarea >
@@ -436,7 +437,10 @@ <h1>litepixel</h1>
436
437
return - 1 ;
437
438
}
438
439
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 ;
440
444
}
441
445
442
446
/**
@@ -485,7 +489,7 @@ <h1>litepixel</h1>
485
489
clearButton . onclick = ( ) => {
486
490
if (
487
491
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." ,
489
493
)
490
494
) {
491
495
pixels . length = 0 ;
@@ -498,25 +502,82 @@ <h1>litepixel</h1>
498
502
// Export
499
503
exportButton . onclick = function ( ) {
500
504
let output = "" ;
505
+ const _pixels = [ ] ;
501
506
502
- output += `paint(${ W } , ${ H } , [\n` ;
507
+ output += `const renameThisVar = paint(${ W } , ${ H } , [\n` ;
503
508
504
509
for ( let y = 0 ; y < H ; y ++ ) {
505
510
output += " '" ;
506
511
for ( let x = 0 ; x < W ; x ++ ) {
507
512
let color = getPixel ( x , y ) ;
508
513
if ( - 1 === ~ ~ color ) color = "." ;
509
514
output += color >= 0 ? ( color % colors . length ) . toString ( 16 ) : color ;
515
+ _pixels . push ( color === "." ? "#" : color ) ;
510
516
}
511
517
output += "',\n" ;
512
518
}
513
519
514
520
output += "], {\n scale: 1\n})" ;
515
521
522
+ const importCode = `Art Code: ${ W } x${ H } /${ _pixels . join ( " " ) } ` ;
523
+ output = `// ${ importCode } \n` + output ;
524
+
516
525
exported . value = output ;
526
+
517
527
copy . disabled = false ;
518
528
} ;
519
529
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
+
520
581
// Download
521
582
downloadButton . onclick = ( ev ) => {
522
583
const offcanvas = new OffscreenCanvas ( art . width , art . height ) ;
@@ -536,7 +597,7 @@ <h1>litepixel</h1>
536
597
copy . onclick = ( ) => {
537
598
if ( ! navigator . clipboard ) {
538
599
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." ,
540
601
) ;
541
602
}
542
603
if ( ! exported . value ) return ;
@@ -548,7 +609,7 @@ <h1>litepixel</h1>
548
609
( err ) => {
549
610
alert ( "Error: Unable to generate your shareable url!" ) ;
550
611
console . error ( "Error on copying text to clipboard:" , err ) ;
551
- }
612
+ } ,
552
613
) ;
553
614
} ;
554
615
@@ -573,7 +634,7 @@ <h1>litepixel</h1>
573
634
const el = document . createElement ( "label" ) ;
574
635
el . dataset . color = i ;
575
636
el . innerHTML = `<span style="background:${ color } ">${ i . toString (
576
- 16
637
+ 16 ,
577
638
) } </span>`;
578
639
palette . appendChild ( el ) ;
579
640
el . title = "Select color #" + i ;
0 commit comments