@@ -9,6 +9,7 @@ import getEllipseOutlinePixels from './utils/getEllipseOutlinePixels';
99import { WHITE , Pixel } from './utils/constants' ;
1010import getLinePixels from './utils/getLinePixels' ;
1111import getRectanglePixels from './utils/getRectanglePixels' ;
12+ import getLassoPixels from './utils/getLassoPixels' ;
1213import getRectangleOutlinePixels from './utils/getRectangleOutlinePixels' ;
1314import fillGrid from './utils/fillGrid' ;
1415import iterateGrid from './utils/interateGrid' ;
@@ -147,6 +148,9 @@ export default class PgInputPixelEditor extends HTMLElement {
147148 #startColor: number = - 1 ;
148149 #startX: number = - 1 ;
149150 #startY: number = - 1 ;
151+ #lassoPath: [ number , number ] [ ] = [ ] ;
152+ #lassoOutlinePixels: Set < string > = new Set ( ) ;
153+ #lassoOutlineList: { x : number ; y : number } [ ] = [ ] ;
150154 #x: number = - 1 ;
151155 #y: number = - 1 ;
152156 #layer: number [ ] = [ 0 ] ;
@@ -784,6 +788,11 @@ export default class PgInputPixelEditor extends HTMLElement {
784788 }
785789 } ) ) ;
786790 break ;
791+ case InputMode . SelectLasso :
792+ this . #lassoPath = [ [ newX , newY ] ] ;
793+ this . #lassoOutlinePixels = new Set ( ) ;
794+ this . #lassoOutlineList = [ ] ;
795+ break ;
787796 case InputMode . Pixel :
788797 this . #setPixel( newX , newY , color ) ;
789798 break ;
@@ -878,7 +887,16 @@ export default class PgInputPixelEditor extends HTMLElement {
878887 this . $selectionPath . setAttribute ( 'd' , bitmaskToPath ( this . #selection, { scale : this . size } ) [ 0 ] ) ;
879888 break ;
880889 case InputMode . SelectLasso :
881-
890+ this . #clearSelectionPreview( ) ;
891+ if ( ! event . shiftKey ) {
892+ this . clearSelection ( ) ;
893+ }
894+ getLassoPixels ( this . #lassoPath) . forEach ( ( { x, y } ) => {
895+ this . #setSelectionPixel( x , y ) ;
896+ } ) ;
897+ this . $selectionPathPreview . classList . toggle ( 'hide' , true ) ;
898+ this . $selectionPath . classList . toggle ( 'hide' , false ) ;
899+ this . $selectionPath . setAttribute ( 'd' , bitmaskToPath ( this . #selection, { scale : this . size } ) [ 0 ] ) ;
882900 break ;
883901 case InputMode . Line :
884902 getLinePixels ( this . #startX, this . #startY, newX , newY ) . forEach ( ( { x, y } ) => {
@@ -969,6 +987,25 @@ export default class PgInputPixelEditor extends HTMLElement {
969987 case InputMode . SelectEllipse :
970988 this . #setSelectionPreview( getEllipsePixels ( startX , startY , lastX , lastY ) ) ;
971989 break ;
990+ case InputMode . SelectLasso : {
991+ const [ sx , sy ] = this . #lassoPath[ 0 ] ;
992+ for ( const [ px , py ] of points ) {
993+ const [ prevX , prevY ] = this . #lassoPath. at ( - 1 ) ! ;
994+ for ( const { x : lx , y : ly } of getLinePixels ( prevX , prevY , px , py ) ) {
995+ const key = `${ lx } ,${ ly } ` ;
996+ if ( ! this . #lassoOutlinePixels. has ( key ) ) {
997+ this . #lassoOutlinePixels. add ( key ) ;
998+ this . #lassoOutlineList. push ( { x : lx , y : ly } ) ;
999+ }
1000+ }
1001+ this . #lassoPath. push ( [ px , py ] ) ;
1002+ }
1003+ const [ cx , cy ] = this . #lassoPath. at ( - 1 ) ! ;
1004+ const closing = getLinePixels ( cx , cy , sx , sy )
1005+ . filter ( ( { x : lx , y : ly } ) => ! this . #lassoOutlinePixels. has ( `${ lx } ,${ ly } ` ) ) ;
1006+ this . #setSelectionPreview( [ ...this . #lassoOutlineList, ...closing ] ) ;
1007+ break ;
1008+ }
9721009 case InputMode . Pixel :
9731010 for ( var point of points ) {
9741011 this . #setPixel( point [ 0 ] , point [ 1 ] , color ) ;
0 commit comments