-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHermiteCurve.js
155 lines (143 loc) · 3.89 KB
/
HermiteCurve.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
class HermiteCurve {
/**
* @param {Number} step
* @param {String} color
* @param {Number} lineWidth
* @param {CanvasRenderingContext2D} ctx
* @param {CanvasRenderingContext2D} highlightCtx
*/
constructor(step, color, lineWidth, ctx, highlightCtx) {
/** @type {Array<PointWithTangent>} */
this.points = [];
this.step = step;
const tc = new tinycolor(color);
if (!tc.isValid()) throw new Error("Invalid color: " + color);
this.color = tc.getOriginalInput();
this.highlightColor = tc.brighten(20).toString();
this.lineWidth = lineWidth;
this.ctx = ctx;
this.highlightCtx = highlightCtx;
/** @type {Array<HermiteSegment>} */
this.segments = [];
this.closed = false;
this.boundingBoxColor = "#050017";
}
draw() {
this.ctx.strokeStyle = this.color;
this.ctx.lineWidth = this.lineWidth;
// Segments should be drawn before points so we don't
// loose the points' modified state.
this.segments.forEach((s) => s.draw());
this.points.forEach((p) => p.draw());
}
/**
* @param {PointWithTangent} p
*/
addPoint(p) {
this.points.push(p);
if (this.points.length > 1) {
const idxLast = this.points.length - 1;
this.segments.push(
this.#getHermiteSegment(this.points[idxLast - 1], this.points[idxLast]),
);
}
this.#getBoundingBox();
}
/**
* @param {Number} idx
*/
deletePoint(idx) {
if (idx < 0 || idx >= this.points.length) return;
if (0 < idx && idx < this.points.length - 1) {
this.segments.splice(
idx - 1,
2,
this.#getHermiteSegment(this.points[idx - 1], this.points[idx + 1]),
);
} else if (idx === 0) {
this.segments.splice(0, 1);
} else {
this.segments.pop();
}
this.points.splice(idx, 1);
this.#getBoundingBox();
}
/**
* @param {PointWithTangent} p1
* @param {PointWithTangent} p2
* @returns {HermiteSegment}
*/
#getHermiteSegment(p1, p2) {
return new HermiteSegment(p1, p2, this.step, this.ctx, this.highlightCtx);
}
#getBoundingBox() {
if (this.points.length === 0) return null;
const bounds = {
min: { x: this.points[0].point.x, y: this.points[0].point.y },
max: { x: this.points[0].point.x, y: this.points[0].point.y },
};
for (let i = 1; i < this.points.length; ++i) {
const p = this.points[i];
if (p.point.x < bounds.min.x) bounds.min.x = p.point.x;
else if (p.point.x > bounds.max.x) bounds.max.x = p.point.x;
if (p.point.y < bounds.min.y) bounds.min.y = p.point.y;
else if (p.point.y > bounds.max.y) bounds.max.y = p.point.y;
}
bounds.min.x -= 40;
bounds.max.x += 40;
bounds.min.y -= 40;
bounds.max.y += 40;
return bounds;
}
/**
* @param {Number} x
* @param {Number} y
*/
inBounds(x, y) {
const bounds = this.#getBoundingBox();
if (!bounds) return false;
return (
bounds.min.x <= x &&
x <= bounds.max.x &&
bounds.min.y <= y &&
y <= bounds.max.y
);
}
drawBoundingBox() {
const bounds = this.#getBoundingBox();
if (!bounds) return;
this.highlightCtx.strokeStyle = this.boundingBoxColor;
this.highlightCtx.lineWidth = 4;
this.highlightCtx.beginPath();
this.highlightCtx.roundRect(
bounds.min.x,
bounds.min.y,
bounds.max.x - bounds.min.x,
bounds.max.y - bounds.min.y,
10,
);
this.highlightCtx.stroke();
}
clearBoundingBox() {
this.highlightCtx.clearRect(
0,
0,
this.highlightCtx.canvas.width,
this.highlightCtx.canvas.height,
);
}
/**
* @param {Number} x
* @param {Number} y
*/
moveBy(x, y) {
this.points.forEach((p) => {
p.movePointBy(x, y);
});
}
highlight() {
this.highlightCtx.strokeStyle = this.highlightColor;
this.highlightCtx.lineWidth = this.lineWidth * 2;
this.segments.forEach((s) => s.highlight());
}
}