UNPKG

1.73 kBJavaScriptView Raw
1import { Rectangle } from "./Rectangle";
2import { Circle } from "./Circle";
3/**
4 * @category Utils
5 */
6export class CircleWarp extends Circle {
7 constructor(x, y, radius, canvasSize) {
8 super(x, y, radius);
9 this.canvasSize = canvasSize;
10 this.canvasSize = {
11 height: canvasSize.height,
12 width: canvasSize.width,
13 };
14 }
15 contains(point) {
16 if (super.contains(point)) {
17 return true;
18 }
19 const posNE = {
20 x: point.x - this.canvasSize.width,
21 y: point.y,
22 };
23 if (super.contains(posNE)) {
24 return true;
25 }
26 const posSE = {
27 x: point.x - this.canvasSize.width,
28 y: point.y - this.canvasSize.height,
29 };
30 if (super.contains(posSE)) {
31 return true;
32 }
33 const posSW = {
34 x: point.x,
35 y: point.y - this.canvasSize.height,
36 };
37 return super.contains(posSW);
38 }
39 intersects(range) {
40 if (super.intersects(range)) {
41 return true;
42 }
43 const rect = range;
44 const circle = range;
45 const newPos = {
46 x: range.position.x - this.canvasSize.width,
47 y: range.position.y - this.canvasSize.height,
48 };
49 if (circle.radius !== undefined) {
50 const biggerCircle = new Circle(newPos.x, newPos.y, circle.radius * 2);
51 return super.intersects(biggerCircle);
52 }
53 else if (rect.size !== undefined) {
54 const rectSW = new Rectangle(newPos.x, newPos.y, rect.size.width * 2, rect.size.height * 2);
55 return super.intersects(rectSW);
56 }
57 return false;
58 }
59}