@@ -6,28 +6,38 @@ interface Edge {
66}
77
88interface Options {
9+ x ?: number ;
10+ y ?: number ;
911 width ?: number ;
1012 height ?: number ;
1113 scale ?: number ;
1214 offsetX ?: number ;
1315 offsetY ?: number ;
16+ include ?: number [ ] ;
1417}
1518
1619export function toIndex ( x : number , y : number , width : number ) {
1720 return y * width + x ;
1821}
1922
23+ /**
24+ * Convert a 2d array to a SVG path.
25+ * @param data
26+ * @param options
27+ * @returns path string
28+ */
2029export default function bitmaskToPath ( data : number [ ] | number [ ] [ ] , options : Options = { } ) {
2130
22- let bitmask : number [ ] | number [ ] [ ] ,
31+ let bitmask : number [ ] ,
2332 width : number ,
2433 height : number ,
2534 scale = 1 ,
2635 offsetX = 0 ,
27- offsetY = 0 ;
36+ offsetY = 0 ,
37+ include = [ 1 ] ;
2838
2939 if ( options . width ) {
30- bitmask = data ;
40+ bitmask = data as number [ ] ; // already flat
3141 width = options . width ;
3242 height = bitmask . length / width ;
3343 if ( height % 1 !== 0 ) {
@@ -53,6 +63,10 @@ export default function bitmaskToPath(data: number[] | number[][], options: Opti
5363 offsetY = options . offsetY ;
5464 }
5565
66+ if ( options . include ) {
67+ include = options . include ;
68+ }
69+
5670 // Naively copy into a new bitmask with a border of 1 to make sampling easier (no out of bounds checks)
5771 const newWidth = width + 2 ;
5872 const newHeight = height + 2 ;
@@ -65,7 +79,7 @@ export default function bitmaskToPath(data: number[] | number[][], options: Opti
6579
6680 for ( let y = 0 ; y < height ; ++ y ) {
6781 for ( let x = 0 ; x < width ; ++ x ) {
68- bm [ BMXYToIndex ( x , y ) ] = bitmask [ toIndex ( x , y , width ) ] ;
82+ bm [ BMXYToIndex ( x , y ) ] = include . includes ( bitmask [ toIndex ( x , y , width ) ] ) ? 1 : 0 ;
6983 }
7084 }
7185
@@ -101,7 +115,7 @@ export default function bitmaskToPath(data: number[] | number[][], options: Opti
101115
102116 for ( let y = 0 ; y < height ; ++ y ) {
103117 for ( let x = 0 ; x < width ; ++ x ) {
104- if ( bm [ BMXYToIndex ( x , y ) ] == 1 ) {
118+ if ( bm [ BMXYToIndex ( x , y ) ] === 1 ) {
105119 const left = bm [ BMXYToIndex ( x - 1 , y ) ] ;
106120 if ( left == 0 ) {
107121 const edge = edges [ EdgeYIndex ( x , y ) ] ;
@@ -116,7 +130,7 @@ export default function bitmaskToPath(data: number[] | number[][], options: Opti
116130 UnionGroup ( edge ) ;
117131 }
118132 const right = bm [ BMXYToIndex ( x + 1 , y ) ] ;
119- if ( right == 0 ) {
133+ if ( right === 0 ) {
120134 const edge = edges [ EdgeYIndex ( x + 1 , y ) ] ;
121135 SetEdge ( edge , x + 1 , y ) ;
122136 if ( bm [ BMXYToIndex ( x + 1 , y + 1 ) ] ) {
@@ -129,7 +143,7 @@ export default function bitmaskToPath(data: number[] | number[][], options: Opti
129143 UnionGroup ( edge ) ;
130144 }
131145 const top = bm [ BMXYToIndex ( x , y - 1 ) ] ;
132- if ( top == 0 ) {
146+ if ( top === 0 ) {
133147 const edge : Edge = edges [ EdgeXIndex ( x , y ) ] ;
134148 SetEdge ( edge , x , y ) ;
135149 if ( bm [ BMXYToIndex ( x + 1 , y - 1 ) ] ) {
@@ -142,7 +156,7 @@ export default function bitmaskToPath(data: number[] | number[][], options: Opti
142156 UnionGroup ( edge ) ;
143157 }
144158 const bottom = bm [ BMXYToIndex ( x , y + 1 ) ] ;
145- if ( bottom == 0 ) {
159+ if ( bottom === 0 ) {
146160 const edge = edges [ EdgeXIndex ( x , y + 1 ) ] ;
147161 SetEdge ( edge , x + 1 , y + 1 ) ;
148162 if ( bm [ BMXYToIndex ( x - 1 , y + 1 ) ] ) {
0 commit comments