Line data Source code
1 : import { ref } from 'vue'
2 :
3 : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
4 : import { useMaskEditorStore } from './maskEditorStore'
5 : import { useCoordinateTransform } from './useCoordinateTransform'
6 :
7 29 : export function useBrushAdjustment(initialSettings?: {
8 : useDominantAxis?: boolean
9 : brushAdjustmentSpeed?: number
10 : }) {
11 29 : const store = useMaskEditorStore()
12 29 : const coordinateTransform = useCoordinateTransform()
13 :
14 29 : const initialPoint = ref<Point | null>(null)
15 29 : const initialBrushSize = ref(0)
16 29 : const initialBrushHardness = ref(0)
17 29 : const useDominantAxis = ref(initialSettings?.useDominantAxis ?? false)
18 29 : const brushAdjustmentSpeed = ref(initialSettings?.brushAdjustmentSpeed ?? 1.0)
19 :
20 0 : async function startBrushAdjustment(event: PointerEvent): Promise<void> {
21 0 : event.preventDefault()
22 :
23 0 : const coords = { x: event.offsetX, y: event.offsetY }
24 0 : const coords_canvas = coordinateTransform.screenToCanvas(coords)
25 :
26 0 : store.brushPreviewGradientVisible = true
27 0 : initialPoint.value = coords_canvas
28 0 : initialBrushSize.value = store.brushSettings.size
29 0 : initialBrushHardness.value = store.brushSettings.hardness
30 : }
31 :
32 0 : async function handleBrushAdjustment(event: PointerEvent): Promise<void> {
33 0 : if (!initialPoint.value) {
34 0 : return
35 : }
36 :
37 0 : const coords = { x: event.offsetX, y: event.offsetY }
38 0 : const brushDeadZone = 5
39 0 : const coords_canvas = coordinateTransform.screenToCanvas(coords)
40 :
41 0 : const delta_x = coords_canvas.x - initialPoint.value.x
42 0 : const delta_y = coords_canvas.y - initialPoint.value.y
43 :
44 0 : const effectiveDeltaX = Math.abs(delta_x) < brushDeadZone ? 0 : delta_x
45 0 : const effectiveDeltaY = Math.abs(delta_y) < brushDeadZone ? 0 : delta_y
46 :
47 0 : let finalDeltaX = effectiveDeltaX
48 0 : let finalDeltaY = effectiveDeltaY
49 :
50 0 : if (useDominantAxis.value) {
51 0 : const ratio = Math.abs(effectiveDeltaX) / Math.abs(effectiveDeltaY)
52 0 : const threshold = 2.0
53 :
54 0 : if (ratio > threshold) {
55 0 : finalDeltaY = 0
56 0 : } else if (ratio < 1 / threshold) {
57 0 : finalDeltaX = 0
58 : }
59 : }
60 :
61 0 : const newSize = Math.max(
62 : 1,
63 : Math.min(
64 : 500,
65 : initialBrushSize.value + (finalDeltaX / 35) * brushAdjustmentSpeed.value
66 : )
67 : )
68 :
69 0 : const newHardness = Math.max(
70 : 0,
71 : Math.min(
72 : 1,
73 : initialBrushHardness.value -
74 : (finalDeltaY / 4000) * brushAdjustmentSpeed.value
75 : )
76 : )
77 :
78 0 : store.setBrushSize(newSize)
79 0 : store.setBrushHardness(newHardness)
80 : }
81 :
82 29 : return { startBrushAdjustment, handleBrushAdjustment }
83 : }
|