pixel-data-js 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pixel-data-js",
3
3
  "type": "module",
4
- "version": "0.33.0",
4
+ "version": "0.35.0",
5
5
  "packageManager": "pnpm@10.33.0",
6
6
  "description": "JS Pixel and ImageData operations",
7
7
  "author": {
@@ -1,11 +1,10 @@
1
1
  import { forEachLinePoint } from '../Algorithm/forEachLinePoint'
2
2
  import type { PixelEngineConfig } from '../History/PixelEngineConfig'
3
- import { _macro_paintRectCenterOffset } from '../Internal/macros'
4
3
  import type { Rect } from '../Rect/_rect-types'
5
4
  import { trimRectBounds } from '../Rect/trimRectBounds'
6
5
  import type { AlphaMaskTile } from '../Tile/_tile-types'
7
6
  import type { TilePool } from '../Tile/TilePool'
8
- import type { PaintAlphaMask, PaintBinaryMask } from './_paint-types'
7
+ import type { PaintAlphaMask, PaintBinaryMask, PaintRect } from './_paint-types'
9
8
  import { eachTileInBounds } from './eachTileInBounds'
10
9
 
11
10
  export class AlphaMaskPaintBuffer {
@@ -187,15 +186,13 @@ export class AlphaMaskPaintBuffer {
187
186
 
188
187
  paintRect(
189
188
  alpha: number,
190
- brushWidth: number,
191
- brushHeight: number,
189
+ brush: PaintRect,
192
190
  x: number,
193
191
  y: number,
194
192
  ): boolean
195
193
  paintRect(
196
194
  alpha: number,
197
- brushWidth: number,
198
- brushHeight: number,
195
+ brush: PaintRect,
199
196
  startX: number,
200
197
  startY: number,
201
198
  endX: number,
@@ -203,8 +200,7 @@ export class AlphaMaskPaintBuffer {
203
200
  ): boolean
204
201
  paintRect(
205
202
  alpha: number,
206
- brushWidth: number,
207
- brushHeight: number,
203
+ brush: PaintRect,
208
204
  x0: number,
209
205
  y0: number,
210
206
  x1: number = x0,
@@ -218,8 +214,10 @@ export class AlphaMaskPaintBuffer {
218
214
  const tileMask = config.tileMask
219
215
  const target = config.target
220
216
 
221
- const centerOffsetX = _macro_paintRectCenterOffset(brushWidth)
222
- const centerOffsetY = _macro_paintRectCenterOffset(brushHeight)
217
+ const brushWidth = brush.w
218
+ const brushHeight = brush.h
219
+ const centerOffsetX = brush.centerOffsetX
220
+ const centerOffsetY = brush.centerOffsetY
223
221
 
224
222
  const trimRectBoundsFn = this.trimRectBoundsFn
225
223
  const eachTileInBoundsFn = this.eachTileInBoundsFn
@@ -1,11 +1,10 @@
1
1
  import { forEachLinePoint } from '../Algorithm/forEachLinePoint'
2
2
  import type { PixelEngineConfig } from '../History/PixelEngineConfig'
3
- import { _macro_paintRectCenterOffset } from '../Internal/macros'
4
3
  import type { Rect } from '../Rect/_rect-types'
5
4
  import { trimRectBounds } from '../Rect/trimRectBounds'
6
5
  import type { BinaryMaskTile } from '../Tile/_tile-types'
7
6
  import type { TilePool } from '../Tile/TilePool'
8
- import type { PaintBinaryMask } from './_paint-types'
7
+ import type { PaintBinaryMask, PaintRect } from './_paint-types'
9
8
  import { eachTileInBounds } from './eachTileInBounds'
10
9
 
11
10
  export class BinaryMaskPaintBuffer {
@@ -104,22 +103,19 @@ export class BinaryMaskPaintBuffer {
104
103
  }
105
104
 
106
105
  paintRect(
107
- brushWidth: number,
108
- brushHeight: number,
106
+ brush: PaintRect,
109
107
  x: number,
110
108
  y: number,
111
109
  ): boolean
112
110
  paintRect(
113
- brushWidth: number,
114
- brushHeight: number,
111
+ brush: PaintRect,
115
112
  startX: number,
116
113
  startY: number,
117
114
  endX: number,
118
115
  endY: number,
119
116
  ): boolean
120
117
  paintRect(
121
- brushWidth: number,
122
- brushHeight: number,
118
+ brush: PaintRect,
123
119
  x0: number,
124
120
  y0: number,
125
121
  x1: number = x0,
@@ -133,8 +129,10 @@ export class BinaryMaskPaintBuffer {
133
129
  const tileMask = config.tileMask
134
130
  const target = config.target
135
131
 
136
- const centerOffsetX = _macro_paintRectCenterOffset(brushWidth)
137
- const centerOffsetY = _macro_paintRectCenterOffset(brushHeight)
132
+ const brushWidth = brush.w
133
+ const brushHeight = brush.h
134
+ const centerOffsetX = brush.centerOffsetX
135
+ const centerOffsetY = brush.centerOffsetY
138
136
 
139
137
  const trimRectBoundsFn = this.trimRectBoundsFn
140
138
  const eachTileInBoundsFn = this.eachTileInBoundsFn
@@ -1,12 +1,11 @@
1
1
  import type { Color32 } from '../_types'
2
2
  import { forEachLinePoint } from '../Algorithm/forEachLinePoint'
3
3
  import type { PixelEngineConfig } from '../History/PixelEngineConfig'
4
- import { _macro_paintRectCenterOffset } from '../Internal/macros'
5
4
  import type { Rect } from '../Rect/_rect-types'
6
5
  import { trimRectBounds } from '../Rect/trimRectBounds'
7
6
  import type { PixelTile } from '../Tile/_tile-types'
8
7
  import type { TilePool } from '../Tile/TilePool'
9
- import type { PaintAlphaMask, PaintBinaryMask } from './_paint-types'
8
+ import type { PaintAlphaMask, PaintBinaryMask, PaintRect } from './_paint-types'
10
9
  import { eachTileInBounds } from './eachTileInBounds'
11
10
 
12
11
  export class ColorPaintBuffer {
@@ -191,15 +190,13 @@ export class ColorPaintBuffer {
191
190
 
192
191
  paintRect(
193
192
  color: Color32,
194
- brushWidth: number,
195
- brushHeight: number,
193
+ brush: PaintRect,
196
194
  x: number,
197
195
  y: number,
198
196
  ): boolean
199
197
  paintRect(
200
198
  color: Color32,
201
- brushWidth: number,
202
- brushHeight: number,
199
+ brush: PaintRect,
203
200
  startX: number,
204
201
  startY: number,
205
202
  endX: number,
@@ -207,8 +204,7 @@ export class ColorPaintBuffer {
207
204
  ): boolean
208
205
  paintRect(
209
206
  color: Color32,
210
- brushWidth: number,
211
- brushHeight: number,
207
+ brush: PaintRect,
212
208
  x0: number,
213
209
  y0: number,
214
210
  x1: number = x0,
@@ -225,8 +221,10 @@ export class ColorPaintBuffer {
225
221
  const tileMask = config.tileMask
226
222
  const target = config.target
227
223
 
228
- const centerOffsetX = _macro_paintRectCenterOffset(brushWidth)
229
- const centerOffsetY = _macro_paintRectCenterOffset(brushHeight)
224
+ const brushWidth = brush.w
225
+ const brushHeight = brush.h
226
+ const centerOffsetX = brush.centerOffsetX
227
+ const centerOffsetY = brush.centerOffsetY
230
228
 
231
229
  let changed = false
232
230
 
@@ -0,0 +1,14 @@
1
+ import { _macro_paintRectCenterOffset } from '../Internal/macros'
2
+ import { PaintMaskOutline, type PaintRect } from './_paint-types'
3
+
4
+ export function makePaintRect(w: number, h: number): PaintRect {
5
+ return {
6
+ type: null,
7
+ outlineType: PaintMaskOutline.RECT,
8
+ data: null,
9
+ w,
10
+ h,
11
+ centerOffsetX: _macro_paintRectCenterOffset(w),
12
+ centerOffsetY: _macro_paintRectCenterOffset(h),
13
+ }
14
+ }
@@ -11,7 +11,7 @@ import { makeRectBinaryMaskOutline } from '../../Mask/BinaryMask/makeRectBinaryM
11
11
  import { fillPixelDataBinaryMask } from '../../PixelData/fillPixelDataBinaryMask'
12
12
  import { makeReusablePixelData } from '../../PixelData/ReusablePixelData'
13
13
  import type { Rect } from '../../Rect/_rect-types'
14
- import { type PaintMask, PaintMaskOutline } from '../_paint-types'
14
+ import { type PaintBrush, PaintMaskOutline } from '../_paint-types'
15
15
 
16
16
  export type PaintCursorRenderer = ReturnType<typeof makePaintCursorRenderer>
17
17
 
@@ -27,39 +27,42 @@ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenC
27
27
  let _color = packColor(0, 255, 255, 255)
28
28
  let _scale = 1
29
29
 
30
- let currentMask: PaintMask = {
31
- type: MaskType.BINARY,
30
+ let currentBrush: PaintBrush = {
31
+ type: null,
32
32
  outlineType: PaintMaskOutline.RECT,
33
33
  w: 1,
34
34
  h: 1,
35
35
  centerOffsetX: _macro_paintRectCenterOffset(10),
36
36
  centerOffsetY: _macro_paintRectCenterOffset(10),
37
- } as PaintMask
37
+ data: null,
38
+ }
38
39
 
39
40
  let outline: BinaryMask
40
41
 
41
- function update(paintMask?: PaintMask, scale?: number, color?: Color32, alphaThreshold = 127) {
42
- currentMask = paintMask ?? currentMask
42
+ function update(paintMask?: PaintBrush, scale?: number, color?: Color32, alphaThreshold = 127) {
43
+ currentBrush = paintMask ?? currentBrush
43
44
 
44
45
  _scale = scale ?? _scale
45
46
  _color = color ?? _color
46
47
 
47
48
  updateBuffer(
48
- currentMask.w * _scale + 2 * _scale,
49
- currentMask.h * _scale + 2 * _scale,
49
+ currentBrush.w * _scale + 2 * _scale,
50
+ currentBrush.h * _scale + 2 * _scale,
50
51
  )
51
52
 
52
- if (currentMask.type === MaskType.BINARY) {
53
- if (currentMask.outlineType === PaintMaskOutline.CIRCLE) {
54
- outline = makeCircleBinaryMaskOutline(currentMask.w, _scale)
55
- } else if (currentMask.outlineType === PaintMaskOutline.RECT) {
56
- outline = makeRectBinaryMaskOutline(currentMask.w, currentMask.h, _scale)
57
- } else if (currentMask.outlineType === PaintMaskOutline.MASKED) {
58
- outline = makeBinaryMaskOutline(currentMask, _scale)
53
+ if (currentBrush.type === MaskType.BINARY) {
54
+ if (currentBrush.outlineType === PaintMaskOutline.CIRCLE) {
55
+ outline = makeCircleBinaryMaskOutline(currentBrush.w, _scale)
56
+ } else if (currentBrush.outlineType === PaintMaskOutline.RECT) {
57
+ outline = makeRectBinaryMaskOutline(currentBrush.w, currentBrush.h, _scale)
58
+ } else if (currentBrush.outlineType === PaintMaskOutline.MASKED) {
59
+ outline = makeBinaryMaskOutline(currentBrush, _scale)
59
60
  }
60
- } else if (currentMask.type === MaskType.ALPHA) {
61
- const mask = makeBinaryMaskFromAlphaMask(currentMask, alphaThreshold)
61
+ } else if (currentBrush.type === MaskType.ALPHA) {
62
+ const mask = makeBinaryMaskFromAlphaMask(currentBrush, alphaThreshold)
62
63
  outline = makeBinaryMaskOutline(mask, _scale)
64
+ } else {
65
+ outline = makeRectBinaryMaskOutline(currentBrush.w, currentBrush.h, _scale)
63
66
  }
64
67
 
65
68
  const pixelData = getPixelData(outline.w, outline.h)
@@ -70,10 +73,10 @@ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenC
70
73
  const boundsScratch = { x: 0, y: 0, w: 0, h: 0 }
71
74
 
72
75
  function getBounds(centerX: number, centerY: number): Rect {
73
- boundsScratch.x = centerX + currentMask.centerOffsetX
74
- boundsScratch.y = centerY + currentMask.centerOffsetY
75
- boundsScratch.w = currentMask.w
76
- boundsScratch.h = currentMask.h
76
+ boundsScratch.x = centerX + currentBrush.centerOffsetX
77
+ boundsScratch.y = centerY + currentBrush.centerOffsetY
78
+ boundsScratch.w = currentBrush.w
79
+ boundsScratch.h = currentBrush.h
77
80
 
78
81
  return boundsScratch
79
82
  }
@@ -81,10 +84,10 @@ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenC
81
84
  const boundsScaledScratch = { x: 0, y: 0, w: 0, h: 0 }
82
85
 
83
86
  function getOutlineBoundsScaled(centerX: number, centerY: number): Rect {
84
- boundsScaledScratch.x = centerX * _scale + currentMask.centerOffsetX * _scale - 1
85
- boundsScaledScratch.y = centerY * _scale + currentMask.centerOffsetY * _scale - 1
86
- boundsScaledScratch.w = currentMask.w * _scale
87
- boundsScaledScratch.h = currentMask.h * _scale
87
+ boundsScaledScratch.x = centerX * _scale + currentBrush.centerOffsetX * _scale - 1
88
+ boundsScaledScratch.y = centerY * _scale + currentBrush.centerOffsetY * _scale - 1
89
+ boundsScaledScratch.w = currentBrush.w * _scale
90
+ boundsScaledScratch.h = currentBrush.h * _scale
88
91
 
89
92
  return boundsScaledScratch
90
93
  }
@@ -94,8 +97,8 @@ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenC
94
97
  centerX: number,
95
98
  centerY: number,
96
99
  ) {
97
- const dx = centerX * _scale + currentMask.centerOffsetX * _scale - 1
98
- const dy = centerY * _scale + currentMask.centerOffsetY * _scale - 1
100
+ const dx = centerX * _scale + currentBrush.centerOffsetX * _scale - 1
101
+ const dy = centerY * _scale + currentBrush.centerOffsetY * _scale - 1
99
102
 
100
103
  drawCtx.drawImage(canvas, Math.floor(dx), Math.floor(dy))
101
104
  }
@@ -104,7 +107,7 @@ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenC
104
107
  return {
105
108
  color: _color,
106
109
  scale: _scale,
107
- currentMask,
110
+ currentBrush,
108
111
  }
109
112
  }
110
113
 
@@ -20,3 +20,14 @@ export interface PaintBinaryMask<T extends PaintMaskOutline = PaintMaskOutline>
20
20
 
21
21
  export type PaintMask = PaintAlphaMask<any> | PaintBinaryMask<any>
22
22
 
23
+ export type PaintBrush = PaintMask | PaintRect
24
+
25
+ export interface PaintRect {
26
+ type: null,
27
+ readonly outlineType: PaintMaskOutline.RECT
28
+ w: number,
29
+ h: number,
30
+ centerOffsetX: number,
31
+ centerOffsetY: number,
32
+ data: null,
33
+ }
package/src/index.ts CHANGED
@@ -126,6 +126,7 @@ export * from './Paint/eachTileInBounds'
126
126
  export * from './Paint/makeCirclePaintMask'
127
127
  export * from './Paint/makePaintMask'
128
128
  export * from './Paint/makeRectFalloffPaintAlphaMask'
129
+ export * from './Paint/PaintRect'
129
130
 
130
131
  export * from './Paint/Render/AlphaMaskPaintBufferCanvasRenderer'
131
132
  export * from './Paint/Render/BinaryMaskPaintBufferCanvasRenderer'