-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointWithTangent.js
91 lines (84 loc) · 1.79 KB
/
PointWithTangent.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
class PointWithTangent {
/** @typedef {{x: Number, y: Number}} Coord
/**
* @param {Number} x
* @param {Number} y
* @param {Number} handleX
* @param {Number} handleY
* @param {Number} radius
* @param {String} pointColor
* @param {String} handleColor
* @param {CanvasRenderingContext2D} ctx
* @param {CanvasRenderingContext2D} highlightCtx
*/
constructor(
x,
y,
handleX,
handleY,
radius,
pointColor,
handleColor,
ctx,
highlightCtx,
) {
this.point = new Point(x, y, radius, pointColor, ctx, highlightCtx);
this.handle = new Point(
handleX,
handleY,
radius * 1.5,
handleColor,
ctx,
highlightCtx,
);
/** @type {Coord} */
this.tangent;
this.#setTangent();
this.ctx = ctx;
this.modified = false;
}
draw() {
this.point.fill();
this.handle.stroke();
this.ctx.beginPath();
this.ctx.setLineDash([5, 15]);
this.ctx.moveTo(this.handle.x, this.handle.y);
this.ctx.lineTo(this.point.x, this.point.y);
this.ctx.stroke();
this.ctx.setLineDash([]);
this.modified = false;
}
/**
* @param {Number} x
* @param {Number} y
*/
moveHandleTo(x, y) {
this.handle.moveTo(x, y);
this.#setTangent();
this.modified = true;
}
/**
* @param {Number} x
* @param {Number} y
*/
movePointTo(x, y) {
const diffX = x - this.point.x;
const diffY = y - this.point.y;
this.point.moveTo(x, y);
this.handle.moveBy(diffX, diffY);
this.modified = true;
}
/**
* @param {Number} x
* @param {Number} y
*/
movePointBy(x, y) {
this.movePointTo(this.point.x + x, this.point.y + y);
}
#setTangent() {
this.tangent = {
x: this.point.x - this.handle.x,
y: this.point.y - this.handle.y,
};
}
}