@quinninc/pixi-transformer 0.0.4 → 0.0.6
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/README.md +61 -61
- package/dist/FreeTransformTool.d.ts +6 -10
- package/dist/FreeTransformTool.js +61 -159
- package/dist/Transformer.d.ts +80 -0
- package/dist/Transformer.js +668 -0
- package/dist/graphics.d.ts +52 -0
- package/dist/graphics.js +115 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/vector.d.ts +1 -20
- package/dist/vector.js +6 -43
- package/package.json +41 -41
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
type Point = {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
};
|
|
5
|
+
import { Pixi } from "./pixi";
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param origin source from where distance will be calculated
|
|
9
|
+
* @param p1 point 1 - positive direction is towards this point
|
|
10
|
+
* @param p2 point 2
|
|
11
|
+
* @returns 1 for same direction, -1 if the angle is more than 90 degrees
|
|
12
|
+
*/
|
|
13
|
+
export declare function getDirection(origin: Point, p1: Point, p2: Point): -1 | 1;
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param point point to rotate
|
|
17
|
+
* @param origin origin point
|
|
18
|
+
* @param rotation angle in radians
|
|
19
|
+
* @returns rotated point
|
|
20
|
+
*/
|
|
21
|
+
export declare function rotatePoint(point: Point, origin: Point, rotation: number): Point;
|
|
22
|
+
/**
|
|
23
|
+
* Calculate the euclidean distance between two points.
|
|
24
|
+
*/
|
|
25
|
+
export declare function calcDistance(p1: Point, p2: Point): number;
|
|
26
|
+
export declare function calcAngleDegrees(x: number, y: number): number;
|
|
27
|
+
export declare function calcAngleRadians(x: number, y: number): number;
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @param p point to snap to edges and corners
|
|
31
|
+
* @param snapDistance max distance from edges and corners to trigger snap
|
|
32
|
+
* @param param2 bounds of the rectangle (0,0), (0,width), (width,height), (0, height)
|
|
33
|
+
*/
|
|
34
|
+
export declare function snapToEdgesAndCorners({ point, anchor, dim: { width, height }, snapDistance, }: {
|
|
35
|
+
point: Point;
|
|
36
|
+
snapDistance?: number;
|
|
37
|
+
dim: {
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
};
|
|
41
|
+
anchor: Point;
|
|
42
|
+
}): Point;
|
|
43
|
+
export declare function applyTransform(container: Pixi.Container, matrix: Pixi.Matrix): void;
|
|
44
|
+
export declare function parseTransformationMatrix(matrix: Pixi.Matrix): {
|
|
45
|
+
tx: number;
|
|
46
|
+
ty: number;
|
|
47
|
+
scaleX: number;
|
|
48
|
+
scaleY: number;
|
|
49
|
+
rotation: number;
|
|
50
|
+
};
|
|
51
|
+
export declare function getChildTransformRelativeToParent(child: Pixi.Container, parent: Pixi.Container): Pixi.Matrix;
|
|
52
|
+
export {};
|
package/dist/graphics.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Pixi } from "./pixi";
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param origin source from where distance will be calculated
|
|
5
|
+
* @param p1 point 1 - positive direction is towards this point
|
|
6
|
+
* @param p2 point 2
|
|
7
|
+
* @returns 1 for same direction, -1 if the angle is more than 90 degrees
|
|
8
|
+
*/
|
|
9
|
+
export function getDirection(origin, p1, p2) {
|
|
10
|
+
const v1 = { x: p1.x - origin.x, y: p1.y - origin.y };
|
|
11
|
+
const v2 = { x: p2.x - origin.x, y: p2.y - origin.y };
|
|
12
|
+
const dot = v1.x * v2.x + v1.y * v2.y;
|
|
13
|
+
return dot >= 0 ? 1 : -1;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param point point to rotate
|
|
18
|
+
* @param origin origin point
|
|
19
|
+
* @param rotation angle in radians
|
|
20
|
+
* @returns rotated point
|
|
21
|
+
*/
|
|
22
|
+
export function rotatePoint(point, origin, rotation) {
|
|
23
|
+
// Translate point to origin
|
|
24
|
+
const translatedX = point.x - origin.x;
|
|
25
|
+
const translatedY = point.y - origin.y;
|
|
26
|
+
// Apply rotation
|
|
27
|
+
const rotatedX = translatedX * Math.cos(rotation) - translatedY * Math.sin(rotation);
|
|
28
|
+
const rotatedY = translatedX * Math.sin(rotation) + translatedY * Math.cos(rotation);
|
|
29
|
+
// Translate point back
|
|
30
|
+
const x = rotatedX + origin.x;
|
|
31
|
+
const y = rotatedY + origin.y;
|
|
32
|
+
return { x, y };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Calculate the euclidean distance between two points.
|
|
36
|
+
*/
|
|
37
|
+
export function calcDistance(p1, p2) {
|
|
38
|
+
return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
|
|
39
|
+
}
|
|
40
|
+
export function calcAngleDegrees(x, y) {
|
|
41
|
+
return (calcAngleRadians(x, y) * 180) / Math.PI;
|
|
42
|
+
}
|
|
43
|
+
export function calcAngleRadians(x, y) {
|
|
44
|
+
return Math.atan2(y, x);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param p point to snap to edges and corners
|
|
49
|
+
* @param snapDistance max distance from edges and corners to trigger snap
|
|
50
|
+
* @param param2 bounds of the rectangle (0,0), (0,width), (width,height), (0, height)
|
|
51
|
+
*/
|
|
52
|
+
export function snapToEdgesAndCorners({ point, anchor, dim: { width, height }, snapDistance = 10, }) {
|
|
53
|
+
const top = -height * anchor.y;
|
|
54
|
+
const bottom = height + top;
|
|
55
|
+
const left = -width * anchor.x;
|
|
56
|
+
const right = width + left;
|
|
57
|
+
// console.log(top, left, bottom, right);
|
|
58
|
+
// console.log(point);
|
|
59
|
+
const snapPoints = [
|
|
60
|
+
// corners
|
|
61
|
+
{ x: left, y: top }, // top-left
|
|
62
|
+
{ x: right, y: top }, // top-right
|
|
63
|
+
{ x: right, y: bottom }, // bottom-right
|
|
64
|
+
{ x: left, y: bottom }, // bottom-left
|
|
65
|
+
// center
|
|
66
|
+
{ x: (left + right) / 2, y: (top + bottom) / 2 }, // center
|
|
67
|
+
// edges
|
|
68
|
+
{ x: (left + right) / 2, y: top }, // top-edge
|
|
69
|
+
{ x: (left + right) / 2, y: bottom }, // bottom-edge
|
|
70
|
+
{ x: left, y: (top + bottom) / 2 }, // left-edge
|
|
71
|
+
{ x: right, y: (top + bottom) / 2 }, // right-edge
|
|
72
|
+
];
|
|
73
|
+
let minDistance = Number.MAX_VALUE;
|
|
74
|
+
let snappedPoint = point;
|
|
75
|
+
for (const snapPoint of snapPoints) {
|
|
76
|
+
const distance = calcDistance(point, snapPoint);
|
|
77
|
+
if (distance < snapDistance && distance < minDistance) {
|
|
78
|
+
minDistance = distance;
|
|
79
|
+
snappedPoint = snapPoint;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return snappedPoint;
|
|
83
|
+
}
|
|
84
|
+
export function applyTransform(container, matrix) {
|
|
85
|
+
// Extract translation
|
|
86
|
+
const { tx, ty, scaleX, scaleY, rotation } = parseTransformationMatrix(matrix);
|
|
87
|
+
3;
|
|
88
|
+
// Apply the extracted transformations to the container
|
|
89
|
+
container.position.set(tx, ty);
|
|
90
|
+
container.scale.set(scaleX, scaleY);
|
|
91
|
+
container.rotation = rotation;
|
|
92
|
+
}
|
|
93
|
+
export function parseTransformationMatrix(matrix) {
|
|
94
|
+
const tx = matrix.tx;
|
|
95
|
+
const ty = matrix.ty;
|
|
96
|
+
// Extract scale
|
|
97
|
+
const scaleX = Math.sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
|
|
98
|
+
const scaleY = Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
|
|
99
|
+
// Extract rotation (in radians)
|
|
100
|
+
const rotation = Math.atan2(matrix.b, matrix.a);
|
|
101
|
+
return { tx, ty, scaleX, scaleY, rotation };
|
|
102
|
+
}
|
|
103
|
+
export function getChildTransformRelativeToParent(child, parent) {
|
|
104
|
+
// Get the worldTransform of the parent and child
|
|
105
|
+
const parentWorldTransform = parent.worldTransform;
|
|
106
|
+
const childWorldTransform = child.worldTransform;
|
|
107
|
+
// Invert the parent's worldTransform
|
|
108
|
+
const inverseParentWorldTransform = new Pixi.Matrix();
|
|
109
|
+
parentWorldTransform.copyTo(inverseParentWorldTransform).invert();
|
|
110
|
+
// Multiply the child's worldTransform by the inverse of the parent's worldTransform
|
|
111
|
+
const relativeTransform = new Pixi.Matrix();
|
|
112
|
+
childWorldTransform.copyTo(relativeTransform);
|
|
113
|
+
relativeTransform.append(inverseParentWorldTransform);
|
|
114
|
+
return relativeTransform;
|
|
115
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { Transformer } from "./
|
|
1
|
+
export { Transformer } from "./Transformer";
|
|
2
2
|
export declare function hello(): void;
|
package/dist/index.js
CHANGED
package/dist/vector.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
x: number;
|
|
3
|
-
y: number;
|
|
4
|
-
};
|
|
1
|
+
import { Point } from "pixi.js";
|
|
5
2
|
/**
|
|
6
3
|
*
|
|
7
4
|
* @param origin source from where distance will be calculated
|
|
@@ -24,19 +21,3 @@ export declare function rotatePoint(point: Point, origin: Point, rotation: numbe
|
|
|
24
21
|
export declare function calcDistance(p1: Point, p2: Point): number;
|
|
25
22
|
export declare function calcAngleDegrees(x: number, y: number): number;
|
|
26
23
|
export declare function calcAngleRadians(x: number, y: number): number;
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
* @param p point to snap to edges and corners
|
|
30
|
-
* @param snapDistance max distance from edges and corners to trigger snap
|
|
31
|
-
* @param param2 bounds of the rectangle (0,0), (0,width), (width,height), (0, height)
|
|
32
|
-
*/
|
|
33
|
-
export declare function snapToEdgesAndCorners({ point, anchor, dim: { width, height }, snapDistance, }: {
|
|
34
|
-
point: Point;
|
|
35
|
-
snapDistance?: number;
|
|
36
|
-
dim: {
|
|
37
|
-
width: number;
|
|
38
|
-
height: number;
|
|
39
|
-
};
|
|
40
|
-
anchor: Point;
|
|
41
|
-
}): Point;
|
|
42
|
-
export {};
|
package/dist/vector.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Point } from "pixi.js";
|
|
1
2
|
/**
|
|
2
3
|
*
|
|
3
4
|
* @param origin source from where distance will be calculated
|
|
@@ -6,8 +7,8 @@
|
|
|
6
7
|
* @returns 1 for same direction, -1 if the angle is more than 90 degrees
|
|
7
8
|
*/
|
|
8
9
|
export function getDirection(origin, p1, p2) {
|
|
9
|
-
const v1 =
|
|
10
|
-
const v2 =
|
|
10
|
+
const v1 = new Point(p1.x - origin.x, p1.y - origin.y);
|
|
11
|
+
const v2 = new Point(p2.x - origin.x, p2.y - origin.y);
|
|
11
12
|
const dot = v1.x * v2.x + v1.y * v2.y;
|
|
12
13
|
return dot >= 0 ? 1 : -1;
|
|
13
14
|
}
|
|
@@ -26,9 +27,9 @@ export function rotatePoint(point, origin, rotation) {
|
|
|
26
27
|
const rotatedX = translatedX * Math.cos(rotation) - translatedY * Math.sin(rotation);
|
|
27
28
|
const rotatedY = translatedX * Math.sin(rotation) + translatedY * Math.cos(rotation);
|
|
28
29
|
// Translate point back
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
return
|
|
30
|
+
const finalX = rotatedX + origin.x;
|
|
31
|
+
const finalY = rotatedY + origin.y;
|
|
32
|
+
return new Point(finalX, finalY);
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* Calculate the euclidean distance between two points.
|
|
@@ -42,41 +43,3 @@ export function calcAngleDegrees(x, y) {
|
|
|
42
43
|
export function calcAngleRadians(x, y) {
|
|
43
44
|
return Math.atan2(y, x);
|
|
44
45
|
}
|
|
45
|
-
/**
|
|
46
|
-
*
|
|
47
|
-
* @param p point to snap to edges and corners
|
|
48
|
-
* @param snapDistance max distance from edges and corners to trigger snap
|
|
49
|
-
* @param param2 bounds of the rectangle (0,0), (0,width), (width,height), (0, height)
|
|
50
|
-
*/
|
|
51
|
-
export function snapToEdgesAndCorners({ point, anchor, dim: { width, height }, snapDistance = 10, }) {
|
|
52
|
-
const top = -height * anchor.y;
|
|
53
|
-
const bottom = height + top;
|
|
54
|
-
const left = -width * anchor.x;
|
|
55
|
-
const right = width + left;
|
|
56
|
-
// console.log(top, left, bottom, right);
|
|
57
|
-
// console.log(point);
|
|
58
|
-
const snapPoints = [
|
|
59
|
-
// corners
|
|
60
|
-
{ x: left, y: top }, // top-left
|
|
61
|
-
{ x: right, y: top }, // top-right
|
|
62
|
-
{ x: right, y: bottom }, // bottom-right
|
|
63
|
-
{ x: left, y: bottom }, // bottom-left
|
|
64
|
-
// center
|
|
65
|
-
{ x: (left + right) / 2, y: (top + bottom) / 2 }, // center
|
|
66
|
-
// edges
|
|
67
|
-
{ x: (left + right) / 2, y: top }, // top-edge
|
|
68
|
-
{ x: (left + right) / 2, y: bottom }, // bottom-edge
|
|
69
|
-
{ x: left, y: (top + bottom) / 2 }, // left-edge
|
|
70
|
-
{ x: right, y: (top + bottom) / 2 }, // right-edge
|
|
71
|
-
];
|
|
72
|
-
let minDistance = Number.MAX_VALUE;
|
|
73
|
-
let snappedPoint = point;
|
|
74
|
-
for (const snapPoint of snapPoints) {
|
|
75
|
-
const distance = calcDistance(point, snapPoint);
|
|
76
|
-
if (distance < snapDistance && distance < minDistance) {
|
|
77
|
-
minDistance = distance;
|
|
78
|
-
snappedPoint = snapPoint;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return snappedPoint;
|
|
82
|
-
}
|
package/package.json
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@quinninc/pixi-transformer",
|
|
3
|
-
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
5
|
-
"repository": {
|
|
6
|
-
"url": "https://github.com/Quinn-Care-Private-Limited/pixi-transformer"
|
|
7
|
-
},
|
|
8
|
-
"type": "module",
|
|
9
|
-
"main": "dist/index.js",
|
|
10
|
-
"module": "dist/index.js",
|
|
11
|
-
"types": "dist/index.d.ts",
|
|
12
|
-
"files": [
|
|
13
|
-
"dist",
|
|
14
|
-
"package.json",
|
|
15
|
-
"README.md"
|
|
16
|
-
],
|
|
17
|
-
"keywords": [
|
|
18
|
-
"pixi.js",
|
|
19
|
-
"transformer",
|
|
20
|
-
"pixi.js v8"
|
|
21
|
-
],
|
|
22
|
-
"author": {
|
|
23
|
-
"name": "Rohit Kaushal",
|
|
24
|
-
"url": "https://github.com/RohitKaushal7"
|
|
25
|
-
},
|
|
26
|
-
"license": "MIT",
|
|
27
|
-
"scripts": {
|
|
28
|
-
"dev": "tsc -w",
|
|
29
|
-
"build": "tsc",
|
|
30
|
-
"deploy": "npm run build && npm version patch && npm publish --access public"
|
|
31
|
-
},
|
|
32
|
-
"devDependencies": {
|
|
33
|
-
"typescript": "^5.2.2",
|
|
34
|
-
"vite": "^5.3.1",
|
|
35
|
-
"pixi.js": "^8.2.1"
|
|
36
|
-
},
|
|
37
|
-
"dependencies": {},
|
|
38
|
-
"peerDependencies": {
|
|
39
|
-
"pixi.js": "^8.2.1"
|
|
40
|
-
}
|
|
41
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@quinninc/pixi-transformer",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.6",
|
|
5
|
+
"repository": {
|
|
6
|
+
"url": "https://github.com/Quinn-Care-Private-Limited/pixi-transformer"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"package.json",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"pixi.js",
|
|
19
|
+
"transformer",
|
|
20
|
+
"pixi.js v8"
|
|
21
|
+
],
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Rohit Kaushal",
|
|
24
|
+
"url": "https://github.com/RohitKaushal7"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "tsc -w",
|
|
29
|
+
"build": "tsc",
|
|
30
|
+
"deploy": "npm run build && npm version patch && npm publish --access public"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "^5.2.2",
|
|
34
|
+
"vite": "^5.3.1",
|
|
35
|
+
"pixi.js": "^8.2.1"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"pixi.js": "^8.2.1"
|
|
40
|
+
}
|
|
41
|
+
}
|