pixel-data-js 0.36.0 → 0.37.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.
Files changed (72) hide show
  1. package/dist/index.prod.cjs +372 -303
  2. package/dist/index.prod.cjs.map +1 -1
  3. package/dist/index.prod.d.ts +109 -67
  4. package/dist/index.prod.js +368 -302
  5. package/dist/index.prod.js.map +1 -1
  6. package/package.json +2 -2
  7. package/src/Algorithm/floodFillSelection.ts +3 -2
  8. package/src/BlendModes/blend-modes-fast.ts +2 -1
  9. package/src/BlendModes/blend-modes-perfect.ts +2 -1
  10. package/src/Canvas/ReusableCanvas.ts +0 -5
  11. package/src/Color/_color-types.ts +8 -0
  12. package/src/Color/colorDistance.ts +9 -0
  13. package/src/Color/convert-color.ts +43 -0
  14. package/src/Color/lerpColor32.ts +44 -0
  15. package/src/Color/pack-color.ts +38 -0
  16. package/src/History/HistoryAction.ts +2 -2
  17. package/src/History/PixelAccumulator.ts +13 -15
  18. package/src/History/PixelMutator/mutatorBlendColor.ts +2 -1
  19. package/src/History/PixelMutator/mutatorBlendColorPaintAlphaMask.ts +1 -1
  20. package/src/History/PixelMutator/mutatorBlendColorPaintBinaryMask.ts +1 -1
  21. package/src/History/PixelMutator/mutatorBlendColorPaintMask.ts +23 -8
  22. package/src/History/PixelMutator/mutatorBlendColorPaintRect.ts +2 -1
  23. package/src/History/PixelMutator/mutatorBlendPixel.ts +2 -1
  24. package/src/History/PixelMutator/mutatorClear.ts +1 -1
  25. package/src/History/PixelMutator/mutatorFill.ts +1 -1
  26. package/src/History/PixelMutator/mutatorFillBinaryMask.ts +1 -1
  27. package/src/History/PixelWriter.ts +5 -5
  28. package/src/IndexedImage/IndexedImage.ts +1 -1
  29. package/src/IndexedImage/indexedImageToAverageColor.ts +3 -2
  30. package/src/Mask/_mask-types.ts +9 -0
  31. package/src/Paint/AlphaMaskPaintBuffer.ts +26 -26
  32. package/src/Paint/BinaryMaskPaintBuffer.ts +19 -19
  33. package/src/Paint/ColorPaintBuffer.ts +40 -42
  34. package/src/Paint/Commit/AlphaMaskPaintBufferCommitter.ts +1 -1
  35. package/src/Paint/Commit/AlphaMaskPaintBufferManager.ts +6 -7
  36. package/src/Paint/Commit/BinaryMaskPaintBufferCommitter.ts +1 -1
  37. package/src/Paint/Commit/BinaryMaskPaintBufferManager.ts +6 -7
  38. package/src/Paint/Commit/ColorPaintBufferManager.ts +6 -7
  39. package/src/Paint/Commit/commitColorPaintBuffer.ts +2 -6
  40. package/src/Paint/Commit/commitMaskPaintBuffer.ts +3 -7
  41. package/src/Paint/Render/AlphaMaskPaintBufferCanvasRenderer.ts +42 -25
  42. package/src/Paint/Render/BinaryMaskPaintBufferCanvasRenderer.ts +40 -24
  43. package/src/Paint/Render/ColorPaintBufferCanvasRenderer.ts +21 -21
  44. package/src/Paint/Render/PaintCursorRenderer.ts +3 -2
  45. package/src/Paint/eachTileInBounds.ts +9 -10
  46. package/src/PixelData/blendColorPixelData.ts +2 -1
  47. package/src/PixelData/blendColorPixelDataAlphaMask.ts +2 -1
  48. package/src/PixelData/blendColorPixelDataBinaryMask.ts +2 -1
  49. package/src/PixelData/blendColorPixelDataMask.ts +2 -1
  50. package/src/PixelData/blendColorPixelDataPaintAlphaMask.ts +1 -1
  51. package/src/PixelData/blendColorPixelDataPaintBinaryMask.ts +1 -1
  52. package/src/PixelData/blendColorPixelDataPaintMask.ts +19 -8
  53. package/src/PixelData/blendPixel.ts +2 -1
  54. package/src/PixelData/blendPixelData.ts +2 -1
  55. package/src/PixelData/blendPixelDataAlphaMask.ts +2 -1
  56. package/src/PixelData/blendPixelDataBinaryMask.ts +2 -1
  57. package/src/PixelData/blendPixelDataPaintBuffer.ts +2 -3
  58. package/src/PixelData/clearPixelDataFast.ts +1 -1
  59. package/src/PixelData/fillPixelData.ts +1 -1
  60. package/src/PixelData/fillPixelDataBinaryMask.ts +1 -1
  61. package/src/PixelData/fillPixelDataFast.ts +1 -1
  62. package/src/PixelData/writePaintBufferToPixelData.ts +1 -5
  63. package/src/Tile/MaskTile.ts +4 -0
  64. package/src/Tile/PixelTile.ts +2 -0
  65. package/src/Tile/TilePool.ts +9 -8
  66. package/src/Tile/TileTargetConfig.ts +27 -0
  67. package/src/Tile/_tile-types.ts +16 -0
  68. package/src/_types.ts +1 -6
  69. package/src/index.ts +7 -3
  70. package/src/History/PixelEngineConfig.ts +0 -28
  71. package/src/Internal/_constants.ts +0 -3
  72. package/src/color.ts +0 -112
@@ -1,9 +1,9 @@
1
1
  import { forEachLinePoint } from '../Algorithm/forEachLinePoint'
2
- import type { PixelEngineConfig } from '../History/PixelEngineConfig'
3
2
  import type { Rect } from '../Rect/_rect-types'
4
3
  import { trimRectBounds } from '../Rect/trimRectBounds'
5
- import type { BinaryMaskTile } from '../Tile/_tile-types'
6
- import type { TilePool } from '../Tile/TilePool'
4
+ import type { BinaryMaskTile, TileTargetMeta } from '../Tile/_tile-types'
5
+ import { makeBinaryMaskTile } from '../Tile/MaskTile'
6
+ import { TilePool } from '../Tile/TilePool'
7
7
  import type { PaintBinaryMask, PaintRect } from './_paint-types'
8
8
  import { eachTileInBounds } from './eachTileInBounds'
9
9
 
@@ -16,8 +16,8 @@ export class BinaryMaskPaintBuffer {
16
16
  private eachTileInBoundsFn = eachTileInBounds
17
17
 
18
18
  constructor(
19
- readonly config: PixelEngineConfig,
20
- readonly tilePool: TilePool<BinaryMaskTile>,
19
+ readonly config: TileTargetMeta,
20
+ readonly tilePool: TilePool<BinaryMaskTile> = new TilePool(config.tileSize, makeBinaryMaskTile),
21
21
  ) {
22
22
  this.lookup = []
23
23
  }
@@ -45,9 +45,9 @@ export class BinaryMaskPaintBuffer {
45
45
  const lookup = this.lookup
46
46
  const tilePool = this.tilePool
47
47
  const config = this.config
48
- const tileShift = config.tileShift
49
- const tileMask = config.tileMask
50
- const target = config.target
48
+ const targetW = config.targetWidth
49
+ const targetH = config.targetHeight
50
+ const tileSize = config.tileSize
51
51
 
52
52
  const { w: bW, h: bH, data: bD, centerOffsetX, centerOffsetY } = brush
53
53
  let changed = false
@@ -64,8 +64,8 @@ export class BinaryMaskPaintBuffer {
64
64
  topLeftY,
65
65
  bW,
66
66
  bH,
67
- target.w,
68
- target.h,
67
+ targetW,
68
+ targetH,
69
69
  scratch,
70
70
  )
71
71
 
@@ -78,8 +78,8 @@ export class BinaryMaskPaintBuffer {
78
78
  for (let i = 0; i < bH_t; i++) {
79
79
  const canvasY = bY + i
80
80
  const bOff = (canvasY - topLeftY) * bW
81
- const tOff = (canvasY & tileMask) << tileShift
82
- const dS = tOff + (bX & tileMask)
81
+ const tOff = (canvasY - tile.y) * tileSize
82
+ const dS = tOff + (bX - tile.x)
83
83
 
84
84
  for (let j = 0; j < bW_t; j++) {
85
85
  const canvasX = bX + j
@@ -125,9 +125,9 @@ export class BinaryMaskPaintBuffer {
125
125
  const lookup = this.lookup
126
126
  const tilePool = this.tilePool
127
127
  const config = this.config
128
- const tileShift = config.tileShift
129
- const tileMask = config.tileMask
130
- const target = config.target
128
+ const targetW = config.targetWidth
129
+ const targetH = config.targetHeight
130
+ const tileSize = config.tileSize
131
131
 
132
132
  const brushWidth = brush.w
133
133
  const brushHeight = brush.h
@@ -152,8 +152,8 @@ export class BinaryMaskPaintBuffer {
152
152
  topLeftY,
153
153
  brushWidth,
154
154
  brushHeight,
155
- target.w,
156
- target.h,
155
+ targetW,
156
+ targetH,
157
157
  scratch,
158
158
  )
159
159
 
@@ -165,8 +165,8 @@ export class BinaryMaskPaintBuffer {
165
165
 
166
166
  for (let i = 0; i < bH_t; i++) {
167
167
  const canvasY = bY + i
168
- const tOff = (canvasY & tileMask) << tileShift
169
- const dS = tOff + (bX & tileMask)
168
+ const tOff = (canvasY - tile.y) * tileSize
169
+ const dS = tOff + (bX - tile.x)
170
170
 
171
171
  for (let j = 0; j < bW_t; j++) {
172
172
  const idx = dS + j
@@ -1,9 +1,8 @@
1
- import type { Color32 } from '../_types'
2
1
  import { forEachLinePoint } from '../Algorithm/forEachLinePoint'
3
- import type { PixelEngineConfig } from '../History/PixelEngineConfig'
2
+ import type { Color32 } from '../Color/_color-types'
4
3
  import type { Rect } from '../Rect/_rect-types'
5
4
  import { trimRectBounds } from '../Rect/trimRectBounds'
6
- import type { PixelTile } from '../Tile/_tile-types'
5
+ import type { PixelTile, TileTargetMeta } from '../Tile/_tile-types'
7
6
  import type { TilePool } from '../Tile/TilePool'
8
7
  import type { PaintAlphaMask, PaintBinaryMask, PaintRect } from './_paint-types'
9
8
  import { eachTileInBounds } from './eachTileInBounds'
@@ -13,7 +12,7 @@ export class ColorPaintBuffer {
13
12
  private readonly scratchBounds: Rect = { x: 0, y: 0, w: 0, h: 0 }
14
13
 
15
14
  constructor(
16
- readonly config: PixelEngineConfig,
15
+ readonly config: TileTargetMeta,
17
16
  readonly tilePool: TilePool<PixelTile>,
18
17
  ) {
19
18
  this.lookup = []
@@ -48,9 +47,9 @@ export class ColorPaintBuffer {
48
47
  const lookup = this.lookup
49
48
  const tilePool = this.tilePool
50
49
  const config = this.config
51
- const tileShift = config.tileShift
52
- const tileMask = config.tileMask
53
- const target = config.target
50
+ const tileSize = config.tileSize
51
+ const targetW = config.targetWidth
52
+ const targetH = config.targetHeight
54
53
 
55
54
  const { w: bW, h: bH, data: bD, centerOffsetX, centerOffsetY } = brush
56
55
  const cRGB = color & 0x00ffffff
@@ -66,8 +65,8 @@ export class ColorPaintBuffer {
66
65
  topLeftY,
67
66
  bW,
68
67
  bH,
69
- target.w,
70
- target.h,
68
+ targetW,
69
+ targetH,
71
70
  scratch,
72
71
  )
73
72
 
@@ -80,8 +79,8 @@ export class ColorPaintBuffer {
80
79
  for (let i = 0; i < bH_t; i++) {
81
80
  const canvasY = bY + i
82
81
  const bOff = (canvasY - topLeftY) * bW
83
- const tOff = (canvasY & tileMask) << tileShift
84
- const dS = tOff + (bX & tileMask)
82
+ const tOff = (canvasY - tile.y) * tileSize
83
+ const dS = tOff + (bX - tile.x)
85
84
 
86
85
  for (let j = 0; j < bW_t; j++) {
87
86
  const canvasX = bX + j
@@ -137,9 +136,9 @@ export class ColorPaintBuffer {
137
136
  const lookup = this.lookup
138
137
  const tilePool = this.tilePool
139
138
  const config = this.config
140
- const tileShift = config.tileShift
141
- const tileMask = config.tileMask
142
- const target = config.target
139
+ const tileSize = config.tileSize
140
+ const targetW = config.targetWidth
141
+ const targetH = config.targetHeight
143
142
 
144
143
  const { w: bW, h: bH, data: bD, centerOffsetX, centerOffsetY } = brush
145
144
  let changed = false
@@ -153,8 +152,8 @@ export class ColorPaintBuffer {
153
152
  topLeftY,
154
153
  bW,
155
154
  bH,
156
- target.w,
157
- target.h,
155
+ targetW,
156
+ targetH,
158
157
  scratch,
159
158
  )
160
159
 
@@ -167,8 +166,9 @@ export class ColorPaintBuffer {
167
166
  for (let i = 0; i < bH_t; i++) {
168
167
  const canvasY = bY + i
169
168
  const bOff = (canvasY - topLeftY) * bW
170
- const tOff = (canvasY & tileMask) << tileShift
171
- const dS = tOff + (bX & tileMask)
169
+
170
+ const tOff = (canvasY - tile.y) * tileSize
171
+ const dS = tOff + (bX - tile.x)
172
172
 
173
173
  for (let j = 0; j < bW_t; j++) {
174
174
  const canvasX = bX + j
@@ -217,9 +217,9 @@ export class ColorPaintBuffer {
217
217
  const lookup = this.lookup
218
218
  const tilePool = this.tilePool
219
219
  const config = this.config
220
- const tileShift = config.tileShift
221
- const tileMask = config.tileMask
222
- const target = config.target
220
+ const targetW = config.targetWidth
221
+ const targetH = config.targetHeight
222
+ const tileSize = config.tileSize
223
223
 
224
224
  const brushWidth = brush.w
225
225
  const brushHeight = brush.h
@@ -242,39 +242,37 @@ export class ColorPaintBuffer {
242
242
  topLeftY,
243
243
  brushWidth,
244
244
  brushHeight,
245
- target.w,
246
- target.h,
245
+ targetW,
246
+ targetH,
247
247
  scratch,
248
248
  )
249
249
 
250
250
  if (scratch.w <= 0 || scratch.h <= 0) return
251
251
 
252
252
  eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
253
- const d32 = tile.data
254
- let tileChanged = false
253
+ const d32 = tile.data
254
+ let tileChanged = false
255
255
 
256
- for (let i = 0; i < bH_t; i++) {
257
- const canvasY = bY + i
258
- const tOff = (canvasY & tileMask) << tileShift
259
- const dS = tOff + (bX & tileMask)
256
+ for (let i = 0; i < bH_t; i++) {
257
+ const canvasY = bY + i
258
+ const tOff = (canvasY - tile.y) * tileSize
259
+ const dS = tOff + (bX - tile.x)
260
260
 
261
- for (let j = 0; j < bW_t; j++) {
262
- const idx = dS + j
261
+ for (let j = 0; j < bW_t; j++) {
262
+ const idx = dS + j
263
263
 
264
- if (d32[idx] !== color) {
265
- d32[idx] = color
266
- tileChanged = true
267
- }
264
+ if (d32[idx] !== color) {
265
+ d32[idx] = color
266
+ tileChanged = true
268
267
  }
269
268
  }
269
+ }
270
270
 
271
- if (tileChanged) {
272
- changed = true
273
- }
274
- },
275
- )
276
- },
277
- )
271
+ if (tileChanged) {
272
+ changed = true
273
+ }
274
+ })
275
+ })
278
276
 
279
277
  return changed
280
278
  }
@@ -1,5 +1,5 @@
1
- import type { Color32 } from '../../_types'
2
1
  import { sourceOverPerfect } from '../../BlendModes/blend-modes-perfect'
2
+ import type { Color32 } from '../../Color/_color-types'
3
3
  import type { PixelAccumulator } from '../../History/PixelAccumulator'
4
4
  import { blendColorPixelDataAlphaMask } from '../../PixelData/blendColorPixelDataAlphaMask'
5
5
  import type { AlphaMaskPaintBuffer } from '../AlphaMaskPaintBuffer'
@@ -1,6 +1,5 @@
1
- import type { CanvasObjectFactory } from '../../Canvas/_canvas-types'
1
+ import type { ReusableCanvasFactory } from '../../Canvas/_canvas-types'
2
2
  import type { PixelWriter } from '../../History/PixelWriter'
3
- import { DEFAULT_CANVAS_FACTORY } from '../../Internal/_constants'
4
3
  import { makeAlphaMaskTile } from '../../Tile/MaskTile'
5
4
  import { TilePool } from '../../Tile/TilePool'
6
5
  import { AlphaMaskPaintBuffer } from '../AlphaMaskPaintBuffer'
@@ -11,17 +10,17 @@ export type AlphaMaskPaintBufferManager =
11
10
  Pick<AlphaMaskPaintBuffer, 'paintAlphaMask' | 'paintBinaryMask' | 'paintRect'>
12
11
  & {
13
12
  commit: ReturnType<typeof makeAlphaMaskPaintBufferCommitter>
14
- draw: ReturnType<typeof makeAlphaMaskPaintBufferCanvasRenderer>
13
+ renderer: ReturnType<typeof makeAlphaMaskPaintBufferCanvasRenderer>
15
14
  clear: () => void
16
15
  }
17
16
 
18
17
  export function makeAlphaMaskPaintBufferManager(
19
18
  writer: Pick<PixelWriter<any>, 'accumulator' | 'config'>,
20
- canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
19
+ reusableCanvasFactory?: () => ReusableCanvasFactory<any>,
21
20
  ): AlphaMaskPaintBufferManager {
22
- const pool = new TilePool(writer.config, makeAlphaMaskTile)
21
+ const pool = new TilePool(writer.config.tileSize, makeAlphaMaskTile)
23
22
  const buffer = new AlphaMaskPaintBuffer(writer.config, pool)
24
- const draw = makeAlphaMaskPaintBufferCanvasRenderer(buffer, canvasFactory)
23
+ const renderer = makeAlphaMaskPaintBufferCanvasRenderer(buffer, reusableCanvasFactory)
25
24
 
26
25
  return {
27
26
  clear: buffer.clear.bind(buffer),
@@ -29,6 +28,6 @@ export function makeAlphaMaskPaintBufferManager(
29
28
  paintAlphaMask: buffer.paintAlphaMask.bind(buffer),
30
29
  paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
31
30
  commit: makeAlphaMaskPaintBufferCommitter(writer.accumulator, buffer),
32
- draw,
31
+ renderer,
33
32
  }
34
33
  }
@@ -1,5 +1,5 @@
1
- import type { Color32 } from '../../_types'
2
1
  import { sourceOverPerfect } from '../../BlendModes/blend-modes-perfect'
2
+ import type { Color32 } from '../../Color/_color-types'
3
3
  import type { PixelAccumulator } from '../../History/PixelAccumulator'
4
4
  import { blendColorPixelDataBinaryMask } from '../../PixelData/blendColorPixelDataBinaryMask'
5
5
  import type { BinaryMaskPaintBuffer } from '../BinaryMaskPaintBuffer'
@@ -1,6 +1,5 @@
1
- import type { CanvasObjectFactory } from '../../Canvas/_canvas-types'
1
+ import type { ReusableCanvasFactory } from '../../Canvas/_canvas-types'
2
2
  import type { PixelWriter } from '../../History/PixelWriter'
3
- import { DEFAULT_CANVAS_FACTORY } from '../../Internal/_constants'
4
3
  import { makeBinaryMaskTile } from '../../Tile/MaskTile'
5
4
  import { TilePool } from '../../Tile/TilePool'
6
5
  import { BinaryMaskPaintBuffer } from '../BinaryMaskPaintBuffer'
@@ -9,23 +8,23 @@ import { makeBinaryMaskPaintBufferCommitter } from './BinaryMaskPaintBufferCommi
9
8
 
10
9
  export type BinaryMaskPaintBufferManager = Pick<BinaryMaskPaintBuffer, 'paintBinaryMask' | 'paintRect'> & {
11
10
  commit: ReturnType<typeof makeBinaryMaskPaintBufferCommitter>
12
- draw: ReturnType<typeof makeBinaryMaskPaintBufferCanvasRenderer>
11
+ renderer: ReturnType<typeof makeBinaryMaskPaintBufferCanvasRenderer>
13
12
  clear: () => void
14
13
  }
15
14
 
16
15
  export function makeBinaryMaskPaintBufferManager(
17
16
  writer: Pick<PixelWriter<any>, 'accumulator' | 'config'>,
18
- canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
17
+ reusableCanvasFactory?: () => ReusableCanvasFactory<any>,
19
18
  ): BinaryMaskPaintBufferManager {
20
- const pool = new TilePool(writer.config, makeBinaryMaskTile)
19
+ const pool = new TilePool(writer.config.tileSize, makeBinaryMaskTile)
21
20
  const buffer = new BinaryMaskPaintBuffer(writer.config, pool)
22
- const draw = makeBinaryMaskPaintBufferCanvasRenderer(buffer, canvasFactory)
21
+ const renderer = makeBinaryMaskPaintBufferCanvasRenderer(buffer, reusableCanvasFactory)
23
22
 
24
23
  return {
25
24
  clear: buffer.clear.bind(buffer),
26
25
  paintRect: buffer.paintRect.bind(buffer),
27
26
  paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
28
27
  commit: makeBinaryMaskPaintBufferCommitter(writer.accumulator, buffer),
29
- draw,
28
+ renderer,
30
29
  }
31
30
  }
@@ -1,6 +1,5 @@
1
- import type { CanvasObjectFactory } from '../../Canvas/_canvas-types'
1
+ import type { ReusableCanvasFactory } from '../../Canvas/_canvas-types'
2
2
  import type { PixelWriter } from '../../History/PixelWriter'
3
- import { DEFAULT_CANVAS_FACTORY } from '../../Internal/_constants'
4
3
  import { makePixelTile } from '../../Tile/PixelTile'
5
4
  import { TilePool } from '../../Tile/TilePool'
6
5
  import { ColorPaintBuffer } from '../ColorPaintBuffer'
@@ -11,17 +10,17 @@ export type ColorPaintBufferManager =
11
10
  Pick<ColorPaintBuffer, 'paintAlphaMask' | 'paintBinaryMask' | 'paintRect'>
12
11
  & {
13
12
  commit: ReturnType<typeof makeColorPaintBufferCommitter>
14
- draw: ReturnType<typeof makeColorPaintBufferCanvasRenderer>
13
+ renderer: ReturnType<typeof makeColorPaintBufferCanvasRenderer>
15
14
  clear: () => void
16
15
  }
17
16
 
18
17
  export function makeColorPaintBufferManager(
19
18
  writer: Pick<PixelWriter<any>, 'accumulator' | 'config'>,
20
- canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
19
+ reusableCanvasFactory?: () => ReusableCanvasFactory<any>,
21
20
  ): ColorPaintBufferManager {
22
- const pool = new TilePool(writer.config, makePixelTile)
21
+ const pool = new TilePool(writer.config.tileSize, makePixelTile)
23
22
  const buffer = new ColorPaintBuffer(writer.config, pool)
24
- const draw = makeColorPaintBufferCanvasRenderer(buffer, canvasFactory)
23
+ const renderer = makeColorPaintBufferCanvasRenderer(buffer, reusableCanvasFactory)
25
24
 
26
25
  return {
27
26
  clear: buffer.clear.bind(buffer),
@@ -29,6 +28,6 @@ export function makeColorPaintBufferManager(
29
28
  paintAlphaMask: buffer.paintAlphaMask.bind(buffer),
30
29
  paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
31
30
  commit: makeColorPaintBufferCommitter(writer.accumulator, buffer),
32
- draw,
31
+ renderer,
33
32
  }
34
33
  }
@@ -20,7 +20,6 @@ export function commitColorPaintBuffer(
20
20
  blendPixelDataFn = blendPixelData,
21
21
  ) {
22
22
  const config = accumulator.config
23
- const tileShift = config.tileShift
24
23
  const lookup = paintBuffer.lookup
25
24
 
26
25
  SCRATCH_OPTS.alpha = alpha
@@ -32,11 +31,8 @@ export function commitColorPaintBuffer(
32
31
  if (tile) {
33
32
  const didChange = accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty)
34
33
 
35
- const dx = tile.tx << tileShift
36
- const dy = tile.ty << tileShift
37
-
38
- SCRATCH_OPTS.x = dx
39
- SCRATCH_OPTS.y = dy
34
+ SCRATCH_OPTS.x = tile.x
35
+ SCRATCH_OPTS.y = tile.y
40
36
  SCRATCH_OPTS.w = tile.w
41
37
  SCRATCH_OPTS.h = tile.h
42
38
 
@@ -1,5 +1,5 @@
1
- import { type Color32 } from '../../_types'
2
1
  import { sourceOverPerfect } from '../../BlendModes/blend-modes-perfect'
2
+ import type { Color32 } from '../../Color/_color-types'
3
3
  import type { PixelAccumulator } from '../../History/PixelAccumulator'
4
4
  import { blendColorPixelDataAlphaMask } from '../../PixelData/blendColorPixelDataAlphaMask'
5
5
  import { blendColorPixelDataBinaryMask } from '../../PixelData/blendColorPixelDataBinaryMask'
@@ -42,7 +42,6 @@ export function commitMaskPaintBuffer(
42
42
  blendColorPixelDataMaskFn: any,
43
43
  ) {
44
44
  const config = accumulator.config
45
- const tileShift = config.tileShift
46
45
  const lookup = paintBuffer.lookup
47
46
 
48
47
  SCRATCH_OPTS.alpha = alpha
@@ -54,11 +53,8 @@ export function commitMaskPaintBuffer(
54
53
  if (tile) {
55
54
  const didChange = accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty)
56
55
 
57
- const dx = tile.tx << tileShift
58
- const dy = tile.ty << tileShift
59
-
60
- SCRATCH_OPTS.x = dx
61
- SCRATCH_OPTS.y = dy
56
+ SCRATCH_OPTS.x = tile.x
57
+ SCRATCH_OPTS.y = tile.y
62
58
  SCRATCH_OPTS.w = tile.w
63
59
  SCRATCH_OPTS.h = tile.h
64
60
 
@@ -1,31 +1,46 @@
1
- import type { Color32 } from '../../_types'
2
- import type { CanvasObjectFactory } from '../../Canvas/_canvas-types'
3
- import { DEFAULT_CANVAS_FACTORY } from '../../Internal/_constants'
4
- import { CANVAS_CTX_FAILED } from '../../Internal/_errors'
5
- import { makePixelData } from '../../PixelData/PixelData'
1
+ import type { ReusableCanvasFactory } from '../../Canvas/_canvas-types'
2
+ import { makeReusableOffscreenCanvas } from '../../Canvas/ReusableCanvas'
3
+ import type { Color32 } from '../../Color/_color-types'
4
+ import type { PixelData } from '../../PixelData/_pixelData-types'
5
+ import { makeReusablePixelData } from '../../PixelData/ReusablePixelData'
6
+ import type { AlphaMaskTile, TileTargetMeta } from '../../Tile/_tile-types'
6
7
  import type { AlphaMaskPaintBuffer } from '../AlphaMaskPaintBuffer'
7
8
 
8
9
  export type AlphaMaskPaintBufferCanvasRenderer = ReturnType<typeof makeAlphaMaskPaintBufferCanvasRenderer>
9
10
 
10
- export function makeAlphaMaskPaintBufferCanvasRenderer(
11
+ export function makeAlphaMaskPaintBufferCanvasRenderer<T extends HTMLCanvasElement | OffscreenCanvas>(
11
12
  paintBuffer: AlphaMaskPaintBuffer,
12
- canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
13
+ reusableCanvasFactory?: () => ReusableCanvasFactory<T>,
13
14
  ) {
14
- const config = paintBuffer.config
15
- const tileSize = config.tileSize
16
- const tileShift = config.tileShift
17
- const tileArea = config.tileArea
18
- const lookup = paintBuffer.lookup
19
-
20
- const canvas = canvasFactory(tileSize, tileSize)
21
- const ctx = canvas.getContext('2d')
22
- if (!ctx) throw new Error(CANVAS_CTX_FAILED)
23
- ctx.imageSmoothingEnabled = false
24
-
25
- const bridge = makePixelData(new ImageData(tileSize, tileSize))
26
- const view32 = bridge.data
15
+ const factory = (reusableCanvasFactory ?? makeReusableOffscreenCanvas) as unknown as () => ReusableCanvasFactory<T>
16
+ const getBuffer = factory()
17
+ const getBridge = makeReusablePixelData()
18
+
19
+ let config: TileTargetMeta
20
+ let tileSize: number
21
+ let tileArea: number
22
+ let lookup: (AlphaMaskTile | undefined)[]
23
+ let view32: Uint32Array
24
+ let bridge: PixelData
25
+ let canvas: HTMLCanvasElement | OffscreenCanvas
26
+ let ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
27
+
28
+ setBuffer(paintBuffer)
29
+
30
+ function setBuffer(value: AlphaMaskPaintBuffer) {
31
+ paintBuffer = value
32
+ config = paintBuffer.config
33
+ tileSize = config.tileSize
34
+ tileArea = config.tileArea
35
+ lookup = paintBuffer.lookup
36
+ bridge = getBridge(tileSize, tileSize)
37
+ view32 = bridge.data
38
+ const buff = getBuffer(tileSize, tileSize)
39
+ canvas = buff.canvas
40
+ ctx = buff.ctx
41
+ }
27
42
 
28
- return function drawPaintBuffer(
43
+ function draw(
29
44
  targetCtx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
30
45
  color: Color32,
31
46
  alpha = 255,
@@ -64,15 +79,17 @@ export function makeAlphaMaskPaintBufferCanvasRenderer(
64
79
  }
65
80
  }
66
81
 
67
- const dx = tile.tx << tileShift
68
- const dy = tile.ty << tileShift
69
-
70
82
  ctx.putImageData(bridge.imageData, 0, 0)
71
- targetCtx.drawImage(canvas, dx, dy)
83
+ targetCtx.drawImage(canvas, tile.x, tile.y)
72
84
  }
73
85
  }
74
86
 
75
87
  targetCtx.globalAlpha = 1
76
88
  targetCtx.globalCompositeOperation = 'source-over'
77
89
  }
90
+
91
+ return {
92
+ draw,
93
+ setBuffer,
94
+ }
78
95
  }
@@ -1,31 +1,46 @@
1
- import type { Color32 } from '../../_types'
2
- import type { CanvasObjectFactory } from '../../Canvas/_canvas-types'
3
- import { DEFAULT_CANVAS_FACTORY } from '../../Internal/_constants'
4
- import { CANVAS_CTX_FAILED } from '../../Internal/_errors'
5
- import { makePixelData } from '../../PixelData/PixelData'
1
+ import type { ReusableCanvasFactory } from '../../Canvas/_canvas-types'
2
+ import { makeReusableOffscreenCanvas } from '../../Canvas/ReusableCanvas'
3
+ import type { Color32 } from '../../Color/_color-types'
4
+ import type { PixelData } from '../../PixelData/_pixelData-types'
5
+ import { makeReusablePixelData } from '../../PixelData/ReusablePixelData'
6
+ import type { BinaryMaskTile, TileTargetMeta } from '../../Tile/_tile-types'
6
7
  import type { BinaryMaskPaintBuffer } from '../BinaryMaskPaintBuffer'
7
8
 
8
9
  export type BinaryMaskPaintBufferCanvasRenderer = ReturnType<typeof makeBinaryMaskPaintBufferCanvasRenderer>
9
10
 
10
- export function makeBinaryMaskPaintBufferCanvasRenderer(
11
+ export function makeBinaryMaskPaintBufferCanvasRenderer<T extends HTMLCanvasElement | OffscreenCanvas>(
11
12
  paintBuffer: BinaryMaskPaintBuffer,
12
- canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
13
+ reusableCanvasFactory?: () => ReusableCanvasFactory<T>,
13
14
  ) {
14
- const config = paintBuffer.config
15
- const tileSize = config.tileSize
16
- const tileShift = config.tileShift
17
- const tileArea = config.tileArea
18
- const lookup = paintBuffer.lookup
15
+ const factory = (reusableCanvasFactory ?? makeReusableOffscreenCanvas) as unknown as () => ReusableCanvasFactory<T>
16
+ const getBuffer = factory()
17
+ const getBridge = makeReusablePixelData()
19
18
 
20
- const canvas = canvasFactory(tileSize, tileSize)
21
- const ctx = canvas.getContext('2d')
22
- if (!ctx) throw new Error(CANVAS_CTX_FAILED)
23
- ctx.imageSmoothingEnabled = false
19
+ let config: TileTargetMeta
20
+ let tileSize: number
21
+ let tileArea: number
22
+ let lookup: (BinaryMaskTile | undefined)[]
23
+ let view32: Uint32Array
24
+ let bridge: PixelData
25
+ let canvas: HTMLCanvasElement | OffscreenCanvas
26
+ let ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
24
27
 
25
- const bridge = makePixelData(new ImageData(tileSize, tileSize))
26
- const view32 = bridge.data
28
+ setBuffer(paintBuffer)
27
29
 
28
- return function drawPaintBuffer(
30
+ function setBuffer(value: BinaryMaskPaintBuffer) {
31
+ paintBuffer = value
32
+ config = paintBuffer.config
33
+ tileSize = config.tileSize
34
+ tileArea = config.tileArea
35
+ lookup = paintBuffer.lookup
36
+ bridge = getBridge(tileSize, tileSize)
37
+ view32 = bridge.data
38
+ const buff = getBuffer(tileSize, tileSize)
39
+ canvas = buff.canvas
40
+ ctx = buff.ctx
41
+ }
42
+
43
+ function draw(
29
44
  targetCtx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
30
45
  color: Color32,
31
46
  alpha = 255,
@@ -47,21 +62,22 @@ export function makeBinaryMaskPaintBufferCanvasRenderer(
47
62
  view32.fill(0)
48
63
 
49
64
  for (let p = 0; p < tileArea; p++) {
50
- // If mask is solid, the final pixel is just the unmodified color
51
65
  if (data8[p] === 1) {
52
66
  view32[p] = color
53
67
  }
54
68
  }
55
69
 
56
- const dx = tile.tx << tileShift
57
- const dy = tile.ty << tileShift
58
-
59
70
  ctx.putImageData(bridge.imageData, 0, 0)
60
- targetCtx.drawImage(canvas, dx, dy)
71
+ targetCtx.drawImage(canvas, tile.x, tile.y)
61
72
  }
62
73
  }
63
74
 
64
75
  targetCtx.globalAlpha = 1
65
76
  targetCtx.globalCompositeOperation = 'source-over'
66
77
  }
78
+
79
+ return {
80
+ draw,
81
+ setBuffer,
82
+ }
67
83
  }