-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircleselector.tsx
More file actions
205 lines (177 loc) · 7.48 KB
/
circleselector.tsx
File metadata and controls
205 lines (177 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React from "react";
import { Angle, CartesianPoint, Vector, PolarPoint, DegreeAngle } from "./math2d";
import { OpenCircleSvg } from "./opencircle";
export function CircleSelector({
value,
displayValue,
setValue,
layoutOptions,
styleOptions
}: {
value: number,
displayValue: string,
setValue: (value: number) => void,
layoutOptions: {
knobRadius?: number,
startAngle: Angle,
endAngle: Angle,
radius: number
},
styleOptions?: {
knobClass?: string,
primaryCircleClass?: string,
secondaryCircleClass?: string,
valueDisplayClass?: string,
primaryCircleProps?: React.SVGProps<SVGPathElement>,
secondaryCircleProps?: React.SVGProps<SVGPathElement>,
knobProps?: React.SVGProps<SVGCircleElement>,
valueDisplayProps?: React.SVGProps<SVGTextElement>
}
}): React.ReactElement {
if (value > 1 || value < 0) {
throw new Error("Illegal value");
}
let { width, center, knobRadius,
startAngleDegrees, endAngleNormalizedDegrees, knobPositionPolar, knobPositionCartesian
} = React.useMemo<{
width: number,
center: CartesianPoint,
knobRadius: number
startAngleDegrees: DegreeAngle,
endAngleNormalizedDegrees: DegreeAngle,
knobPositionPolar: PolarPoint,
knobPositionCartesian: CartesianPoint
}>(() => {
const knobRadius = (layoutOptions?.knobRadius) ?? 10;
const width = layoutOptions.radius * 2 + knobRadius * 2;
const center = new CartesianPoint(width / 2, width / 2);
const startAngleDegrees = layoutOptions.startAngle.convertToDegrees();
const endAngleDegrees = layoutOptions.endAngle.convertToDegrees();
const endAngleNormalizedDegrees = new PolarPoint(center, layoutOptions.radius, endAngleDegrees)
.normalize(startAngleDegrees)
.angle.convertToDegrees();
const knobPositionPolar = new PolarPoint(
center,
layoutOptions.radius,
new DegreeAngle(endAngleNormalizedDegrees.degrees * value + startAngleDegrees.degrees)
);
const knobPositionCartesian = knobPositionPolar.convertToCartesian();
return {
width,
center,
knobRadius,
startAngleDegrees,
endAngleNormalizedDegrees,
knobPositionPolar,
knobPositionCartesian
}
}, [value, layoutOptions]);
const pressed = React.useRef<{ pressed: boolean, lastRegisteredInput: number }>({
pressed: false,
lastRegisteredInput: 0
});
function onPress() {
pressed.current.pressed = true;
}
function onRelease() {
pressed.current.pressed = false;
pressed.current.lastRegisteredInput = 0;
}
function onMove(this: HTMLDivElement, event: MouseEvent | TouchEvent) {
function isTouchEvent(event: Event): event is TouchEvent {
return (event as any).touches;
}
event.preventDefault();
/* Debounce and check for mouse press */
const currentTime = new Date().getTime();
// Mouse movement only, so the input must not change the value
if (!pressed.current.pressed || currentTime - 25 < pressed.current.lastRegisteredInput) {
return;
}
/* Determine point on circle closest to the cursor */
function calculateClosestPointOnCircle(point: CartesianPoint): { point: CartesianPoint, offset: number } {
const centerToPoint: Vector = new CartesianPoint(
point.x - center.x,
point.y - center.y
);
const centerToPointLength = centerToPoint.calculateEuclideanDistance();
return {
offset: Math.abs(layoutOptions.radius - centerToPointLength),
point: new CartesianPoint(
centerToPoint.x / centerToPointLength * layoutOptions.radius + center.x,
centerToPoint.y / centerToPointLength * layoutOptions.radius + center.y
)
};
}
// Rectangle containing all elements. Offsets are relative to the viewport.
const targetDimensionsInViewport: DOMRect = this.getBoundingClientRect();
const cursorPosition: Vector = new CartesianPoint(
(isTouchEvent(event) ? event.touches[0].clientX : event.clientX) - targetDimensionsInViewport.left,
(isTouchEvent(event) ? event.touches[0].clientY : event.clientY) - targetDimensionsInViewport.top
);
const pointOnCircleCartesian = calculateClosestPointOnCircle(cursorPosition);
/* Check whether user has moved too far away */
if (pointOnCircleCartesian.offset > 75) {
return;
}
/* Check whether knob is in the "invalid part" of the circle */
const knobOpeningAngleDegrees = pointOnCircleCartesian.point.convertPointOnCircleToPolar(
center, layoutOptions.radius)
.normalize(startAngleDegrees)
.angle.convertToDegrees();
if (knobOpeningAngleDegrees.degrees > endAngleNormalizedDegrees.degrees) {
return;
}
const openingAngle = knobOpeningAngleDegrees.degrees;
const totalValueAngle = endAngleNormalizedDegrees.degrees;
const percentage = openingAngle / totalValueAngle;
pressed.current.lastRegisteredInput = currentTime;
setValue(percentage);
}
const containerRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
const element = containerRef.current;
if (!element) {
return;
}
// Necessary as event listeners must not be passive (Event#preventDefault fails otherwise)
element.addEventListener("mousemove", onMove, { passive: false });
element.addEventListener("touchmove", onMove, { passive: false });
return () => {
element.removeEventListener("mousemove", onMove);
element.removeEventListener("touchmove", onMove);
}
// eslint demands 'onMove' to be in the dependencies array as well
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [containerRef]);
return <div ref={containerRef}
onTouchStart={onPress} onTouchEnd={onRelease} onMouseDown={onPress} onMouseUp={onRelease}>
<svg width={width} height={width}>
<OpenCircleSvg
pathProps={styleOptions?.secondaryCircleProps}
layoutOptions={{ center, ...layoutOptions }}
styleOptions={{
className: styleOptions?.secondaryCircleClass
}} />
<OpenCircleSvg
pathProps={styleOptions?.primaryCircleProps}
layoutOptions={{
center,
radius: layoutOptions.radius,
startAngle: layoutOptions.startAngle,
endAngle: knobPositionPolar.angle
}}
styleOptions={{
className: styleOptions?.primaryCircleClass
}} />
<circle cx={knobPositionCartesian.x} cy={knobPositionCartesian.y}
r={knobRadius} fill="white"
className={styleOptions?.knobClass}
{...styleOptions?.knobProps} />
<text x={center.x} y={center.y}
textAnchor="middle"
className={styleOptions?.valueDisplayClass}
{...styleOptions?.valueDisplayProps}>{displayValue}</text>
</svg>
</div>
}