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.
@@ -1,53 +1,62 @@
1
- import f from "../functions/calcTileSize.js";
2
- import u from "./inCircle.js";
3
- import p from "./inPoly.js";
4
- import x from "./inRect.js";
5
- import y from "./onLinePath.js";
6
- function I(o, c, a, s, m, d) {
7
- const r = f(m, d), i = [];
8
- for (const e of c) {
9
- if (!e.hasCollisions)
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 n = e.x * r, l = e.y * r;
12
- switch (e.constructorName) {
11
+ const t = n.x * e, a = n.y * e;
12
+ switch (n.constructorName) {
13
13
  case "Rect":
14
14
  case "CanvasImage":
15
- const t = e;
16
- x(t, n, l, a, s, r, t.rotation) && i.push({
17
- id: t.id,
18
- element: e,
19
- originalEvent: o,
20
- posOnMap: { x: t.x, y: t.y }
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
- u(e, n, l, a, s, r) && i.push({
25
- id: e.id,
26
- element: e,
27
- originalEvent: o,
28
- posOnMap: { x: e.x, y: e.y }
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
- p(e, a, s, r) && i.push({
33
- id: e.id,
34
- element: e,
35
- originalEvent: o,
36
- posOnMap: { x: e.x, y: e.y }
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
- y(e, a, s, r) && i.push({
41
- id: e.id,
42
- element: e,
43
- originalEvent: o,
44
- posOnMap: { x: e.x, y: e.y }
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((e, n) => e.element.zIndex > n.element.zIndex ? -1 : e.element.zIndex < n.element.zIndex || e.id > n.id ? 1 : e.id < n.id ? -1 : 0), i[0]);
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
- I as default
61
+ O as default
53
62
  };
@@ -0,0 +1,2 @@
1
+ import { default as Position2D } from '../types/Position2D';
2
+ export default function lineWithLine(aOrigin: Position2D, aDestination: Position2D, bOrigin: Position2D, bDestination: Position2D): Position2D | null;
@@ -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,6 @@
1
+ function e(n, t, a, c, s) {
2
+ return (n - c) * (n - c) + (t - s) * (t - s) <= a * a;
3
+ }
4
+ export {
5
+ e as default
6
+ };
@@ -0,0 +1,2 @@
1
+ import { default as Position2D } from '../types/Position2D';
2
+ export default function pointInPolygon(points: Position2D[], pointX: number, pointY: number, tileSize?: 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,2 @@
1
+ import { default as Position2D } from '../types/Position2D';
2
+ export default function pointOnLinePath(points: Position2D[], lineWidth: number, pointX: number, pointY: number, tileSize?: number): boolean;
@@ -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, CanvasImage, CanvasObject, Circle, LinePath, Polygon, preloadImages, Rect, useGamepad, useKeyboard, useMousePosition, useWindowDimensions, type Canvas2DProps, type CollideElementResultItem, type Position2D, type Surface2D };
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 "./functions/preloadImages.js";
3
- import { default as s } from "./hooks/useGamepad.js";
4
- import { default as d } from "./hooks/useKeyboard.js";
5
- import { default as l } from "./hooks/useMousePosition.js";
6
- import { default as x } from "./hooks/useWindowDimensions.js";
7
- import { default as i } from "./shapes/CanvasImage.js";
8
- import { default as c } from "./shapes/CanvasObject.js";
9
- import { default as v } from "./shapes/Circle.js";
10
- import { default as b } from "./shapes/LinePath.js";
11
- import { default as D } from "./shapes/Polygon.js";
12
- import { default as h } from "./shapes/Rect.js";
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
- i as CanvasImage,
16
- c as CanvasObject,
17
- v as Circle,
18
- b as LinePath,
19
- D as Polygon,
20
- h as Rect,
21
- r as preloadImages,
22
- s as useGamepad,
23
- d as useKeyboard,
24
- l as useMousePosition,
25
- x as useWindowDimensions
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
  };
@@ -0,0 +1,2 @@
1
+ import { default as Position2D } from '../types/Position2D.ts';
2
+ export default function getLineLength(from: Position2D, to: Position2D): number;
@@ -0,0 +1,7 @@
1
+ function i(t, n) {
2
+ const e = n.x - t.x, f = n.y - t.y;
3
+ return Math.sqrt(e * e + f * f);
4
+ }
5
+ export {
6
+ i as default
7
+ };
@@ -0,0 +1,2 @@
1
+ import { Position2D } from '../main';
2
+ export default function normalOnLine(lineOrigin: Position2D, lineDestination: Position2D, clockwise?: boolean): Position2D | null;
@@ -0,0 +1,7 @@
1
+ function f(n, t, o = !1) {
2
+ const s = t.x - n.x, x = t.y - n.y, y = Math.hypot(s, x), c = s / y, u = x / y, a = o ? u : -u, d = o ? -c : c;
3
+ return { x: a, y: d };
4
+ }
5
+ export {
6
+ f as default
7
+ };
@@ -0,0 +1,2 @@
1
+ import { default as Position2D } from '../types/Position2D';
2
+ export default function segmentAngle(from: Position2D, to: Position2D): number;
@@ -0,0 +1,7 @@
1
+ function r(t, n) {
2
+ const e = n.x - t.x, a = n.y - t.y;
3
+ return e === 0 && a === 0 ? 0 : Math.atan2(a, e) + Math.PI / 2;
4
+ }
5
+ export {
6
+ r as default
7
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas2d-wrapper",
3
- "version": "2.1.1",
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.0",
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": "^24.9.2",
28
- "@types/react": "^19.2.2",
29
- "@types/react-dom": "^19.2.2",
30
- "@vitejs/plugin-react": "^5.1.0",
31
- "@vitest/coverage-v8": "^4.0.6",
32
- "eslint": "^9.39.0",
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": "^11.0.3",
35
+ "glob": "^13.0.0",
36
36
  "globals": "^16.5.0",
37
- "jsdom": "^27.1.0",
38
- "react": "^19.2.0",
39
- "react-dom": "^19.2.0",
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.46.2",
42
- "vite": "^7.1.12",
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.6"
45
+ "vitest": "^4.0.15"
46
46
  }
47
47
  }