pixel-data-js 0.34.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/dist/index.prod.cjs +31 -25
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +7 -3
- package/dist/index.prod.js +31 -25
- package/dist/index.prod.js.map +1 -1
- package/package.json +1 -1
- package/src/Paint/PaintRect.ts +4 -1
- package/src/Paint/Render/PaintCursorRenderer.ts +31 -28
- package/src/Paint/_paint-types.ts +5 -0
package/package.json
CHANGED
package/src/Paint/PaintRect.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { _macro_paintRectCenterOffset } from '../Internal/macros'
|
|
2
|
-
import type
|
|
2
|
+
import { PaintMaskOutline, type PaintRect } from './_paint-types'
|
|
3
3
|
|
|
4
4
|
export function makePaintRect(w: number, h: number): PaintRect {
|
|
5
5
|
return {
|
|
6
|
+
type: null,
|
|
7
|
+
outlineType: PaintMaskOutline.RECT,
|
|
8
|
+
data: null,
|
|
6
9
|
w,
|
|
7
10
|
h,
|
|
8
11
|
centerOffsetX: _macro_paintRectCenterOffset(w),
|
|
@@ -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
|
|
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
|
|
31
|
-
type:
|
|
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
|
-
|
|
37
|
+
data: null,
|
|
38
|
+
}
|
|
38
39
|
|
|
39
40
|
let outline: BinaryMask
|
|
40
41
|
|
|
41
|
-
function update(paintMask?:
|
|
42
|
-
|
|
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
|
-
|
|
49
|
-
|
|
49
|
+
currentBrush.w * _scale + 2 * _scale,
|
|
50
|
+
currentBrush.h * _scale + 2 * _scale,
|
|
50
51
|
)
|
|
51
52
|
|
|
52
|
-
if (
|
|
53
|
-
if (
|
|
54
|
-
outline = makeCircleBinaryMaskOutline(
|
|
55
|
-
} else if (
|
|
56
|
-
outline = makeRectBinaryMaskOutline(
|
|
57
|
-
} else if (
|
|
58
|
-
outline = makeBinaryMaskOutline(
|
|
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 (
|
|
61
|
-
const mask = makeBinaryMaskFromAlphaMask(
|
|
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 +
|
|
74
|
-
boundsScratch.y = centerY +
|
|
75
|
-
boundsScratch.w =
|
|
76
|
-
boundsScratch.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 +
|
|
85
|
-
boundsScaledScratch.y = centerY * _scale +
|
|
86
|
-
boundsScaledScratch.w =
|
|
87
|
-
boundsScaledScratch.h =
|
|
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 +
|
|
98
|
-
const dy = centerY * _scale +
|
|
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
|
-
|
|
110
|
+
currentBrush,
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
113
|
|
|
@@ -20,9 +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
|
+
|
|
23
25
|
export interface PaintRect {
|
|
26
|
+
type: null,
|
|
27
|
+
readonly outlineType: PaintMaskOutline.RECT
|
|
24
28
|
w: number,
|
|
25
29
|
h: number,
|
|
26
30
|
centerOffsetX: number,
|
|
27
31
|
centerOffsetY: number,
|
|
32
|
+
data: null,
|
|
28
33
|
}
|