@@ -381,10 +381,31 @@ export default class PgInputPixelEditor extends HTMLElement {
381381 } ) ) ;
382382 }
383383
384- #setPenPreview( pixels : Pixel [ ] , centerX : number , centerY : number , previousX : number , previousY : number ) {
384+ #previousPreviewRect = { x : 0 , y : 0 , width : 1 , height : 1 } ;
385+ #clearStampPreview( ) {
386+ const { x, y, width, height } = this . #previousPreviewRect;
387+ this . #context. clearRect ( x , y , width , height ) ;
388+ // base layer to main canvas
389+ this . #context. drawImage (
390+ this . #baseLayer,
391+ x , y , width , height ,
392+ x , y , width , height
393+ ) ;
394+ // edit to main canvas
395+ this . #context. drawImage (
396+ this . #editLayer,
397+ x , y , width , height ,
398+ x , y , width , height
399+ ) ;
400+ this . #context. drawImage (
401+ this . #previewLayer,
402+ x , y , width , height ,
403+ x , y , width , height
404+ ) ;
405+ }
406+
407+ #setStampPreview( pixels : Pixel [ ] , centerX : number , centerY : number , previousX : number , previousY : number ) {
385408 const totalSize = this . size + this . gridSize ;
386- const actualWidth = this . width * totalSize - this . gridSize ;
387- const actualHeight = this . height * totalSize - this . gridSize ;
388409 const { minX, maxX, minY, maxY } = pixels . reduce ( ( previous , current ) => {
389410 return {
390411 minX : Math . min ( previous . minX , current . x , previousX ) ,
@@ -397,7 +418,6 @@ export default class PgInputPixelEditor extends HTMLElement {
397418 const offsetY = previousY - centerY ;
398419 const x = offsetX < 0 ? ( minX + offsetX ) * totalSize : minX * totalSize ;
399420 const y = offsetY < 0 ? ( minY + offsetY ) * totalSize : minY * totalSize ;
400- console . log ( offsetX , offsetY , x , y ) ;
401421 const width = ( maxX - minX + 1 + Math . abs ( offsetX ) ) * totalSize ;
402422 const height = ( maxY - minY + 1 + Math . abs ( offsetY ) ) * totalSize ;
403423 this . #context. clearRect ( x , y , width , height ) ;
@@ -419,12 +439,13 @@ export default class PgInputPixelEditor extends HTMLElement {
419439 this . #previewLayerContext. fillStyle = '#1B79C8' ;
420440 this . #previewLayerContext. fillRect ( x * totalSize + 2 , y * totalSize + 2 , this . size - 4 , this . size - 4 ) ;
421441 } ) ;
422- //console.log(x, y, width, height);
423442 this . #context. drawImage (
424443 this . #previewLayer,
425444 x , y , width , height ,
426445 x , y , width , height
427446 ) ;
447+ // Store to clear
448+ this . #previousPreviewRect = { x, y, width, height } ;
428449 // Debug
429450 this . dispatchEvent ( new CustomEvent ( 'debug' , {
430451 detail : {
@@ -686,7 +707,7 @@ export default class PgInputPixelEditor extends HTMLElement {
686707 let newY = Math . floor ( ( event . clientY - rect . top ) / totalSize ) ;
687708 if ( newX === this . #moveX && newY === this . #moveY) { return ; }
688709 if ( newX < 0 || newY < 0 ) { return ; }
689- this . #setPenPreview (
710+ this . #setStampPreview (
690711 this . #inputStamp. map ( arr => ( { x : arr [ 0 ] + newX , y : arr [ 1 ] + newY } ) ) ,
691712 newX ,
692713 newY ,
@@ -884,6 +905,8 @@ export default class PgInputPixelEditor extends HTMLElement {
884905 inputModeStamp ( stamp : number [ ] [ ] ) {
885906 this . #inputStamp = stamp ;
886907 this . #inputMode = InputMode . Stamp ;
908+ // Clear previous pixel
909+ this . #clearStampPreview( ) ;
887910 }
888911
889912 inputModePixel ( ) {
0 commit comments