@quinninc/pixi-transformer 0.0.9 → 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 CHANGED
@@ -1,63 +1,63 @@
1
- # Free Transform Tool for PIXI.js
2
-
3
- Works with pixi.js v8 and above.
4
-
5
- ![preview](https://cdn.quinn.live/transformer-image.png)
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
+ ![preview](https://cdn.quinn.live/transformer-image.png)
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)
@@ -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;
@@ -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;
@@ -149,10 +150,18 @@ export class Transformer extends Pixi.Container {
149
150
  if (!that.target)
150
151
  return;
151
152
  const eventPoint = that.convertToLocal(ev.global);
152
- console.log("Prev anchor - ", that._anchor);
153
- console.log("ev.position - ", eventPoint.x, eventPoint.y);
154
- console.log("that.target.position - ", that.target.position.x, that.target.position.y);
155
- console.log("that.target.pivot - ", that.target.pivot.x, that.target.pivot.y);
153
+ // console.log("Prev anchor - ", that._anchor);
154
+ // console.log("ev.position - ", eventPoint.x, eventPoint.y);
155
+ // console.log(
156
+ // "that.target.position - ",
157
+ // that.target.position.x,
158
+ // that.target.position.y,
159
+ // );
160
+ // console.log(
161
+ // "that.target.pivot - ",
162
+ // that.target.pivot.x,
163
+ // that.target.pivot.y,
164
+ // );
156
165
  let dx, dy;
157
166
  // calc new pivot point
158
167
  // step 1 - translate to origin
@@ -189,12 +198,18 @@ export class Transformer extends Pixi.Container {
189
198
  // console.log("onMoveHandleDown this, that - ", this, that);
190
199
  if (!that.target)
191
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
192
210
  if (that.pivotKey) {
193
211
  this.draggingPivot = true;
194
212
  }
195
- // data
196
- this.targetStartPos = that.target.position.clone();
197
- this.dragDistance = 0;
198
213
  this.on("globalpointermove", onMoveHandleMove);
199
214
  }
200
215
  function onMoveHandleMove(moveEvent) {
@@ -222,19 +237,41 @@ export class Transformer extends Pixi.Container {
222
237
  const moveDelta = new Pixi.Point(eventPoint.x - this.eventStartPos.x, eventPoint.y - this.eventStartPos.y);
223
238
  that.target.position.x = this.targetStartPos.x + moveDelta.x;
224
239
  that.target.position.y = this.targetStartPos.y + moveDelta.y;
240
+ // Calculate distance moved from the start point
225
241
  this.dragDistance = calcDistance(eventPoint, this.eventStartPos);
226
- that.update();
242
+ that.update(); // Update transformer visuals during move
227
243
  moveEvent.stopPropagation();
228
244
  }
229
245
  function onMoveHandleUp(upEvent) {
230
246
  // console.log("onMoveHandleUp this, that - ", this, that);
231
- upEvent.stopPropagation();
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)
232
252
  if (this.draggingPivot) {
233
253
  changeAnchorPoint(upEvent);
234
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();
235
258
  }
236
- this.draggingPivot = false;
237
- this.off("globalpointermove", onMoveHandleMove);
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
238
275
  }
239
276
  // ------------</ MoveTool>---------------
240
277
  // ------------<ScaleXYTool>---------------
@@ -547,7 +584,7 @@ export class Transformer extends Pixi.Container {
547
584
  }
548
585
  update() {
549
586
  if (!this.target) {
550
- console.log("no target, returning...");
587
+ // console.log("no target, returning...");
551
588
  return;
552
589
  }
553
590
  // copy object translation/transformation
package/package.json CHANGED
@@ -1,41 +1,41 @@
1
- {
2
- "name": "@quinninc/pixi-transformer",
3
- "private": false,
4
- "version": "0.0.9",
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
+ }