-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabricBezier.js
227 lines (197 loc) · 6.54 KB
/
fabricBezier.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const pointConfig = {
strokeWidth: 4,
radius: 8,
fill: '#fff',
stroke: '#333'
};
const handleConfig = {
strokeWidth: 2,
radius: 4,
fill: '#fff',
stroke: '#666'
};
const lineConfig = {
fill: '',
stroke: 'red',
objectCaching: false
};
fabric.Circle.prototype.originX = fabric.Circle.prototype.originY = 'center';
class Line {
constructor(startPoint, endPoint) {
this.position = new Vector2(startPoint.position.x, startPoint.position.y);
this.start = startPoint;
this.end = endPoint;
this.points = [startPoint, endPoint];
this.paths = [];
this.paths.push(this.createLine(this.start, this.end));
}
createLine(startPoint, endPoint) {
const line = new fabric.Path(
`M ${startPoint.position.x} ${startPoint.position.y} ` +
`C ${startPoint.firstHandle.position.x}, ${startPoint.firstHandle.position.y}, ` +
`${endPoint.secondHandle.position.x}, ${endPoint.secondHandle.position.y}, ` +
`${endPoint.position.x}, ${endPoint.position.y}`,
{...lineConfig}
);
line.hasBorders = false;
line.hasControls = false;
line.selectable = false;
return line;
}
move(diff) {
this.position = this.position.add(diff);
this.start.move(diff);
this.end.move(diff);
}
// Returns true if the given fabric element is used in the line
// Ex: Point, line or handle
containsElement(element) {
return [
...this.points.reduce((acc, p) => {
acc.push(p.fabricElement);
acc.push(p.firstHandle.fabricElement);
acc.push(p.secondHandle.fabricElement);
return acc;
}, []),
...this.paths
].includes(element);
}
moveElement(element) {
// Check if it's a point
let matchingPoint = this.points.find(p => p.fabricElement === element);
if (matchingPoint) {
this.movePoint(matchingPoint);
}
// Else, it's a handle, find its point and perform the appropriate stuff
matchingPoint = this.points.find(p => p.firstHandle.fabricElement === element || p.secondHandle.fabricElement === element);
if (matchingPoint) {
const handle = matchingPoint.firstHandle.fabricElement === element ? matchingPoint.firstHandle : matchingPoint.secondHandle;
this.moveHandle(matchingPoint, handle);
}
}
movePoint(point) {
const offset = new Vector2(
point.fabricElement.left - point.position.x,
point.fabricElement.top - point.position.y
);
point.position = point.position.add(offset);
point.firstHandle.move(offset);
point.secondHandle.move(offset);
const pointIndex = this.points.indexOf(point);
const pathWherePointIsStart = this.paths[pointIndex];
const pathWherePointIsEnd = this.paths[pointIndex - 1];
if (pathWherePointIsStart) {
const startPosition = pathWherePointIsStart.path[0];
startPosition[1] = point.position.x;
startPosition[2] = point.position.y;
const path = pathWherePointIsStart.path[1];
path[1] = point.firstHandle.position.x;
path[2] = point.firstHandle.position.y;
}
if (pathWherePointIsEnd) {
const path = pathWherePointIsEnd.path[1];
path[3] = point.secondHandle.position.x;
path[4] = point.secondHandle.position.y;
path[5] = point.position.x;
path[6] = point.position.y;
}
}
moveHandle(point, handle) {
console.debug('Move handle');
const isFirstHandle = handle === point.firstHandle;
// Update handle position vector
const offset = new Vector2(
handle.fabricElement.left - handle.position.x,
handle.fabricElement.top - handle.position.y
);
handle.position = handle.position.add(offset);
// Move the opposite handle, as they need to match their strength
if (isFirstHandle) {
point.secondHandle.move(offset.flip());
} else {
point.firstHandle.move(offset.flip());
}
const pointIndex = this.points.indexOf(point);
const pathWherePointIsStart = this.paths[pointIndex];
const pathWherePointIsEnd = this.paths[pointIndex - 1];
if (pathWherePointIsStart) {
const path = pathWherePointIsStart.path[1];
path[1] = point.firstHandle.position.x;
path[2] = point.firstHandle.position.y;
}
if (pathWherePointIsEnd) {
const path = pathWherePointIsEnd.path[1];
path[3] = point.secondHandle.position.x;
path[4] = point.secondHandle.position.y;
}
}
addSegment(segmentPosition, canvas) {
const newPoint = new LinePoint(segmentPosition, new Vector2(50, 50));
const previousEnd = this.end;
this.end = newPoint;
this.points.push(newPoint);
// The new segment is the new end, so make the previous handle visible
previousEnd.firstHandle.fabricElement.visible = true;
newPoint.firstHandle.fabricElement.visible = false;
// Create the new path segment
const newPath = this.createLine(previousEnd, newPoint);
this.paths.push(newPath);
// Add those things to the canvas
canvas.add(newPoint.fabricElement);
canvas.add(newPoint.firstHandle.fabricElement);
canvas.add(newPoint.secondHandle.fabricElement);
canvas.add(newPath);
}
}
class Vector2 {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(other) {
return new Vector2(this.x + other.x, this.y + other.y);
}
substract(other) {
return new Vector2(this.x - other.x, this.y - other.y);
}
flip() {
return new Vector2(-this.x, -this.y);
}
}
class LinePoint {
constructor(position, handleVector) {
this.position = position;
this.handleVector = handleVector;
this.fabricElement = this.createElement();
this.firstHandle = new Handle(position.add(handleVector));
this.secondHandle = new Handle(position.add(handleVector.flip()));
}
createElement() {
const element = new fabric.Circle({...pointConfig, top: this.position.y, left: this.position.x});
element.hasBorders = false;
element.hasControls = false;
return element;
}
move(offset) {
this.position = this.position.add(offset);
this.fabricElement.top = this.position.y;
this.fabricElement.left = this.position.x;
this.fabricElement.setCoords();
this.firstHandle.move(offset);
this.secondHandle.move(offset);
}
}
class Handle {
constructor(position) {
this.position = position;
this.fabricElement = new fabric.Circle({...handleConfig, top: position.y, left: position.x});
// this.fabricElement.hasBorders = false;
this.fabricElement.hasControls = false;
}
move(offset) {
this.position = this.position.add(offset);
this.fabricElement.left = this.position.x;
this.fabricElement.top = this.position.y;
this.fabricElement.setCoords();
}
}