pixel-data-js 0.28.0 → 0.30.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.
@@ -1,13 +1,15 @@
1
- import type { Color32 } from '../_types'
2
- import { CANVAS_CTX_FAILED } from '../Internal/_errors'
3
- import { makePixelData } from '../PixelData/PixelData'
4
- import type { BinaryMaskPaintBuffer } from './BinaryMaskPaintBuffer'
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'
6
+ import type { BinaryMaskPaintBuffer } from '../BinaryMaskPaintBuffer'
5
7
 
6
8
  export type BinaryMaskPaintBufferCanvasRenderer = ReturnType<typeof makeBinaryMaskPaintBufferCanvasRenderer>
7
9
 
8
10
  export function makeBinaryMaskPaintBufferCanvasRenderer(
9
11
  paintBuffer: BinaryMaskPaintBuffer,
10
- offscreenCanvasClass = OffscreenCanvas,
12
+ canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
11
13
  ) {
12
14
  const config = paintBuffer.config
13
15
  const tileSize = config.tileSize
@@ -15,18 +17,16 @@ export function makeBinaryMaskPaintBufferCanvasRenderer(
15
17
  const tileArea = config.tileArea
16
18
  const lookup = paintBuffer.lookup
17
19
 
18
- const canvas = new offscreenCanvasClass(tileSize, tileSize)
20
+ const canvas = canvasFactory(tileSize, tileSize)
19
21
  const ctx = canvas.getContext('2d')
20
-
21
22
  if (!ctx) throw new Error(CANVAS_CTX_FAILED)
22
-
23
23
  ctx.imageSmoothingEnabled = false
24
24
 
25
25
  const bridge = makePixelData(new ImageData(tileSize, tileSize))
26
26
  const view32 = bridge.data
27
27
 
28
28
  return function drawPaintBuffer(
29
- targetCtx: CanvasRenderingContext2D,
29
+ targetCtx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
30
30
  color: Color32,
31
31
  alpha = 255,
32
32
  compOperation: GlobalCompositeOperation = 'source-over',
@@ -1,28 +1,26 @@
1
- import { CANVAS_CTX_FAILED } from '../Internal/_errors'
2
- import type { ColorPaintBuffer } from './ColorPaintBuffer'
1
+ import type { CanvasObjectFactory } from '../../Canvas/_canvas-types'
2
+ import { DEFAULT_CANVAS_FACTORY } from '../../Internal/_constants'
3
+ import { CANVAS_CTX_FAILED } from '../../Internal/_errors'
4
+ import type { ColorPaintBuffer } from '../ColorPaintBuffer'
3
5
 
4
6
  export type ColorPaintBufferCanvasRenderer = ReturnType<typeof makeColorPaintBufferCanvasRenderer>
5
7
 
6
- /**
7
- *
8
- * @param paintBuffer
9
- * @param offscreenCanvasClass - @internal
10
- */
11
8
  export function makeColorPaintBufferCanvasRenderer(
12
9
  paintBuffer: ColorPaintBuffer,
13
- offscreenCanvasClass = OffscreenCanvas,
10
+ canvasFactory: CanvasObjectFactory<any> = DEFAULT_CANVAS_FACTORY,
14
11
  ) {
15
12
  const config = paintBuffer.config
16
13
  const tileSize = config.tileSize
17
14
  const tileShift = config.tileShift
18
15
  const lookup = paintBuffer.lookup
19
- const canvas = new offscreenCanvasClass(tileSize, tileSize)
16
+
17
+ const canvas = canvasFactory(tileSize, tileSize)
20
18
  const ctx = canvas.getContext('2d')
21
19
  if (!ctx) throw new Error(CANVAS_CTX_FAILED)
22
20
  ctx.imageSmoothingEnabled = false
23
21
 
24
22
  return function drawPaintBuffer(
25
- targetCtx: CanvasRenderingContext2D,
23
+ targetCtx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
26
24
  alpha = 255,
27
25
  compOperation: GlobalCompositeOperation = 'source-over',
28
26
  ): void {
@@ -1,27 +1,26 @@
1
- import { type Color32 } from '../_types'
2
- import type { CanvasContext, CanvasObjectFactory } from '../Canvas/_canvas-types'
3
- import { packColor } from '../color'
4
- import { CANVAS_CTX_FAILED } from '../Internal/_errors'
5
- import { _macro_paintRectCenterOffset } from '../Internal/macros'
6
- import { type BinaryMask, MaskType } from '../Mask/_mask-types'
7
- import { makeBinaryMaskFromAlphaMask } from '../Mask/BinaryMask/makeBinaryMaskFromAlphaMask'
8
- import { makeBinaryMaskOutline } from '../Mask/BinaryMask/makeBinaryMaskOutline'
9
- import { makeCircleBinaryMaskOutline } from '../Mask/BinaryMask/makeCircleBinaryMaskOutline'
10
- import { makeRectBinaryMaskOutline } from '../Mask/BinaryMask/makeRectBinaryMaskOutline'
11
- import { fillPixelDataBinaryMask } from '../PixelData/fillPixelDataBinaryMask'
12
- import { makeReusablePixelData } from '../PixelData/ReusablePixelData'
13
- import type { Rect } from '../Rect/_rect-types'
14
- import { type PaintMask, PaintMaskOutline } from './_paint-types'
1
+ import { type Color32 } from '../../_types'
2
+ import type { ReusableCanvasFactory } from '../../Canvas/_canvas-types'
3
+ import { makeReusableOffscreenCanvas } from '../../Canvas/ReusableCanvas'
4
+ import { packColor } from '../../color'
5
+ import { _macro_paintRectCenterOffset } from '../../Internal/macros'
6
+ import { type BinaryMask, MaskType } from '../../Mask/_mask-types'
7
+ import { makeBinaryMaskFromAlphaMask } from '../../Mask/BinaryMask/makeBinaryMaskFromAlphaMask'
8
+ import { makeBinaryMaskOutline } from '../../Mask/BinaryMask/makeBinaryMaskOutline'
9
+ import { makeCircleBinaryMaskOutline } from '../../Mask/BinaryMask/makeCircleBinaryMaskOutline'
10
+ import { makeRectBinaryMaskOutline } from '../../Mask/BinaryMask/makeRectBinaryMaskOutline'
11
+ import { fillPixelDataBinaryMask } from '../../PixelData/fillPixelDataBinaryMask'
12
+ import { makeReusablePixelData } from '../../PixelData/ReusablePixelData'
13
+ import type { Rect } from '../../Rect/_rect-types'
14
+ import { type PaintMask, PaintMaskOutline } from '../_paint-types'
15
15
 
16
16
  export type PaintCursorRenderer = ReturnType<typeof makePaintCursorRenderer>
17
17
 
18
- export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenCanvas>(
19
- factory: CanvasObjectFactory<T> = (w, h) => new OffscreenCanvas(w, h) as T,
18
+ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenCanvas = OffscreenCanvas>(
19
+ reusableCanvasFactory?: () => ReusableCanvasFactory<T>,
20
20
  ) {
21
- const canvas = factory(1, 1)
22
- const ctx = canvas.getContext('2d')! as CanvasContext<T>
23
- if (!ctx) throw new Error(CANVAS_CTX_FAILED)
24
- ctx.imageSmoothingEnabled = false
21
+ const factory = (reusableCanvasFactory ?? makeReusableOffscreenCanvas) as unknown as () => ReusableCanvasFactory<T>
22
+ const updateBuffer = factory()
23
+ const { canvas, ctx } = updateBuffer(1, 1)
25
24
 
26
25
  const getPixelData = makeReusablePixelData()
27
26
 
@@ -45,8 +44,10 @@ export function makePaintCursorRenderer<T extends HTMLCanvasElement | OffscreenC
45
44
  _scale = scale ?? _scale
46
45
  _color = color ?? _color
47
46
 
48
- canvas.width = currentMask.w * _scale + 2 * _scale
49
- canvas.height = currentMask.h * _scale + 2 * _scale
47
+ updateBuffer(
48
+ currentMask.w * _scale + 2 * _scale,
49
+ currentMask.h * _scale + 2 * _scale,
50
+ )
50
51
 
51
52
  if (currentMask.type === MaskType.BINARY) {
52
53
  if (currentMask.outlineType === PaintMaskOutline.CIRCLE) {
package/src/index.ts CHANGED
@@ -106,16 +106,27 @@ export * from './MaskRect/subtractBinaryMaskRects'
106
106
 
107
107
  export * from './Paint/_paint-types'
108
108
  export * from './Paint/AlphaMaskPaintBuffer'
109
- export * from './Paint/AlphaMaskPaintBufferCanvasRenderer'
110
109
  export * from './Paint/BinaryMaskPaintBuffer'
111
- export * from './Paint/BinaryMaskPaintBufferCanvasRenderer'
112
110
  export * from './Paint/ColorPaintBuffer'
113
- export * from './Paint/ColorPaintBufferCanvasRenderer'
111
+
112
+ export * from './Paint/Commit/AlphaMaskPaintBufferCommitter'
113
+ export * from './Paint/Commit/AlphaMaskPaintBufferManager'
114
+ export * from './Paint/Commit/BinaryMaskPaintBufferCommitter'
115
+ export * from './Paint/Commit/BinaryMaskPaintBufferManager'
116
+ export * from './Paint/Commit/ColorPaintBufferCommitter'
117
+ export * from './Paint/Commit/ColorPaintBufferManager'
118
+ export * from './Paint/Commit/commitColorPaintBuffer'
119
+ export * from './Paint/Commit/commitMaskPaintBuffer'
120
+
114
121
  export * from './Paint/eachTileInBounds'
115
122
  export * from './Paint/makeCirclePaintMask'
116
123
  export * from './Paint/makePaintMask'
117
124
  export * from './Paint/makeRectFalloffPaintAlphaMask'
118
- export * from './Paint/PaintCursorRenderer'
125
+
126
+ export * from './Paint/Render/AlphaMaskPaintBufferCanvasRenderer'
127
+ export * from './Paint/Render/BinaryMaskPaintBufferCanvasRenderer'
128
+ export * from './Paint/Render/ColorPaintBufferCanvasRenderer'
129
+ export * from './Paint/Render/PaintCursorRenderer'
119
130
 
120
131
  export * from './PixelData/_pixelData-types'
121
132
  export * from './PixelData/applyAlphaMaskToPixelData'