@quinninc/pixi-transformer 0.1.0 → 0.1.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/README.md +63 -63
- package/dist/Transformer.d.ts +3 -1
- package/dist/Transformer.js +37 -8
- package/package.json +41 -41
package/README.md
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
# Free Transform Tool for PIXI.js
|
|
2
|
-
|
|
3
|
-
Works with pixi.js v8 and above.
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
### ✨ Features
|
|
8
|
-
|
|
9
|
-
- Translate - Drag to move anywhere
|
|
10
|
-
- Scale - Scale on X, Y or both axis'
|
|
11
|
-
- Rotate - drag to rotate
|
|
12
|
-
- Anchor point - update anchor point of transforms (AltLeft + Click or Drag)
|
|
13
|
-
- Snapping - snap rotation at 45degrees, snap anchor point to edges and corners (hold ShiftLeft).
|
|
14
|
-
- Theming - custom border color, control size and color, and anchor point graphic
|
|
15
|
-
|
|
16
|
-
> Note - all transformations are applied wrt the pivot point, update pivot point to achieve the desired result.
|
|
17
|
-
|
|
18
|
-
## Installation
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install @quinninc/pixi-transformer
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
```tsx
|
|
27
|
-
const root = new Container();
|
|
28
|
-
app.stage.addChild(root);
|
|
29
|
-
|
|
30
|
-
// create tool and add to stage
|
|
31
|
-
const selectTool = new FreeTransformTool();
|
|
32
|
-
selectTool.label = "transform-tool";
|
|
33
|
-
root.addChild(selectTool);
|
|
34
|
-
|
|
35
|
-
const obj = new Sprite({
|
|
36
|
-
texture: Texture.from("lenna"),
|
|
37
|
-
label: "lenna",
|
|
38
|
-
eventMode: "static",
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
root.addChild(obj);
|
|
42
|
-
|
|
43
|
-
obj.on("pointertap", (ev) => {
|
|
44
|
-
console.log("select - ", ev.currentTarget.label);
|
|
45
|
-
selectTool.select(ev.currentTarget);
|
|
46
|
-
});
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## API
|
|
50
|
-
|
|
51
|
-
### `FreeTransformTool` (class)
|
|
52
|
-
|
|
53
|
-
**Constructor Parameter Object:**
|
|
54
|
-
|
|
55
|
-
- `lineColor` - border line color. default - `0x66cfff`
|
|
56
|
-
- `handleColor` - (`number`) - handles' fill color. default - `0xffffff`
|
|
57
|
-
- `controlsSize` - (`number`) - handles' size in pixels. default - `10`
|
|
58
|
-
- `debug` - (`boolean`) - show scaling distance debug lines. default - `false`
|
|
59
|
-
- `generateAnchorMark` - a function to generate your custom anchor mark. default - `undefined` (uses default anchor mark)
|
|
60
|
-
- `pivotKeys` - (`string[]`) - keys to hold to change pivot point. default - `['AltLeft']` - [List of KeyCodes](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes#a-full-list-of-key-event-values)
|
|
61
|
-
- `snapKeys` - (`string[]`) - keys to hold to snap to grid. default - `['ShiftLeft']`
|
|
62
|
-
|
|
63
|
-
**returns:** `PIXI.Container` (the select tool)
|
|
1
|
+
# Free Transform Tool for PIXI.js
|
|
2
|
+
|
|
3
|
+
Works with pixi.js v8 and above.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
### ✨ Features
|
|
8
|
+
|
|
9
|
+
- Translate - Drag to move anywhere
|
|
10
|
+
- Scale - Scale on X, Y or both axis'
|
|
11
|
+
- Rotate - drag to rotate
|
|
12
|
+
- Anchor point - update anchor point of transforms (AltLeft + Click or Drag)
|
|
13
|
+
- Snapping - snap rotation at 45degrees, snap anchor point to edges and corners (hold ShiftLeft).
|
|
14
|
+
- Theming - custom border color, control size and color, and anchor point graphic
|
|
15
|
+
|
|
16
|
+
> Note - all transformations are applied wrt the pivot point, update pivot point to achieve the desired result.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @quinninc/pixi-transformer
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
const root = new Container();
|
|
28
|
+
app.stage.addChild(root);
|
|
29
|
+
|
|
30
|
+
// create tool and add to stage
|
|
31
|
+
const selectTool = new FreeTransformTool();
|
|
32
|
+
selectTool.label = "transform-tool";
|
|
33
|
+
root.addChild(selectTool);
|
|
34
|
+
|
|
35
|
+
const obj = new Sprite({
|
|
36
|
+
texture: Texture.from("lenna"),
|
|
37
|
+
label: "lenna",
|
|
38
|
+
eventMode: "static",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
root.addChild(obj);
|
|
42
|
+
|
|
43
|
+
obj.on("pointertap", (ev) => {
|
|
44
|
+
console.log("select - ", ev.currentTarget.label);
|
|
45
|
+
selectTool.select(ev.currentTarget);
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### `FreeTransformTool` (class)
|
|
52
|
+
|
|
53
|
+
**Constructor Parameter Object:**
|
|
54
|
+
|
|
55
|
+
- `lineColor` - border line color. default - `0x66cfff`
|
|
56
|
+
- `handleColor` - (`number`) - handles' fill color. default - `0xffffff`
|
|
57
|
+
- `controlsSize` - (`number`) - handles' size in pixels. default - `10`
|
|
58
|
+
- `debug` - (`boolean`) - show scaling distance debug lines. default - `false`
|
|
59
|
+
- `generateAnchorMark` - a function to generate your custom anchor mark. default - `undefined` (uses default anchor mark)
|
|
60
|
+
- `pivotKeys` - (`string[]`) - keys to hold to change pivot point. default - `['AltLeft']` - [List of KeyCodes](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes#a-full-list-of-key-event-values)
|
|
61
|
+
- `snapKeys` - (`string[]`) - keys to hold to snap to grid. default - `['ShiftLeft']`
|
|
62
|
+
|
|
63
|
+
**returns:** `PIXI.Container` (the select tool)
|
package/dist/Transformer.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export declare class Transformer extends Pixi.Container {
|
|
|
19
19
|
private target;
|
|
20
20
|
private movedThreshold;
|
|
21
21
|
private updateCallbacks;
|
|
22
|
+
private onMoveEndCallback?;
|
|
22
23
|
private handleOpacity;
|
|
23
24
|
private controlsSize;
|
|
24
25
|
private controlsDimOpacity;
|
|
@@ -60,7 +61,7 @@ export declare class Transformer extends Pixi.Container {
|
|
|
60
61
|
*
|
|
61
62
|
* selectTool.unselect();
|
|
62
63
|
*/
|
|
63
|
-
constructor({ id, lineColor, handleColor, controlsSize, debug, generateAnchorMark, snapKeys, pivotKeys, }?: {
|
|
64
|
+
constructor({ id, lineColor, handleColor, controlsSize, debug, generateAnchorMark, snapKeys, pivotKeys, onMoveEnd, }?: {
|
|
64
65
|
id?: string;
|
|
65
66
|
lineColor?: string | number;
|
|
66
67
|
handleColor?: string | number;
|
|
@@ -69,6 +70,7 @@ export declare class Transformer extends Pixi.Container {
|
|
|
69
70
|
generateAnchorMark?: false | ((graphic: Pixi.Graphics) => void);
|
|
70
71
|
snapKeys?: string[];
|
|
71
72
|
pivotKeys?: string[];
|
|
73
|
+
onMoveEnd?: () => void;
|
|
72
74
|
});
|
|
73
75
|
private addToolTip;
|
|
74
76
|
private handleHandleEvents;
|
package/dist/Transformer.js
CHANGED
|
@@ -28,7 +28,7 @@ export class Transformer extends Pixi.Container {
|
|
|
28
28
|
*
|
|
29
29
|
* selectTool.unselect();
|
|
30
30
|
*/
|
|
31
|
-
constructor({ id = "transformer", lineColor = 0x66cfff, handleColor = 0xffffff, controlsSize = 10, debug = false, generateAnchorMark, snapKeys = ["ShiftLeft"], pivotKeys = ["AltLeft"], } = {}) {
|
|
31
|
+
constructor({ id = "transformer", lineColor = 0x66cfff, handleColor = 0xffffff, controlsSize = 10, debug = false, generateAnchorMark, snapKeys = ["ShiftLeft"], pivotKeys = ["AltLeft"], onMoveEnd, } = {}) {
|
|
32
32
|
super();
|
|
33
33
|
// target Pixi.Container
|
|
34
34
|
this.target = null;
|
|
@@ -65,6 +65,7 @@ export class Transformer extends Pixi.Container {
|
|
|
65
65
|
this.generateAnchorMark = generateAnchorMark;
|
|
66
66
|
else if (generateAnchorMark === false)
|
|
67
67
|
this.generateAnchorMark = () => { };
|
|
68
|
+
this.onMoveEndCallback = onMoveEnd;
|
|
68
69
|
this.zIndex = 1e7;
|
|
69
70
|
// Pixi.Container Properties
|
|
70
71
|
this.visible = false;
|
|
@@ -197,12 +198,18 @@ export class Transformer extends Pixi.Container {
|
|
|
197
198
|
// console.log("onMoveHandleDown this, that - ", this, that);
|
|
198
199
|
if (!that.target)
|
|
199
200
|
return;
|
|
201
|
+
// Store target's starting position for move calculation
|
|
202
|
+
this.targetStartPos = that.target.position.clone();
|
|
203
|
+
// Store event's starting position relative to the transformer
|
|
204
|
+
this.eventStartPos = that.convertToLocal(downEvent.global.clone());
|
|
205
|
+
// Reset drag distance counter
|
|
206
|
+
this.dragDistance = 0;
|
|
207
|
+
// Set dragging flag (might be redundant if handleHandleEvents also sets it, but safe)
|
|
208
|
+
that.dragging = true;
|
|
209
|
+
this.dragging = true; // Flag specific to this handle instance
|
|
200
210
|
if (that.pivotKey) {
|
|
201
211
|
this.draggingPivot = true;
|
|
202
212
|
}
|
|
203
|
-
// data
|
|
204
|
-
this.targetStartPos = that.target.position.clone();
|
|
205
|
-
this.dragDistance = 0;
|
|
206
213
|
this.on("globalpointermove", onMoveHandleMove);
|
|
207
214
|
}
|
|
208
215
|
function onMoveHandleMove(moveEvent) {
|
|
@@ -230,19 +237,41 @@ export class Transformer extends Pixi.Container {
|
|
|
230
237
|
const moveDelta = new Pixi.Point(eventPoint.x - this.eventStartPos.x, eventPoint.y - this.eventStartPos.y);
|
|
231
238
|
that.target.position.x = this.targetStartPos.x + moveDelta.x;
|
|
232
239
|
that.target.position.y = this.targetStartPos.y + moveDelta.y;
|
|
240
|
+
// Calculate distance moved from the start point
|
|
233
241
|
this.dragDistance = calcDistance(eventPoint, this.eventStartPos);
|
|
234
|
-
that.update();
|
|
242
|
+
that.update(); // Update transformer visuals during move
|
|
235
243
|
moveEvent.stopPropagation();
|
|
236
244
|
}
|
|
237
245
|
function onMoveHandleUp(upEvent) {
|
|
238
246
|
// console.log("onMoveHandleUp this, that - ", this, that);
|
|
239
|
-
|
|
247
|
+
const wasDragging = this.dragging; // Capture state before resetting
|
|
248
|
+
// Reset state for this handle instance first
|
|
249
|
+
this.off("globalpointermove", onMoveHandleMove);
|
|
250
|
+
this.dragging = false; // Reset handle-specific dragging flag
|
|
251
|
+
// that.dragging = false; // Reset global dragging flag (might be reset by handleHandleEvents too)
|
|
240
252
|
if (this.draggingPivot) {
|
|
241
253
|
changeAnchorPoint(upEvent);
|
|
242
254
|
that.anchorMark.position.set(0, 0);
|
|
255
|
+
this.draggingPivot = false;
|
|
256
|
+
// Maybe call update here if pivot change needs immediate visual update?
|
|
257
|
+
// that.update();
|
|
243
258
|
}
|
|
244
|
-
|
|
245
|
-
|
|
259
|
+
// --- Trigger onMoveEnd Callback ---
|
|
260
|
+
const scale = Math.min(that.scale.x, that.scale.y) || 1;
|
|
261
|
+
if (wasDragging &&
|
|
262
|
+
this.dragDistance > that.movedThreshold / scale &&
|
|
263
|
+
that.target &&
|
|
264
|
+
that.onMoveEndCallback) {
|
|
265
|
+
console.log("Move ended, calling onMoveEndCallback (no args)");
|
|
266
|
+
that.onMoveEndCallback();
|
|
267
|
+
}
|
|
268
|
+
// --- End Trigger ---
|
|
269
|
+
this.dragDistance = 0;
|
|
270
|
+
// Note: The generic onHandleUp from handleHandleEvents will also fire
|
|
271
|
+
// and call updateCallbacks. Ensure order is okay or adjust if needed.
|
|
272
|
+
// If the generic one shouldn't fire its updateCallbacks for this handle,
|
|
273
|
+
// you might need to structure handleHandleEvents differently.
|
|
274
|
+
upEvent.stopPropagation(); // Prevent event bubbling further if needed
|
|
246
275
|
}
|
|
247
276
|
// ------------</ MoveTool>---------------
|
|
248
277
|
// ------------<ScaleXYTool>---------------
|
package/package.json
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@quinninc/pixi-transformer",
|
|
3
|
-
"private": false,
|
|
4
|
-
"version": "0.1.
|
|
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.1.1",
|
|
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
|
+
}
|