-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.js
90 lines (82 loc) · 2.03 KB
/
Point.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
class Point {
/**
* @param {Number} x
* @param {Number} y
* @param {Number} radius
* @param {String} color
* @param {CanvasRenderingContext2D} ctx
* @param {CanvasRenderingContext2D} highlightCtx
*/
constructor(x, y, radius, color, ctx, highlightCtx) {
this.x = x;
this.y = y;
this.radius = radius;
this.highlightRadius = radius * 2.5;
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.ctx = ctx;
this.highlightCtx = highlightCtx;
}
fill() {
this.ctx.fillStyle = this.color;
this.ctx.beginPath();
this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
this.ctx.fill();
}
stroke() {
this.ctx.strokeStyle = this.color;
this.ctx.beginPath();
this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
this.ctx.stroke();
}
/**
* @param {Number} x
* @param {Number} y
*/
inPoint(x, y) {
return (
this.x - this.highlightRadius <= x &&
x <= this.x + this.highlightRadius &&
this.y - this.highlightRadius <= y &&
y <= this.y + this.highlightRadius
);
}
/**
* @param {Number} x
* @param {Number} y
*/
moveTo(x, y) {
this.x = x;
this.y = y;
}
/**
* @param {Number} x
* @param {Number} y
*/
moveBy(x, y) {
this.x += x;
this.y += y;
}
highlight() {
this.highlightCtx.fillStyle = this.highlightColor;
this.highlightCtx.beginPath();
this.highlightCtx.arc(this.x, this.y, this.highlightRadius, 0, 2 * Math.PI);
this.highlightCtx.fill();
}
unhighlight() {
this.highlightCtx.clearRect(
this.x - this.highlightRadius - 20,
this.y - this.highlightRadius - 20,
this.x + this.highlightRadius + 20,
this.y + this.highlightRadius + 20,
);
// this.highlightCtx.clearRect(
// 0,
// 0,
// this.highlightCtx.canvas.width,
// this.highlightCtx.canvas.height,
// );
}
}