@@ -21,6 +21,7 @@ import { getGridColorIndexes } from './utils/getGridColorIndexes';
2121import { getFloodFill } from './utils/getFloodFill' ;
2222import { readMetadata , textEncode , writeMetadata } from './utils/pngMetadata' ;
2323import { canvasToPngBuffer } from './utils/canvasToPngBuffer' ;
24+ import { blobToImage } from './utils/blobToImage' ;
2425
2526type Color = [ number , number , number , number ] ;
2627
@@ -611,7 +612,7 @@ export default class PgInputPixelEditor extends HTMLElement {
611612 }
612613 }
613614
614- handleKeyDown ( event : KeyboardEvent ) {
615+ async handleKeyDown ( event : KeyboardEvent ) {
615616 console . log ( event . shiftKey , event . ctrlKey , event . altKey , event . key ) ;
616617 this . #isShift = true ;
617618 switch ( event . key ) {
@@ -634,10 +635,10 @@ export default class PgInputPixelEditor extends HTMLElement {
634635 switch ( event . key ) {
635636 case 'c' :
636637 if ( this . hasSelection ( ) ) {
637- const image = this . getSelectionPng ( ) ;
638+ const image = await this . getSelectionPng ( ) ;
638639 this . copyPngToClipboard ( image , { } ) ;
639640 } else {
640- const image = this . getExportPng ( ) ;
641+ const image = await this . getExportPng ( ) ;
641642 this . copyPngToClipboard ( image , { } ) ;
642643 }
643644 break ;
@@ -1353,22 +1354,29 @@ export default class PgInputPixelEditor extends HTMLElement {
13531354 getExportCanvas ( options : Export = { } ) {
13541355 const canvas = document . createElement ( 'canvas' ) as HTMLCanvasElement ;
13551356 const context = canvas . getContext ( '2d' ) as CanvasRenderingContext2D ;
1356- const scale = ( options . scale ?? 1 ) ;
1357+ const scale = options . scale ?? 1 ;
1358+ const offsetX = options . x ?? 0 ;
1359+ const offsetY = options . y ?? 0 ;
13571360 canvas . width = ( options . width ?? this . width ) * scale ;
13581361 canvas . height = ( options . height ?? this . height ) * scale ;
1359- this . #export. forEach ( ( row , y ) => {
1360- row . forEach ( ( column , x ) => {
1361- const [ r , g , b , a ] = this . #colors[ column ] ;
1362+ const rows = options . height ?? this . #export. length ;
1363+ const columns = options . width ?? this . #export[ 0 ] . length ;
1364+ for ( let y = 0 ; y < rows ; y ++ ) {
1365+ for ( let x = 0 ; x < columns ; x ++ ) {
1366+ const relativeX = x + offsetX ;
1367+ const relativeY = y + offsetY ;
1368+ const color = this . #export[ relativeY ] [ relativeX ] ;
1369+ const [ r , g , b , a ] = this . #colors[ color ] ;
13621370 context . fillStyle = `rgba(${ r } ,${ g } ,${ b } ,${ a } )` ;
13631371 context . fillRect ( x * scale , y * scale , scale , scale ) ;
1364- } ) ;
1365- } ) ;
1372+ }
1373+ }
13661374 return canvas ;
13671375 }
13681376
1369- async #getExportPng( option : Export = { } ) {
1377+ async #getExportPng( options : Export = { } ) {
13701378 return new Promise < Blob > ( ( resolve , reject ) => {
1371- this . getExportCanvas ( option ) . toBlob ( ( blob ) => {
1379+ this . getExportCanvas ( options ) . toBlob ( ( blob ) => {
13721380 if ( blob ) {
13731381 resolve ( blob ) ;
13741382 } else {
@@ -1378,19 +1386,20 @@ export default class PgInputPixelEditor extends HTMLElement {
13781386 } ) ;
13791387 }
13801388
1381- async getExportPng ( option : Export = { } , meta : any = { } ) {
1382- const blob = await this . #getExportPng( option ) ;
1389+ async getExportPng ( options : Export = { } , meta : any = null ) {
1390+ const blob = await this . #getExportPng( options ) ;
13831391 if ( meta ) {
13841392 const arrayBuffer = await blob . arrayBuffer ( ) ;
13851393 const file = new Uint8Array ( arrayBuffer ) ;
13861394 const data = { tEXt : meta } ;
1387- return writeMetadata ( file , data ) ;
1395+ const blobWithMeta = writeMetadata ( file , data ) ;
1396+ return blobWithMeta . buffer ;
13881397 } else {
13891398 return blob ;
13901399 }
13911400 }
13921401
1393- getSelectionPng ( ) {
1402+ async getSelectionPng ( options : Export = { } , meta : any = null ) {
13941403 const xList : number [ ] = [ ] ;
13951404 const yList : number [ ] = [ ] ;
13961405 this . #selectionPixels. forEach ( ( [ x , y ] ) => {
@@ -1401,7 +1410,13 @@ export default class PgInputPixelEditor extends HTMLElement {
14011410 minY = Math . min ( ...yList ) ,
14021411 maxX = Math . max ( ...xList ) ,
14031412 maxY = Math . max ( ...yList ) ;
1404- console . log ( minX , minY , maxX , maxY ) ;
1413+ return await this . getExportPng ( {
1414+ ...options ,
1415+ x : minX ,
1416+ y : minY ,
1417+ width : maxX - minX ,
1418+ height : maxY - minY ,
1419+ } , meta ) ;
14051420 }
14061421
14071422}
0 commit comments