Line data Source code
1 : import { computed, onBeforeUnmount, ref } from 'vue'
2 : import type { Ref } from 'vue'
3 :
4 : import { createInterpolator } from '@/components/curve/curveUtils'
5 : import type { CurveInterpolation } from '@comfyorg/graph-model/curve'
6 : import type { PointTuple as CurvePoint } from '@comfyorg/shared-frontend-utils/geometry'
7 :
8 : interface UseCurveEditorOptions {
9 : svgRef: Ref<SVGSVGElement | null>
10 : modelValue: Ref<CurvePoint[]>
11 : interpolation: Ref<CurveInterpolation>
12 : onEditEnd?: () => void
13 : }
14 :
15 3 : export function useCurveEditor({
16 : svgRef,
17 : modelValue,
18 : interpolation,
19 : onEditEnd
20 : }: UseCurveEditorOptions) {
21 3 : const dragIndex = ref(-1)
22 3 : let cleanupDrag: (() => void) | null = null
23 :
24 3 : const curvePath = computed(() => {
25 4 : const points = modelValue.value
26 4 : if (points.length < 2) return ''
27 4 : const sorted = [...points].sort((a, b) => a[0] - b[0])
28 :
29 4 : if (interpolation.value === 'linear') {
30 4 : return sorted
31 : .map((p, i) => `${i === 0 ? 'M' : 'L'}${p[0]},${1 - p[1]}`)
32 : .join('')
33 : }
34 :
35 0 : const interpolate = createInterpolator(sorted, interpolation.value)
36 0 : const xMin = sorted[0][0]
37 0 : const xMax = sorted[sorted.length - 1][0]
38 0 : const segments = 128
39 0 : const range = xMax - xMin
40 0 : const parts: string[] = []
41 0 : for (let i = 0; i <= segments; i++) {
42 0 : const x = xMin + range * (i / segments)
43 0 : const y = 1 - interpolate(x)
44 0 : parts.push(`${i === 0 ? 'M' : 'L'}${x},${y}`)
45 : }
46 0 : return parts.join('')
47 : })
48 :
49 1 : function svgCoords(e: PointerEvent): [number, number] {
50 1 : const svg = svgRef.value
51 1 : if (!svg) return [0, 0]
52 :
53 1 : const ctm = svg.getScreenCTM()
54 1 : if (!ctm) return [0, 0]
55 :
56 1 : const svgPt = new DOMPoint(e.clientX, e.clientY).matrixTransform(
57 : ctm.inverse()
58 : )
59 1 : return [
60 : Math.max(0, Math.min(1, svgPt.x)),
61 : Math.max(0, Math.min(1, 1 - svgPt.y))
62 : ]
63 : }
64 :
65 1 : function findNearestPoint(x: number, y: number): number {
66 1 : const threshold2 = 0.04 * 0.04
67 1 : let nearest = -1
68 1 : let minDist2 = threshold2
69 1 : for (let i = 0; i < modelValue.value.length; i++) {
70 2 : const dx = modelValue.value[i][0] - x
71 2 : const dy = modelValue.value[i][1] - y
72 2 : const dist2 = dx * dx + dy * dy
73 2 : if (dist2 < minDist2) {
74 0 : minDist2 = dist2
75 0 : nearest = i
76 : }
77 : }
78 1 : return nearest
79 : }
80 :
81 1 : function handleSvgPointerDown(e: PointerEvent) {
82 1 : if (e.button !== 0) return
83 :
84 1 : const [x, y] = svgCoords(e)
85 :
86 1 : const nearby = findNearestPoint(x, y)
87 1 : if (nearby >= 0) {
88 0 : startDrag(nearby, e)
89 0 : return
90 : }
91 :
92 1 : if (e.ctrlKey) return
93 :
94 1 : const newPoint: CurvePoint = [x, y]
95 1 : const newPoints: CurvePoint[] = [...modelValue.value, newPoint]
96 1 : newPoints.sort((a, b) => a[0] - b[0])
97 1 : modelValue.value = newPoints
98 :
99 1 : startDrag(newPoints.indexOf(newPoint), e)
100 : }
101 :
102 1 : function startDrag(index: number, e: PointerEvent) {
103 1 : cleanupDrag?.()
104 :
105 1 : if (e.button === 2 || (e.button === 0 && e.ctrlKey)) {
106 0 : if (modelValue.value.length > 2) {
107 0 : const newPoints = [...modelValue.value]
108 0 : newPoints.splice(index, 1)
109 0 : modelValue.value = newPoints
110 0 : onEditEnd?.()
111 : }
112 0 : return
113 : }
114 :
115 1 : dragIndex.value = index
116 1 : const svg = svgRef.value
117 1 : if (!svg) return
118 :
119 1 : svg.setPointerCapture(e.pointerId)
120 :
121 1 : const onMove = (ev: PointerEvent) => {
122 0 : if (dragIndex.value < 0) return
123 0 : const [x, y] = svgCoords(ev)
124 0 : const movedPoint: CurvePoint = [x, y]
125 0 : const newPoints = [...modelValue.value]
126 0 : newPoints[dragIndex.value] = movedPoint
127 0 : newPoints.sort((a, b) => a[0] - b[0])
128 0 : modelValue.value = newPoints
129 0 : dragIndex.value = newPoints.indexOf(movedPoint)
130 : }
131 :
132 1 : const endDrag = () => {
133 1 : if (dragIndex.value < 0) return
134 1 : dragIndex.value = -1
135 1 : svg.removeEventListener('pointermove', onMove)
136 1 : svg.removeEventListener('pointerup', endDrag)
137 1 : svg.removeEventListener('lostpointercapture', endDrag)
138 1 : cleanupDrag = null
139 1 : onEditEnd?.()
140 : }
141 :
142 1 : cleanupDrag = endDrag
143 :
144 1 : svg.addEventListener('pointermove', onMove)
145 1 : svg.addEventListener('pointerup', endDrag)
146 1 : svg.addEventListener('lostpointercapture', endDrag)
147 : }
148 :
149 3 : onBeforeUnmount(() => {
150 0 : cleanupDrag?.()
151 : })
152 :
153 3 : return {
154 : curvePath,
155 : handleSvgPointerDown,
156 : startDrag
157 : }
158 : }
|