canvas2d-wrapper 2.1.1 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collisions/collideElement.js +43 -34
- package/dist/collisions/lineWithLine.d.ts +2 -0
- package/dist/collisions/lineWithLine.js +7 -0
- package/dist/collisions/pointInCircle.d.ts +1 -0
- package/dist/collisions/pointInCircle.js +6 -0
- package/dist/collisions/pointInPolygon.d.ts +2 -0
- package/dist/collisions/pointInPolygon.js +21 -0
- package/dist/collisions/pointInRectangle.d.ts +1 -0
- package/dist/collisions/pointInRectangle.js +11 -0
- package/dist/collisions/pointOnLinePath.d.ts +2 -0
- package/dist/collisions/pointOnLinePath.js +45 -0
- package/dist/main.d.ts +15 -1
- package/dist/main.js +38 -22
- package/dist/maths/getLineLength.d.ts +2 -0
- package/dist/maths/getLineLength.js +7 -0
- package/dist/maths/normalOnLine.d.ts +2 -0
- package/dist/maths/normalOnLine.js +7 -0
- package/dist/maths/segmentAngle.d.ts +2 -0
- package/dist/maths/segmentAngle.js +7 -0
- package/package.json +15 -15
- package/dist/Canvas2D.spec.js +0 -38101
- package/dist/collisions/inCircle.d.ts +0 -3
- package/dist/collisions/inCircle.js +0 -6
- package/dist/collisions/inPoly.d.ts +0 -2
- package/dist/collisions/inPoly.js +0 -21
- package/dist/collisions/inRect.d.ts +0 -2
- package/dist/collisions/inRect.js +0 -12
- package/dist/collisions/onLinePath.d.ts +0 -2
- package/dist/collisions/onLinePath.js +0 -45
|
@@ -1,53 +1,62 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
function
|
|
7
|
-
const
|
|
8
|
-
for (const
|
|
9
|
-
if (!
|
|
1
|
+
import u from "../functions/calcTileSize.js";
|
|
2
|
+
import f from "./pointInCircle.js";
|
|
3
|
+
import x from "./pointInPolygon.js";
|
|
4
|
+
import h from "./pointInRectangle.js";
|
|
5
|
+
import g from "./pointOnLinePath.js";
|
|
6
|
+
function O(r, d, s, l, p, m) {
|
|
7
|
+
const e = u(p, m), i = [];
|
|
8
|
+
for (const n of d) {
|
|
9
|
+
if (!n.hasCollisions)
|
|
10
10
|
continue;
|
|
11
|
-
const
|
|
12
|
-
switch (
|
|
11
|
+
const t = n.x * e, a = n.y * e;
|
|
12
|
+
switch (n.constructorName) {
|
|
13
13
|
case "Rect":
|
|
14
14
|
case "CanvasImage":
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
const o = n;
|
|
16
|
+
h(
|
|
17
|
+
t,
|
|
18
|
+
a,
|
|
19
|
+
o.width * e,
|
|
20
|
+
o.height * e,
|
|
21
|
+
s,
|
|
22
|
+
l,
|
|
23
|
+
o.rotation
|
|
24
|
+
) && i.push({
|
|
25
|
+
id: o.id,
|
|
26
|
+
element: n,
|
|
27
|
+
originalEvent: r,
|
|
28
|
+
posOnMap: { x: o.x, y: o.y }
|
|
21
29
|
});
|
|
22
30
|
break;
|
|
23
31
|
case "Circle":
|
|
24
|
-
|
|
25
|
-
id:
|
|
26
|
-
element:
|
|
27
|
-
originalEvent:
|
|
28
|
-
posOnMap: { x:
|
|
32
|
+
f(t, a, n.radius * e, s, l) && i.push({
|
|
33
|
+
id: n.id,
|
|
34
|
+
element: n,
|
|
35
|
+
originalEvent: r,
|
|
36
|
+
posOnMap: { x: n.x, y: n.y }
|
|
29
37
|
});
|
|
30
38
|
break;
|
|
31
39
|
case "Polygon":
|
|
32
|
-
|
|
33
|
-
id:
|
|
34
|
-
element:
|
|
35
|
-
originalEvent:
|
|
36
|
-
posOnMap: { x:
|
|
40
|
+
x(n.points, s, l, e) && i.push({
|
|
41
|
+
id: n.id,
|
|
42
|
+
element: n,
|
|
43
|
+
originalEvent: r,
|
|
44
|
+
posOnMap: { x: n.x, y: n.y }
|
|
37
45
|
});
|
|
38
46
|
break;
|
|
39
47
|
case "LinePath":
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
const c = n;
|
|
49
|
+
g(c.points, c.lineWidth, s, l, e) && i.push({
|
|
50
|
+
id: n.id,
|
|
51
|
+
element: n,
|
|
52
|
+
originalEvent: r,
|
|
53
|
+
posOnMap: { x: n.x, y: n.y }
|
|
45
54
|
});
|
|
46
55
|
break;
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
|
-
return i.length === 0 ? null : (i.length === 1 || i.sort((
|
|
58
|
+
return i.length === 0 ? null : (i.length === 1 || i.sort((n, t) => n.element.zIndex > t.element.zIndex ? -1 : n.element.zIndex < t.element.zIndex || n.id > t.id ? 1 : n.id < t.id ? -1 : 0), i[0]);
|
|
50
59
|
}
|
|
51
60
|
export {
|
|
52
|
-
|
|
61
|
+
O as default
|
|
53
62
|
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function e(x, l, y, u) {
|
|
2
|
+
const t = l.x - x.x, c = l.y - x.y, s = u.x - y.x, d = u.y - y.y, _ = (-c * (x.x - y.x) + t * (x.y - y.y)) / (-s * c + t * d), o = (+s * (x.y - y.y) - d * (x.x - y.x)) / (-s * c + t * d);
|
|
3
|
+
return _ >= 0 && _ <= 1 && o >= 0 && o <= 1 ? { x: x.x + o * t, y: x.y + o * c } : null;
|
|
4
|
+
}
|
|
5
|
+
export {
|
|
6
|
+
e as default
|
|
7
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function pointInCircle(centerX: number, centerY: number, radius: number, pointX: number, pointY: number): boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function s(r, a, t, u = 1) {
|
|
2
|
+
let n = !1;
|
|
3
|
+
for (let e = 0; e < r.length; e++) {
|
|
4
|
+
const l = r[e].x * u, f = r[e].y * u;
|
|
5
|
+
if (a === l && t === f)
|
|
6
|
+
return !0;
|
|
7
|
+
let c = e + 1;
|
|
8
|
+
c >= r.length && (c = 0);
|
|
9
|
+
const j = r[c].x * u, o = r[c].y * u;
|
|
10
|
+
if (f > t != o > t) {
|
|
11
|
+
const g = (a - l) * (o - f) - (j - l) * (t - f);
|
|
12
|
+
if (g === 0)
|
|
13
|
+
return !0;
|
|
14
|
+
g < 0 != o < f && (n = !n);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return n;
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
s as default
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function pointInRectangle(rectangleX: number, rectangleY: number, rectangleWidth: number, rectangleHeight: number, pointX: number, pointY: number, rotation?: number): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function L(f, M, x, I, u, c, s) {
|
|
2
|
+
let d = u, h = c;
|
|
3
|
+
if (s) {
|
|
4
|
+
const e = f + x / 2, t = M + I / 2;
|
|
5
|
+
d = e + (u - e) * Math.cos(-s) - (c - t) * Math.sin(-s), h = t + (c - t) * Math.cos(-s) + (u - e) * Math.sin(-s);
|
|
6
|
+
}
|
|
7
|
+
return f <= d && d <= f + x && M <= h && h <= M + I;
|
|
8
|
+
}
|
|
9
|
+
export {
|
|
10
|
+
L as default
|
|
11
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import h from "./pointInCircle.js";
|
|
2
|
+
function q(r, m, u, n, x = 1) {
|
|
3
|
+
const a = m / 2;
|
|
4
|
+
if (h(
|
|
5
|
+
r[0].x * x,
|
|
6
|
+
r[0].y * x,
|
|
7
|
+
a * x,
|
|
8
|
+
u,
|
|
9
|
+
n
|
|
10
|
+
) || h(
|
|
11
|
+
r[r.length - 1].x * x,
|
|
12
|
+
r[r.length - 1].y * x,
|
|
13
|
+
a * x,
|
|
14
|
+
u,
|
|
15
|
+
n
|
|
16
|
+
))
|
|
17
|
+
return !0;
|
|
18
|
+
for (let f = 1; f < r.length; f++) {
|
|
19
|
+
const y = {
|
|
20
|
+
x: r[f - 1].x * x,
|
|
21
|
+
y: r[f - 1].y * x
|
|
22
|
+
}, c = {
|
|
23
|
+
x: r[f].x * x,
|
|
24
|
+
y: r[f].y * x
|
|
25
|
+
}, d = y.x - c.x, o = y.y - c.y, s = Math.sqrt(d * d + o * o), t = ((u - y.x) * (c.x - y.x) + (n - y.y) * (c.y - y.y)) / Math.pow(s, 2);
|
|
26
|
+
if (t < 0 || t > 1)
|
|
27
|
+
continue;
|
|
28
|
+
const g = {
|
|
29
|
+
x: y.x + t * (c.x - y.x),
|
|
30
|
+
y: y.y + t * (c.y - y.y)
|
|
31
|
+
};
|
|
32
|
+
if (h(
|
|
33
|
+
g.x,
|
|
34
|
+
g.y,
|
|
35
|
+
a * x,
|
|
36
|
+
u,
|
|
37
|
+
n
|
|
38
|
+
))
|
|
39
|
+
return !0;
|
|
40
|
+
}
|
|
41
|
+
return !1;
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
q as default
|
|
45
|
+
};
|
package/dist/main.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { default as Canvas2D } from './Canvas2D';
|
|
2
|
+
import { default as lineWithLine } from './collisions/lineWithLine';
|
|
3
|
+
import { default as pointInCircle } from './collisions/pointInCircle';
|
|
4
|
+
import { default as pointInPolygon } from './collisions/pointInPolygon';
|
|
5
|
+
import { default as pointInRectangle } from './collisions/pointInRectangle';
|
|
6
|
+
import { default as pointOnLinePath } from './collisions/pointOnLinePath';
|
|
2
7
|
import { default as preloadImages } from './functions/preloadImages';
|
|
3
8
|
import { default as useGamepad } from './hooks/useGamepad';
|
|
4
9
|
import { default as useKeyboard } from './hooks/useKeyboard';
|
|
5
10
|
import { default as useMousePosition } from './hooks/useMousePosition';
|
|
6
11
|
import { default as useWindowDimensions } from './hooks/useWindowDimensions';
|
|
12
|
+
import { default as getLineLength } from './maths/getLineLength';
|
|
13
|
+
import { default as normalOnLine } from './maths/normalOnLine';
|
|
14
|
+
import { default as segmentAngle } from './maths/segmentAngle';
|
|
7
15
|
import { default as CanvasImage } from './shapes/CanvasImage';
|
|
8
16
|
import { default as CanvasObject } from './shapes/CanvasObject';
|
|
9
17
|
import { default as Circle } from './shapes/Circle';
|
|
@@ -21,4 +29,10 @@ declare global {
|
|
|
21
29
|
};
|
|
22
30
|
};
|
|
23
31
|
}
|
|
24
|
-
export { Canvas2D
|
|
32
|
+
export { Canvas2D };
|
|
33
|
+
export { CanvasImage, CanvasObject, Circle, LinePath, Polygon, Rect };
|
|
34
|
+
export { lineWithLine, pointInCircle, pointInPolygon, pointInRectangle, pointOnLinePath };
|
|
35
|
+
export { preloadImages };
|
|
36
|
+
export { useGamepad, useKeyboard, useMousePosition, useWindowDimensions };
|
|
37
|
+
export { getLineLength, normalOnLine, segmentAngle };
|
|
38
|
+
export { type Canvas2DProps, type CollideElementResultItem, type Position2D, type Surface2D };
|
package/dist/main.js
CHANGED
|
@@ -1,26 +1,42 @@
|
|
|
1
1
|
import { default as o } from "./Canvas2D.js";
|
|
2
|
-
import { default as r } from "./
|
|
3
|
-
import { default as s } from "./
|
|
4
|
-
import { default as
|
|
5
|
-
import { default as
|
|
6
|
-
import { default as
|
|
7
|
-
import { default as i } from "./
|
|
8
|
-
import { default as
|
|
9
|
-
import { default as
|
|
10
|
-
import { default as
|
|
11
|
-
import { default as
|
|
12
|
-
import { default as
|
|
2
|
+
import { default as r } from "./collisions/lineWithLine.js";
|
|
3
|
+
import { default as s } from "./collisions/pointInCircle.js";
|
|
4
|
+
import { default as n } from "./collisions/pointInPolygon.js";
|
|
5
|
+
import { default as p } from "./collisions/pointInRectangle.js";
|
|
6
|
+
import { default as d } from "./collisions/pointOnLinePath.js";
|
|
7
|
+
import { default as i } from "./functions/preloadImages.js";
|
|
8
|
+
import { default as L } from "./hooks/useGamepad.js";
|
|
9
|
+
import { default as C } from "./hooks/useKeyboard.js";
|
|
10
|
+
import { default as P } from "./hooks/useMousePosition.js";
|
|
11
|
+
import { default as v } from "./hooks/useWindowDimensions.js";
|
|
12
|
+
import { default as O } from "./maths/getLineLength.js";
|
|
13
|
+
import { default as D } from "./maths/normalOnLine.js";
|
|
14
|
+
import { default as W } from "./maths/segmentAngle.js";
|
|
15
|
+
import { default as w } from "./shapes/CanvasImage.js";
|
|
16
|
+
import { default as G } from "./shapes/CanvasObject.js";
|
|
17
|
+
import { default as M } from "./shapes/Circle.js";
|
|
18
|
+
import { default as q } from "./shapes/LinePath.js";
|
|
19
|
+
import { default as B } from "./shapes/Polygon.js";
|
|
20
|
+
import { default as F } from "./shapes/Rect.js";
|
|
13
21
|
export {
|
|
14
22
|
o as Canvas2D,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
w as CanvasImage,
|
|
24
|
+
G as CanvasObject,
|
|
25
|
+
M as Circle,
|
|
26
|
+
q as LinePath,
|
|
27
|
+
B as Polygon,
|
|
28
|
+
F as Rect,
|
|
29
|
+
O as getLineLength,
|
|
30
|
+
r as lineWithLine,
|
|
31
|
+
D as normalOnLine,
|
|
32
|
+
s as pointInCircle,
|
|
33
|
+
n as pointInPolygon,
|
|
34
|
+
p as pointInRectangle,
|
|
35
|
+
d as pointOnLinePath,
|
|
36
|
+
i as preloadImages,
|
|
37
|
+
W as segmentAngle,
|
|
38
|
+
L as useGamepad,
|
|
39
|
+
C as useKeyboard,
|
|
40
|
+
P as useMousePosition,
|
|
41
|
+
v as useWindowDimensions
|
|
26
42
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "canvas2d-wrapper",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/main.js",
|
|
6
6
|
"types": "dist/main.d.ts",
|
|
@@ -20,28 +20,28 @@
|
|
|
20
20
|
"react-dom": "^19.0.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@eslint/js": "^9.39.
|
|
23
|
+
"@eslint/js": "^9.39.1",
|
|
24
24
|
"@testing-library/jest-dom": "^6.9.1",
|
|
25
25
|
"@testing-library/react": "^16.3.0",
|
|
26
26
|
"@types/jest": "^30.0.0",
|
|
27
|
-
"@types/node": "^
|
|
28
|
-
"@types/react": "^19.2.
|
|
29
|
-
"@types/react-dom": "^19.2.
|
|
30
|
-
"@vitejs/plugin-react": "^5.1.
|
|
31
|
-
"@vitest/coverage-v8": "^4.0.
|
|
32
|
-
"eslint": "^9.39.
|
|
27
|
+
"@types/node": "^25.0.1",
|
|
28
|
+
"@types/react": "^19.2.7",
|
|
29
|
+
"@types/react-dom": "^19.2.3",
|
|
30
|
+
"@vitejs/plugin-react": "^5.1.2",
|
|
31
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
32
|
+
"eslint": "^9.39.1",
|
|
33
33
|
"eslint-plugin-react-hooks": "^7.0.1",
|
|
34
34
|
"eslint-plugin-react-refresh": "^0.4.24",
|
|
35
|
-
"glob": "^
|
|
35
|
+
"glob": "^13.0.0",
|
|
36
36
|
"globals": "^16.5.0",
|
|
37
|
-
"jsdom": "^27.
|
|
38
|
-
"react": "^19.2.
|
|
39
|
-
"react-dom": "^19.2.
|
|
37
|
+
"jsdom": "^27.3.0",
|
|
38
|
+
"react": "^19.2.3",
|
|
39
|
+
"react-dom": "^19.2.3",
|
|
40
40
|
"typescript": "5.9",
|
|
41
|
-
"typescript-eslint": "^8.
|
|
42
|
-
"vite": "^7.
|
|
41
|
+
"typescript-eslint": "^8.49.0",
|
|
42
|
+
"vite": "^7.2.7",
|
|
43
43
|
"vite-plugin-dts": "^4.5.4",
|
|
44
44
|
"vite-plugin-lib-inject-css": "^2.2.2",
|
|
45
|
-
"vitest": "^4.0.
|
|
45
|
+
"vitest": "^4.0.15"
|
|
46
46
|
}
|
|
47
47
|
}
|