@@ -7,12 +7,13 @@ import cloneGrid from './utils/cloneGrid';
77import getEllipseOutlinePixels from './utils/getEllipseOutlinePixels' ;
88import { WHITE } from './utils/constants' ;
99import getLinePixels from './utils/getLinePixels' ;
10- import getRectanglePixels from './utils/getRectangleOutlinePixels ' ;
10+ import getRectanglePixels from './utils/getRectanglePixels ' ;
1111import getRectangleOutlinePixels from './utils/getRectangleOutlinePixels' ;
1212import fillGrid from './utils/fillGrid' ;
1313import iterateGrid from './utils/interateGrid' ;
1414import bitmaskToPath from './utils/bitmapToMask' ;
1515import createLayer from './utils/createLayer' ;
16+ import diffGrid from './utils/diffGrid' ;
1617
1718type Pixel = { x : number , y : number } ;
1819
@@ -40,6 +41,9 @@ export default class PgInputPixelEditor extends HTMLElement {
4041 #startY: number = - 1 ;
4142 #x: number = - 1 ;
4243 #y: number = - 1 ;
44+ #isCtrl: boolean = false ;
45+ #isShift: boolean = false ;
46+ #isAlt: boolean = false ;
4347 #data: number [ ] [ ] = [ ] ;
4448 #undoHistory: [ number , number , number ] [ ] [ ] = [ ] ;
4549 #redoHistory: [ number , number , number ] [ ] [ ] = [ ] ;
@@ -61,11 +65,11 @@ export default class PgInputPixelEditor extends HTMLElement {
6165 this . #context = context ;
6266 // Wire Up Events
6367 this . $canvas . addEventListener (
64- 'contextMenu ' ,
68+ 'contextmenu ' ,
6569 this . handleContextMenu . bind ( this )
6670 ) ;
6771 this . $canvas . addEventListener (
68- 'doubleClick ' ,
72+ 'doubleclick ' ,
6973 this . handleDoubleClick . bind ( this )
7074 ) ;
7175 this . $canvas . addEventListener (
@@ -193,17 +197,22 @@ export default class PgInputPixelEditor extends HTMLElement {
193197 maxY : Math . max ( previous . maxY , current . y , previousY )
194198 } ;
195199 } , { minX : this . width , maxX : 0 , minY : this . height , maxY : 0 } ) ;
200+ const x = minX * totalSize ;
201+ const y = minY * totalSize ;
202+ const width = ( maxX - minX + 1 ) * totalSize ;
203+ const height = ( maxY - minY + 1 ) * totalSize ;
204+ this . #context. clearRect ( x , y , width , height ) ;
196205 // base layer to main canvas
197206 this . #context. drawImage (
198207 this . #baseLayer,
199- minX * totalSize , minY * totalSize , maxX * totalSize , maxY * totalSize ,
200- minX * totalSize , minY * totalSize , maxX * totalSize , maxY * totalSize
208+ x , y , width , height ,
209+ x , y , width , height
201210 ) ;
202211 // edit to main canvas
203212 this . #context. drawImage (
204213 this . #editLayer,
205- minX * totalSize , minY * totalSize , maxX * totalSize , maxY * totalSize ,
206- minX * totalSize , minY * totalSize , maxX * totalSize , maxY * totalSize
214+ x , y , width , height ,
215+ x , y , width , height
207216 ) ;
208217 // preview layer
209218 this . #previewLayerContext. clearRect ( 0 , 0 , actualWidth , actualHeight ) ;
@@ -222,20 +231,28 @@ export default class PgInputPixelEditor extends HTMLElement {
222231 // preview layer to main canvas
223232 this . #context. drawImage (
224233 this . #previewLayer,
225- minX * totalSize , minY * totalSize , maxX * totalSize , maxY * totalSize ,
226- minX * totalSize , minY * totalSize , maxX * totalSize , maxY * totalSize
234+ x , y , width , height ,
235+ x , y , width , height
227236 ) ;
237+ // Debug
228238 this . dispatchEvent ( new CustomEvent ( 'debug' , {
229239 detail : {
240+ x,
241+ y,
242+ width,
243+ height,
244+ canvas : this . $canvas ,
245+ context : this . #context,
230246 editLayer : this . #editLayer,
247+ noEditLayer : this . #noEditLayer,
231248 baseLayer : this . #baseLayer,
232249 previewLayer : this . #previewLayer
233250 }
234251 } ) ) ;
235- console . log ( 'render preview' , minX , minY , maxX , maxY ) ;
236252 }
237253
238254 handleKeyDown ( event : KeyboardEvent ) {
255+ console . log ( event . shiftKey , event . ctrlKey , event . altKey , event . key ) ;
239256 if ( event . key === ' ' ) {
240257 console . log ( 'space!' )
241258 }
@@ -259,6 +276,11 @@ export default class PgInputPixelEditor extends HTMLElement {
259276 event . stopPropagation ( ) ;
260277 return ;
261278 }
279+ // Update Modifiers
280+ this . #isAlt = event . altKey ;
281+ this . #isCtrl = event . ctrlKey ;
282+ this . #isShift = event . shiftKey ;
283+ // Drawing
262284 const rect = this . $canvas . getBoundingClientRect ( ) ;
263285 const totalSize = this . size + this . gridSize ;
264286 let newX = Math . floor ( ( event . clientX - rect . left ) / totalSize ) ;
@@ -273,6 +295,7 @@ export default class PgInputPixelEditor extends HTMLElement {
273295 this . #x = newX ;
274296 this . #y = newY ;
275297 const color = event . buttons === 32 ? 0 : 1 ;
298+ console . log ( this . #colors, this . #colors[ color ] , event . buttons ) ;
276299 switch ( this . #inputMode) {
277300 case InputMode . Pixel :
278301 this . #setPixel( newX , newY , color ) ;
@@ -337,6 +360,9 @@ export default class PgInputPixelEditor extends HTMLElement {
337360 handlePointerMove ( event : PointerEvent ) {
338361 const canvas = this . $canvas ;
339362 if ( this . #isPressed) {
363+ this . #isAlt = event . altKey ;
364+ this . #isCtrl = event . ctrlKey ;
365+ this . #isShift = event . shiftKey ;
340366 const data = this . #data;
341367 const rect = canvas . getBoundingClientRect ( ) ;
342368 const totalSize = this . size + this . gridSize ;
@@ -427,14 +453,15 @@ export default class PgInputPixelEditor extends HTMLElement {
427453 // base layer to main canvas
428454 this . #context. drawImage ( this . #baseLayer, 0 , 0 ) ;
429455 // editing layer to main canvas
430- this . #context. drawImage ( this . #editLayer , 0 , 0 ) ;
456+ this . #context. drawImage ( this . #noEditLayer , 0 , 0 ) ;
431457 }
432458
433459 mergeColor ( fromIndex : number , toIndex : number ) {
434460 // ToDo: Code this
435461 }
436462 clear ( ) {
437463 this . #data = fillGrid ( this . width , this . height ) ;
464+ this . #updateGrid( ) ;
438465 }
439466 clearHistory ( ) {
440467 this . #undoHistory = [ ] ;
0 commit comments