-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle-vs-rectangle.html
153 lines (129 loc) · 3.61 KB
/
circle-vs-rectangle.html
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
<html>
<head><title>Circle and Rectangle Intersection</title>
<style>
canvas {
background: #eee;
}
</style>
</head>
<body>
<canvas width=800 height=400></canvas>
<script>
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
let selectedObject = null;
class Sprite {
constructor(top, left, width, height) {
this.top= top; this.left = left;
this.width = width; this.height = height;
}
right() { return this.left + this.width; }
bottom() { return this.top + this.height; }
moveTo(x, y) {
this.left = x;
this.top = y;
}
containsMouse(x, y) {
return (this.top < y && y < this.top + this.height &&
this.left < x && x < this.left + this.width);
}
}
class Rect extends Sprite {
// no need to have constructor.
draw() {
ctx.beginPath();
ctx.fillStyle = 'rgba(50, 50, 205, .6)';
ctx.rect(this.left, this.top, this.width, this.height);
ctx.fill();
if (selectedObject == this) {
ctx.strokeStyle = 'black';
ctx.stroke();
}
}
}
class Circle extends Sprite {
constructor(centerX, centerY, radius) {
super(centerY - radius, centerX - radius, 2 * radius, 2* radius);
this.radius = radius;
}
centerX() { return this.left + this.width / 2; }
centerY() { return this.top + this.height / 2; }
draw() {
ctx.beginPath();
ctx.moveTo(this.centerX() + this.radius, this.centerY());
ctx.arc(this.centerX(), this.centerY(), this.radius, 0, 2 * Math.PI);
ctx.fillStyle = 'rgba(100, 205, 100, .6)';
if (connector.dist < this.radius) {
ctx.fillStyle = 'rgba(205, 100, 100, .6)';
}
ctx.fill();
if (selectedObject == this) {
ctx.strokeStyle = 'black';
ctx.stroke();
}
}
}
function clamp(val, low, high) {
if (val < low) return low;
if (val > high) return high;
return val;
}
class CenterConnector {
constructor() {
this.nearestX = 0;
this.nearestY = 0;
this.dist = 0;
}
update() {
this.nearestX = clamp(circle.centerX(), rect.left, rect.right());
this.nearestY = clamp(circle.centerY(), rect.top, rect.bottom());
this.dist = Math.sqrt( (this.nearestX - circle.centerX()) ** 2 +
(this.nearestY - circle.centerY()) ** 2);
}
draw() {
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.fillStyle = 'red';
ctx.moveTo(this.nearestX, this.nearestY);
ctx.arc(this.nearestX, this.nearestY, 3, 0, 2*Math.PI);
ctx.fill();
ctx.lineTo(circle.centerX(), circle.centerY());
ctx.stroke();
ctx.arc(circle.centerX(), circle.centerY(), 3, 0, 2*Math.PI);
ctx.fill();
ctx.font = '18px sans-serif';
ctx.fillText('' + Math.floor(this.dist),
(this.nearestX + circle.centerX()) / 2 + 20,
(this.nearestY + circle.centerY()) / 2 + 20);
}
}
const rect = new Rect(40, 40, 100, 75);
const circle = new Circle(280, 70, 60);
const connector = new CenterConnector();
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
rect.draw();
circle.draw();
connector.update();
connector.draw();
}
window.setInterval(gameLoop, 100);
let gripX = 0, gripY = 0;
canvas.addEventListener('mousedown', (e) => {
selectedObject = null;
for (let sprite of [circle, rect]) {
if (sprite.containsMouse(e.x, e.y)) {
selectedObject = sprite;
gripX = e.x - sprite.left;
gripY = e.y - sprite.top;
}
}
})
canvas.addEventListener('mousemove', (e) => {
if (!e.buttons) return;
if (selectedObject == null) return;
selectedObject.moveTo(e.x - gripX, e.y - gripY);
})
</script>
</body>
</html>