@quinninc/pixi-transformer 0.0.3 → 0.0.5

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.
@@ -67,12 +67,13 @@ export declare class Transformer extends Pixi.Container {
67
67
  private createHandleIndicatorTo;
68
68
  private deScale;
69
69
  select(target: Pixi.Container): void;
70
- onUpdate: () => void;
71
70
  unselect(): void;
71
+ onUpdate: () => void;
72
72
  update(): void;
73
73
  setTitle(title: string): void;
74
74
  setCursor(cursor: string): void;
75
75
  private generateAnchorMark;
76
76
  get _target(): Pixi.Container<Pixi.ContainerChild> | Pixi.Sprite | null;
77
77
  cleanup(): void;
78
+ private convertToLocal;
78
79
  }
@@ -1,5 +1,5 @@
1
1
  import { Pixi } from "./pixi";
2
- import { calcAngleRadians, calcDistance, getDirection, rotatePoint, snapToEdgesAndCorners, } from "./vector";
2
+ import { calcAngleRadians, calcDistance, getDirection, rotatePoint, snapToEdgesAndCorners, } from "./graphics";
3
3
  export class Transformer extends Pixi.Container {
4
4
  /**
5
5
  *
@@ -126,19 +126,20 @@ export class Transformer extends Pixi.Container {
126
126
  this.addChild(this.moveHandle);
127
127
  function changeAnchorPoint(ev) {
128
128
  // console.log("TAPP this, that - ", this, that);
129
- if (!ev.ctrlKey)
129
+ if (!ev.ctrlKey && !ev.metaKey)
130
130
  return;
131
131
  if (!that.target)
132
132
  return;
133
+ const eventPoint = that.convertToLocal(ev.global);
133
134
  console.log("Prev anchor - ", that._anchor);
134
- console.log("ev.position - ", ev.global.x, ev.global.y);
135
+ console.log("ev.position - ", eventPoint.x, eventPoint.y);
135
136
  console.log("that.target.position - ", that.target.position.x, that.target.position.y);
136
137
  console.log("that.target.pivot - ", that.target.pivot.x, that.target.pivot.y);
137
138
  let dx, dy;
138
139
  // calc new pivot point
139
140
  // step 1 - translate to origin
140
- dx = ev.global.x - that.target.position.x;
141
- dy = ev.global.y - that.target.position.y;
141
+ dx = eventPoint.x - that.target.position.x;
142
+ dy = eventPoint.y - that.target.position.y;
142
143
  // step 2 - rotate by -angle
143
144
  const rp = rotatePoint({ x: dx, y: dy }, { x: 0, y: 0 }, -that.target.rotation);
144
145
  let rdx = rp.x;
@@ -170,7 +171,7 @@ export class Transformer extends Pixi.Container {
170
171
  // console.log("onMoveHandleDown this, that - ", this, that);
171
172
  if (!that.target)
172
173
  return;
173
- if (downEvent.ctrlKey) {
174
+ if (downEvent.ctrlKey || downEvent.metaKey) {
174
175
  this.draggingPivot = true;
175
176
  }
176
177
  // data
@@ -182,10 +183,11 @@ export class Transformer extends Pixi.Container {
182
183
  // console.log("onMoveHandleMove this, that - ", this, that);
183
184
  if (!this.dragging || !that.target)
184
185
  return;
186
+ const eventPoint = that.convertToLocal(moveEvent.global);
185
187
  if (this.draggingPivot) {
186
188
  // changing pivot point
187
- let x = moveEvent.global.x - that.target.x;
188
- let y = moveEvent.global.y - that.target.y;
189
+ let x = eventPoint.x - that.target.x;
190
+ let y = eventPoint.y - that.target.y;
189
191
  const p = rotatePoint({ x, y }, { x: 0, y: 0 }, -that.target.rotation);
190
192
  const snappedPoint = snapToEdgesAndCorners({
191
193
  point: p,
@@ -199,10 +201,10 @@ export class Transformer extends Pixi.Container {
199
201
  that.anchorMark.position.set(snappedPoint.x / that.target.scale.x, snappedPoint.y / that.target.scale.y);
200
202
  return;
201
203
  }
202
- const moveDelta = new Pixi.Point(moveEvent.global.x - this.eventStartGlobalPos.x, moveEvent.global.y - this.eventStartGlobalPos.y);
204
+ const moveDelta = new Pixi.Point(eventPoint.x - this.eventStartPos.x, eventPoint.y - this.eventStartPos.y);
203
205
  that.target.position.x = this.targetStartPos.x + moveDelta.x;
204
206
  that.target.position.y = this.targetStartPos.y + moveDelta.y;
205
- this.dragDistance = calcDistance(moveEvent.global, this.eventStartGlobalPos);
207
+ this.dragDistance = calcDistance(eventPoint, this.eventStartPos);
206
208
  that.update();
207
209
  moveEvent.stopPropagation();
208
210
  }
@@ -240,9 +242,10 @@ export class Transformer extends Pixi.Container {
240
242
  // console.log("onScaleToolMove this, that - ", this, that);
241
243
  if (!this.dragging || !that.target)
242
244
  return;
243
- const distStart = calcDistance(this.eventStartGlobalPos, that.target.position);
244
- const distEnd = calcDistance(moveEvent.global, that.target.position);
245
- const direction = getDirection(new Pixi.Point(that.position.x, that.position.y), new Pixi.Point(this.eventStartGlobalPos.x, this.eventStartGlobalPos.y), new Pixi.Point(moveEvent.global.x, moveEvent.global.y));
245
+ const eventPoint = that.convertToLocal(moveEvent.global);
246
+ const distStart = calcDistance(this.eventStartPos, that.target.position);
247
+ const distEnd = calcDistance(eventPoint, that.target.position);
248
+ const direction = getDirection(that.position, this.eventStartPos, eventPoint);
246
249
  this.rescaleFactor = (distEnd / distStart) * direction;
247
250
  that.target.scale.x = this.targetStartScale.x * this.rescaleFactor;
248
251
  that.target.scale.y = this.targetStartScale.y * this.rescaleFactor;
@@ -274,11 +277,12 @@ export class Transformer extends Pixi.Container {
274
277
  // console.log("onVScaleToolMove this - ", this);
275
278
  if (!this.dragging || !that.target)
276
279
  return;
280
+ const eventPoint = that.convertToLocal(moveEvent.global);
277
281
  const referencePoint = that.target.position.clone();
278
282
  // referencePoint.x = that.target.position.x + that.target.width / 2;
279
- const distStart = calcDistance(this.eventStartGlobalPos, referencePoint);
280
- const distEnd = calcDistance(moveEvent.global, referencePoint);
281
- const direction = getDirection(referencePoint, this.eventStartGlobalPos, moveEvent.global);
283
+ const distStart = calcDistance(this.eventStartPos, referencePoint);
284
+ const distEnd = calcDistance(eventPoint, referencePoint);
285
+ const direction = getDirection(referencePoint, this.eventStartPos, eventPoint);
282
286
  this.rescaleFactor = (distEnd / distStart) * direction;
283
287
  that.target.scale.y = this.targetStartScale.y * this.rescaleFactor;
284
288
  that.update();
@@ -306,11 +310,12 @@ export class Transformer extends Pixi.Container {
306
310
  // console.log("onHScaleToolMove this - ", this);
307
311
  if (!this.dragging || !that.target)
308
312
  return;
313
+ const eventPoint = that.convertToLocal(moveEvent.global);
309
314
  const referencePoint = that.target.position.clone();
310
315
  // referencePoint.y = that.target.position.y + that.target.height / 2;
311
- const distStart = calcDistance(this.eventStartGlobalPos, referencePoint);
312
- const distEnd = calcDistance(moveEvent.global, referencePoint);
313
- const direction = getDirection(referencePoint, this.eventStartGlobalPos, moveEvent.global);
316
+ const distStart = calcDistance(this.eventStartPos, referencePoint);
317
+ const distEnd = calcDistance(eventPoint, referencePoint);
318
+ const direction = getDirection(referencePoint, this.eventStartPos, eventPoint);
314
319
  const rescaleFactor = (distEnd / distStart) * direction;
315
320
  that.target.scale.x = this.targetStartScale.x * rescaleFactor;
316
321
  that.update();
@@ -335,20 +340,21 @@ export class Transformer extends Pixi.Container {
335
340
  // console.log("onRotateToolMove this, that - ", this, that);
336
341
  if (!this.dragging || !that.target)
337
342
  return;
343
+ const eventPoint = that.convertToLocal(moveEvent.global);
338
344
  // the drag point is relative to the display object x,y position on the stage (it's registration point)
339
345
  const relativeStartPoint = {
340
- x: this.eventStartGlobalPos.x - that.target.x,
341
- y: this.eventStartGlobalPos.y - that.target.y,
346
+ x: this.eventStartPos.x - that.target.x,
347
+ y: this.eventStartPos.y - that.target.y,
342
348
  };
343
349
  const relativeEndPoint = {
344
- x: moveEvent.global.x - that.target.x,
345
- y: moveEvent.global.y - that.target.y,
350
+ x: eventPoint.x - that.target.x,
351
+ y: eventPoint.y - that.target.y,
346
352
  };
347
353
  const startAngle = calcAngleRadians(relativeStartPoint.x, relativeStartPoint.y);
348
354
  const endAngle = calcAngleRadians(relativeEndPoint.x, relativeEndPoint.y);
349
355
  const deltaAngle = endAngle - startAngle;
350
356
  let finalRotation = this.targetStartRotation + deltaAngle;
351
- if (moveEvent.ctrlKey) {
357
+ if (moveEvent.ctrlKey || moveEvent.metaKey) {
352
358
  const snapAngle = 45;
353
359
  let finalAngle = (finalRotation * 180) / Math.PI;
354
360
  const proximity = Math.min(Math.abs(finalAngle % snapAngle), snapAngle - Math.abs(finalAngle % snapAngle));
@@ -391,19 +397,20 @@ export class Transformer extends Pixi.Container {
391
397
  // console.log("onHandleDown this - ", this);
392
398
  that.dragging = true;
393
399
  handle.dragging = true;
394
- handle.eventStartGlobalPos = e.global.clone();
400
+ handle.eventStartPos = that.convertToLocal(e.global.clone());
395
401
  handle.targetStartScale = that.target.scale.clone();
396
402
  handle.targetStartRotation = that.target.rotation;
397
403
  handle.on("globalpointermove", onHandleMove);
398
404
  // graphic - fromPoint
399
405
  if (that.debug) {
400
- that.createHandleIndicatorTo(that.fromPoint, handle.eventStartGlobalPos.x, handle.eventStartGlobalPos.y);
406
+ that.createHandleIndicatorTo(that.fromPoint, handle.eventStartPos.x, handle.eventStartPos.y);
401
407
  }
402
408
  }
403
409
  function onHandleMove(e) {
404
410
  // console.log("onHandleMove this - ", this);
411
+ const eventPoint = that.convertToLocal(e.global.clone());
405
412
  if (that.debug) {
406
- that.createHandleIndicatorTo(that.toPoint, e.globalX, e.globalY);
413
+ that.createHandleIndicatorTo(that.toPoint, eventPoint.x, eventPoint.y);
407
414
  that.deScale(that.fromPoint);
408
415
  }
409
416
  }
@@ -482,7 +489,11 @@ export class Transformer extends Pixi.Container {
482
489
  _anchor = new Pixi.Point(0.5, 0.5);
483
490
  }
484
491
  this._anchor = _anchor;
485
- console.log("Anchor - ", _anchor);
492
+ if (this.parent !== this.target.parent) {
493
+ // console.log("Different parent - switching transformer location position");
494
+ this.target.parent.addChild(this);
495
+ }
496
+ // console.log("Anchor - ", _anchor);
486
497
  this.update();
487
498
  this.visible = true;
488
499
  }
@@ -644,4 +655,11 @@ export class Transformer extends Pixi.Container {
644
655
  return this.target;
645
656
  }
646
657
  cleanup() { }
658
+ convertToLocal(point) {
659
+ if (this.target) {
660
+ const p = this.target.parent.toLocal(point);
661
+ return p;
662
+ }
663
+ return point;
664
+ }
647
665
  }
@@ -2,6 +2,7 @@ type Point = {
2
2
  x: number;
3
3
  y: number;
4
4
  };
5
+ import { Pixi } from "./pixi";
5
6
  /**
6
7
  *
7
8
  * @param origin source from where distance will be calculated
@@ -39,4 +40,13 @@ export declare function snapToEdgesAndCorners({ point, anchor, dim: { width, hei
39
40
  };
40
41
  anchor: Point;
41
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;
42
52
  export {};
@@ -1,3 +1,4 @@
1
+ import { Pixi } from "./pixi";
1
2
  /**
2
3
  *
3
4
  * @param origin source from where distance will be calculated
@@ -80,3 +81,35 @@ export function snapToEdgesAndCorners({ point, anchor, dim: { width, height }, s
80
81
  }
81
82
  return snappedPoint;
82
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 "./FreeTransformTool";
1
+ export { Transformer } from "./Transformer";
2
2
  export declare function hello(): void;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { Transformer } from "./FreeTransformTool";
1
+ export { Transformer } from "./Transformer";
2
2
  export function hello() {
3
3
  console.log("🧩 Hello, from @quinninc/pixi-transformer!");
4
4
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@quinninc/pixi-transformer",
3
3
  "private": false,
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "repository": {
6
6
  "url": "https://github.com/Quinn-Care-Private-Limited/pixi-transformer"
7
7
  },
@@ -14,6 +14,11 @@
14
14
  "package.json",
15
15
  "README.md"
16
16
  ],
17
+ "keywords": [
18
+ "pixi.js",
19
+ "transformer",
20
+ "pixi.js v8"
21
+ ],
17
22
  "author": {
18
23
  "name": "Rohit Kaushal",
19
24
  "url": "https://github.com/RohitKaushal7"