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
|
import React, { useEffect, useState } from 'react';
type Props = {
setFunc: (newPoints: { p1: number[]; p2: number[] }) => void;
};
const ANIMATION_DURATION = 750;
// const ANIMATION_TIMING_FUNC = "cubic-bezier(.42,.97,.52,1.49)";
const ANIMATION_TIMING_FUNC = 'cubic-bezier(0.3, .2, .2, 1.4)';
const CONTAINER_WIDTH = 200;
const EDITOR_WIDTH = 100;
const OFFSET = (CONTAINER_WIDTH - EDITOR_WIDTH) / 2;
const CubicBezierEditor = ({ setFunc }: Props) => {
const [animating, setAnimating] = useState(false);
const [cPoints, setCPoints] = useState({ p1: [0.67, 0.2], p2: [0.37, 0.88] });
const [c1Down, setC1Down] = useState(false);
const [c2Down, setC2Down] = useState(false);
const roundToHundredth = (num: number) => {
return Math.round(num * 100) / 100;
};
useEffect(() => {
if (animating) {
setTimeout(() => {
setAnimating(false);
}, ANIMATION_DURATION * 2);
}
}, [animating]);
useEffect(() => {
if (!c1Down) return;
window.addEventListener('pointerup', () => {
setC1Down(false);
});
const handlePointerMove = (e: PointerEvent) => {
const newX = cPoints.p1[0] + e.movementX / EDITOR_WIDTH;
if (newX < 0 || newX > 1) {
return;
}
setCPoints(prev => ({
...prev,
p1: [roundToHundredth(prev.p1[0] + e.movementX / EDITOR_WIDTH), roundToHundredth(prev.p1[1] - e.movementY / EDITOR_WIDTH)],
}));
setFunc({
...cPoints,
p1: [roundToHundredth(cPoints.p1[0] + e.movementX / EDITOR_WIDTH), roundToHundredth(cPoints.p1[1] - e.movementY / EDITOR_WIDTH)],
});
};
window.addEventListener('pointermove', handlePointerMove);
return () => window.removeEventListener('pointermove', handlePointerMove);
}, [c1Down, cPoints]);
useEffect(() => {
if (!c2Down) return;
window.addEventListener('pointerup', () => {
setC2Down(false);
});
const handlePointerMove = (e: PointerEvent) => {
const newX = cPoints.p2[0] + e.movementX / EDITOR_WIDTH;
if (newX < 0 || newX > 1) {
return;
}
setCPoints(prev => ({
...prev,
p2: [roundToHundredth(prev.p2[0] + e.movementX / EDITOR_WIDTH), roundToHundredth(prev.p2[1] - e.movementY / EDITOR_WIDTH)],
}));
setFunc({
...cPoints,
p1: [roundToHundredth(cPoints.p2[0] + e.movementX / EDITOR_WIDTH), roundToHundredth(cPoints.p2[1] - e.movementY / EDITOR_WIDTH)],
});
};
window.addEventListener('pointermove', handlePointerMove);
return () => window.removeEventListener('pointermove', handlePointerMove);
}, [c2Down, cPoints]);
return (
<div>
<svg className="presBox-bezier-editor" width={`${CONTAINER_WIDTH}`} height={`${CONTAINER_WIDTH}`} xmlns="http://www.w3.org/2000/svg">
{/* Outlines */}
<line x1={`${0 + OFFSET}`} y1={`${EDITOR_WIDTH + OFFSET}`} x2={`${EDITOR_WIDTH + OFFSET}`} y2={`${0 + OFFSET}`} stroke="#c1c1c1" strokeWidth="1" />
{/* Box Outline */}
<rect x={`${0 + OFFSET}`} y={`${0 + OFFSET}`} width={EDITOR_WIDTH} height={EDITOR_WIDTH} stroke="#c5c5c5" fill="transparent" strokeWidth="1" />
{/* Editor */}
<path
d={`M ${0 + OFFSET} ${EDITOR_WIDTH + OFFSET} C ${cPoints.p1[0] * EDITOR_WIDTH + OFFSET} ${EDITOR_WIDTH - cPoints.p1[1] * EDITOR_WIDTH + OFFSET}, ${
cPoints.p2[0] * EDITOR_WIDTH + OFFSET
} ${EDITOR_WIDTH - cPoints.p2[1] * EDITOR_WIDTH + OFFSET}, ${EDITOR_WIDTH + OFFSET} ${0 + OFFSET}`}
stroke="#ffffff"
fill="transparent"
/>
{/* Bottom left */}
<line x1={`${0 + OFFSET}`} y1={`${EDITOR_WIDTH + OFFSET}`} x2={`${cPoints.p1[0] * EDITOR_WIDTH + OFFSET}`} y2={`${EDITOR_WIDTH - cPoints.p1[1] * EDITOR_WIDTH + OFFSET}`} stroke="#ffffff" strokeWidth="1" />
<circle
cx={`${cPoints.p1[0] * EDITOR_WIDTH + OFFSET}`}
cy={`${EDITOR_WIDTH - cPoints.p1[1] * EDITOR_WIDTH + OFFSET}`}
r="5"
fill={`${c1Down ? '#3fa9ff' : '#ffffff'}`}
onPointerDown={e => {
e.stopPropagation();
setC1Down(true);
}}
onPointerUp={e => {
setC1Down(false);
}}
/>
{/* Top right */}
<line x1={`${EDITOR_WIDTH + OFFSET}`} y1={`${0 + OFFSET}`} x2={`${cPoints.p2[0] * EDITOR_WIDTH + OFFSET}`} y2={`${EDITOR_WIDTH - cPoints.p2[1] * EDITOR_WIDTH + OFFSET}`} stroke="#ffffff" strokeWidth="1" />
<circle
cx={`${cPoints.p2[0] * EDITOR_WIDTH + OFFSET}`}
cy={`${EDITOR_WIDTH - cPoints.p2[1] * EDITOR_WIDTH + OFFSET}`}
r="5"
fill={`${c2Down ? '#3fa9ff' : '#ffffff'}`}
onPointerDown={e => {
e.stopPropagation();
setC2Down(true);
}}
onPointerUp={e => {
setC2Down(false);
}}
/>
</svg>
</div>
);
};
export default CubicBezierEditor;
|