void(),
<canvas width="400" height="200" style="outline:1px solid black;cursor:none;"></canvas>
<script>
class Point {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
render(game) {
game.ctx2d.beginPath();
game.ctx2d.arc(this.x, this.y, this.radius, 0, 7);
game.ctx2d.fill();
}
}
class Cursor extends Point {
constructor(x, y, radius, length, filter) {
super(x, y, radius);
this.length = length;
this.filter = filter;
this.points = new Set();
}
search(points) {
this.points.clear();
for (let point of points)
if (this.filter(this, point))
this.points.add(point);
}
render(game) {
this.search(game.points);
game.ctx2d.beginPath();
for (let point of this.points) {
game.ctx2d.moveTo(this.x, this.y);
game.ctx2d.lineTo(point.x, point.y);
}
game.ctx2d.closePath();
game.ctx2d.stroke();
super.render(game);
}
}
class Game {
constructor(canvas, cursor, points) {
this.ctx2d = canvas.getContext('2d');
this.cursor = cursor;
this.points = new Set(points);
this.render();
canvas.onmousemove = (e) => {
this.cursor.x = e.offsetX;
this.cursor.y = e.offsetY;
this.render();
};
}
render() {
this.ctx2d.clearRect(0, 0, 400, 200);
for (let point of this.points)
point.render(this);
this.cursor.render(this);
}
}
const theGame = new Game(
document.querySelector('canvas'),
new Cursor(200, 100, 2, 50, (A, B) => A.length > Math.hypot(A.x - B.x, A.y - B.y)),
new Array(100).fill(0).map(() => new Point(400 * Math.random(), 200 * Math.random(), 2))
);
</script>