@quinninc/pixi-transformer 0.1.2 → 0.1.3

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.
@@ -19,7 +19,6 @@ export declare class Transformer extends Pixi.Container {
19
19
  private target;
20
20
  private movedThreshold;
21
21
  private updateCallbacks;
22
- private onMoveEndCallback?;
23
22
  private handleOpacity;
24
23
  private controlsSize;
25
24
  private controlsDimOpacity;
@@ -61,7 +60,7 @@ export declare class Transformer extends Pixi.Container {
61
60
  *
62
61
  * selectTool.unselect();
63
62
  */
64
- constructor({ id, lineColor, handleColor, controlsSize, debug, generateAnchorMark, snapKeys, pivotKeys, onMoveEnd, }?: {
63
+ constructor({ id, lineColor, handleColor, controlsSize, debug, generateAnchorMark, snapKeys, pivotKeys, }?: {
65
64
  id?: string;
66
65
  lineColor?: string | number;
67
66
  handleColor?: string | number;
@@ -70,7 +69,6 @@ export declare class Transformer extends Pixi.Container {
70
69
  generateAnchorMark?: false | ((graphic: Pixi.Graphics) => void);
71
70
  snapKeys?: string[];
72
71
  pivotKeys?: string[];
73
- onMoveEnd?: () => void;
74
72
  });
75
73
  private addToolTip;
76
74
  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"], onMoveEnd, } = {}) {
31
+ constructor({ id = "transformer", lineColor = 0x66cfff, handleColor = 0xffffff, controlsSize = 10, debug = false, generateAnchorMark, snapKeys = ["ShiftLeft"], pivotKeys = ["AltLeft"], } = {}) {
32
32
  super();
33
33
  // target Pixi.Container
34
34
  this.target = null;
@@ -65,7 +65,6 @@ 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;
69
68
  this.zIndex = 1e7;
70
69
  // Pixi.Container Properties
71
70
  this.visible = false;
@@ -144,24 +143,11 @@ export class Transformer extends Pixi.Container {
144
143
  this.moveHandle.hitArea = new Pixi.Rectangle(0, 0, this.controlsSize, this.controlsSize);
145
144
  this.addChild(this.moveHandle);
146
145
  function changeAnchorPoint(ev) {
147
- // console.log("TAPP this, that - ", this, that);
148
146
  if (!that.pivotKey)
149
147
  return;
150
148
  if (!that.target)
151
149
  return;
152
150
  const eventPoint = that.convertToLocal(ev.global);
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
- // );
165
151
  let dx, dy;
166
152
  // calc new pivot point
167
153
  // step 1 - translate to origin
@@ -177,11 +163,9 @@ export class Transformer extends Pixi.Container {
177
163
  dim: { width: that.target.width, height: that.target.height },
178
164
  anchor: that._anchor,
179
165
  });
180
- // console.log("UP SNAPPING - ", sp, pSnapped);
181
166
  // step 3 - scale by 1/scale
182
167
  rdx = pSnapped.x / that.target.scale.x;
183
168
  rdy = pSnapped.y / that.target.scale.y;
184
- // console.log("3 - dx, dy - ", dx, dy);
185
169
  // step 4 - translate to new pivot
186
170
  const x = that.target.pivot.x + rdx;
187
171
  const y = that.target.pivot.y + rdy;
@@ -195,7 +179,6 @@ export class Transformer extends Pixi.Container {
195
179
  that.target.position.set(posX, posY);
196
180
  }
197
181
  function onMoveHandleDown(downEvent) {
198
- // console.log("onMoveHandleDown this, that - ", this, that);
199
182
  if (!that.target)
200
183
  return;
201
184
  // Store target's starting position for move calculation
@@ -213,7 +196,6 @@ export class Transformer extends Pixi.Container {
213
196
  this.on("globalpointermove", onMoveHandleMove);
214
197
  }
215
198
  function onMoveHandleMove(moveEvent) {
216
- // console.log("onMoveHandleMove this, that - ", this, that);
217
199
  if (!this.dragging || !that.target)
218
200
  return;
219
201
  const eventPoint = that.convertToLocal(moveEvent.global);
@@ -230,7 +212,6 @@ export class Transformer extends Pixi.Container {
230
212
  },
231
213
  anchor: that._anchor,
232
214
  });
233
- // console.log("MOVE SNAPPING - ", p, snappedPoint);
234
215
  that.anchorMark.position.set(snappedPoint.x / that.target.scale.x, snappedPoint.y / that.target.scale.y);
235
216
  return;
236
217
  }
@@ -243,37 +224,25 @@ export class Transformer extends Pixi.Container {
243
224
  moveEvent.stopPropagation();
244
225
  }
245
226
  function onMoveHandleUp(upEvent) {
246
- // console.log("onMoveHandleUp this, that - ", this, that);
247
- const wasDragging = this.dragging; // Capture state before resetting
227
+ const wasDragging = this.dragging;
248
228
  // Reset state for this handle instance first
249
229
  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)
230
+ this.dragging = false;
252
231
  if (this.draggingPivot) {
253
232
  changeAnchorPoint(upEvent);
254
233
  that.anchorMark.position.set(0, 0);
255
234
  this.draggingPivot = false;
256
- // Maybe call update here if pivot change needs immediate visual update?
257
- // that.update();
258
235
  }
259
- // --- Trigger onMoveEnd Callback ---
236
+ // --- Emit Move End Event ---
260
237
  const scale = Math.min(that.scale.x, that.scale.y) || 1;
261
- console.log("this.dragDistance - ", this.dragDistance);
262
- console.log("that.movedThreshold / scale - ", that.movedThreshold / scale);
263
238
  if (wasDragging &&
264
239
  this.dragDistance > that.movedThreshold / scale &&
265
- that.target &&
266
- that.onMoveEndCallback) {
267
- console.log("Move ended, calling onMoveEndCallback (no args)");
268
- that.onMoveEndCallback();
240
+ that.target) {
241
+ that.emit("pixi-transformer-moveend");
269
242
  }
270
- // --- End Trigger ---
243
+ // --- End Emit ---
271
244
  this.dragDistance = 0;
272
- // Note: The generic onHandleUp from handleHandleEvents will also fire
273
- // and call updateCallbacks. Ensure order is okay or adjust if needed.
274
- // If the generic one shouldn't fire its updateCallbacks for this handle,
275
- // you might need to structure handleHandleEvents differently.
276
- upEvent.stopPropagation(); // Prevent event bubbling further if needed
245
+ upEvent.stopPropagation();
277
246
  }
278
247
  // ------------</ MoveTool>---------------
279
248
  // ------------<ScaleXYTool>---------------
@@ -289,14 +258,12 @@ export class Transformer extends Pixi.Container {
289
258
  .on("pointerup", onScaleXYToolUp);
290
259
  }
291
260
  function onScaleXYToolDown(downEvent) {
292
- // console.log("onScaleToolDown this, that - ", this, that);
293
261
  if (!that.target)
294
262
  return;
295
263
  this.targetResolutionStart = that.target.resolution;
296
264
  this.on("globalpointermove", onScaleXYToolMove);
297
265
  }
298
266
  function onScaleXYToolMove(moveEvent) {
299
- // console.log("onScaleToolMove this, that - ", this, that);
300
267
  if (!this.dragging || !that.target)
301
268
  return;
302
269
  const eventPoint = that.convertToLocal(moveEvent.global);
@@ -309,7 +276,6 @@ export class Transformer extends Pixi.Container {
309
276
  that.update();
310
277
  }
311
278
  function onScaleXYToolUp() {
312
- // console.log("onScaleToolUp this, that - ", this, that);
313
279
  if (that.target) {
314
280
  that.target.resolution =
315
281
  this.targetResolutionStart * this.rescaleFactor;
@@ -325,18 +291,15 @@ export class Transformer extends Pixi.Container {
325
291
  .on("pointerupoutside", onScaleYToolUp);
326
292
  }
327
293
  function onScaleYToolDown(downEvent) {
328
- // console.log("onVScaleToolDown this, that - ", this, that);
329
294
  if (!that.target)
330
295
  return;
331
296
  this.on("globalpointermove", onScaleYToolMove);
332
297
  }
333
298
  function onScaleYToolMove(moveEvent) {
334
- // console.log("onVScaleToolMove this - ", this);
335
299
  if (!this.dragging || !that.target)
336
300
  return;
337
301
  const eventPoint = that.convertToLocal(moveEvent.global);
338
302
  const referencePoint = that.target.position.clone();
339
- // referencePoint.x = that.target.position.x + that.target.width / 2;
340
303
  const distStart = calcDistance(this.eventStartPos, referencePoint);
341
304
  const distEnd = calcDistance(eventPoint, referencePoint);
342
305
  const direction = getDirection(referencePoint, this.eventStartPos, eventPoint);
@@ -345,7 +308,6 @@ export class Transformer extends Pixi.Container {
345
308
  that.update();
346
309
  }
347
310
  function onScaleYToolUp(upEvent) {
348
- // console.log("onVScaleToolUp this, that - ", this, that);
349
311
  this.off("globalpointermove", onScaleYToolMove);
350
312
  }
351
313
  // ------------</ ScaleYTool>---------------
@@ -358,18 +320,15 @@ export class Transformer extends Pixi.Container {
358
320
  }
359
321
  this.addChild(this.scaleRHandle);
360
322
  function onScaleXToolDown(downEvent) {
361
- // console.log("onHScaleToolDown this, that - ", this, that);
362
323
  if (!that.target)
363
324
  return;
364
325
  this.on("globalpointermove", onScaleXToolMove);
365
326
  }
366
327
  function onScaleXToolMove(moveEvent) {
367
- // console.log("onHScaleToolMove this - ", this);
368
328
  if (!this.dragging || !that.target)
369
329
  return;
370
330
  const eventPoint = that.convertToLocal(moveEvent.global);
371
331
  const referencePoint = that.target.position.clone();
372
- // referencePoint.y = that.target.position.y + that.target.height / 2;
373
332
  const distStart = calcDistance(this.eventStartPos, referencePoint);
374
333
  const distEnd = calcDistance(eventPoint, referencePoint);
375
334
  const direction = getDirection(referencePoint, this.eventStartPos, eventPoint);
@@ -378,7 +337,6 @@ export class Transformer extends Pixi.Container {
378
337
  that.update();
379
338
  }
380
339
  function onScaleXToolUp(upEvent) {
381
- // console.log("onHScaleToolUp this, that - ", this, that);
382
340
  this.off("globalpointermove", onScaleXToolMove);
383
341
  }
384
342
  // ------------</ ScaleXTool>---------------
@@ -388,13 +346,11 @@ export class Transformer extends Pixi.Container {
388
346
  .on("pointerup", onRotateToolUp)
389
347
  .on("pointerupoutside", onRotateToolUp);
390
348
  function onRotateToolDown(downEvent) {
391
- // console.log("onRotateToolDown this, that - ", this, that);
392
349
  if (!that.target)
393
350
  return;
394
351
  this.on("globalpointermove", onRotateToolMove);
395
352
  }
396
353
  function onRotateToolMove(moveEvent) {
397
- // console.log("onRotateToolMove this, that - ", this, that);
398
354
  if (!this.dragging || !that.target)
399
355
  return;
400
356
  const eventPoint = that.convertToLocal(moveEvent.global);
@@ -425,7 +381,6 @@ export class Transformer extends Pixi.Container {
425
381
  that.update();
426
382
  }
427
383
  function onRotateToolUp(upEvent) {
428
- // console.log("onRotateToolUp this, that - ", this, that);
429
384
  this.off("globalpointermove", onRotateToolMove);
430
385
  }
431
386
  // ------------</ RotateTool>---------------
@@ -449,9 +404,7 @@ export class Transformer extends Pixi.Container {
449
404
  });
450
405
  }
451
406
  handleHandleEvents(handle, that) {
452
- // console.log("handleHandleEvents this, handle - ", this, handle.label);
453
407
  function onHandleDown(e) {
454
- // console.log("onHandleDown this - ", this);
455
408
  that.dragging = true;
456
409
  handle.dragging = true;
457
410
  handle.eventStartPos = that.convertToLocal(e.global.clone());
@@ -464,7 +417,6 @@ export class Transformer extends Pixi.Container {
464
417
  }
465
418
  }
466
419
  function onHandleMove(e) {
467
- // console.log("onHandleMove this - ", this);
468
420
  const eventPoint = that.convertToLocal(e.global.clone());
469
421
  if (that.debug) {
470
422
  that.createHandleIndicatorTo(that.toPoint, eventPoint.x, eventPoint.y);
@@ -472,7 +424,6 @@ export class Transformer extends Pixi.Container {
472
424
  }
473
425
  }
474
426
  function onHandleUp() {
475
- // console.log("onHandleUp this - ", this);
476
427
  that.update();
477
428
  handle.off("globalpointermove", onHandleMove);
478
429
  if (that.debug) {
@@ -552,25 +503,19 @@ export class Transformer extends Pixi.Container {
552
503
  if (this.target) {
553
504
  this.unselect();
554
505
  }
555
- // console.log("select this - ", this);
556
506
  this.target = target;
557
507
  let _anchor;
558
508
  if (this.target.pivot) {
559
- // console.log("target has pivot", this.target.pivot);
560
- // when you set a width of 100 on a sprites with image with actual width of 500, the sprite container is scaled down to 0.2 by default
561
509
  _anchor = new Pixi.Point(this.target.pivot.x / Math.abs(this.target.width / this.target.scale.x), this.target.pivot.y /
562
510
  Math.abs(this.target.height / this.target.scale.y));
563
511
  }
564
512
  else {
565
- // console.log("no-pivot|no-anchor", this.target.pivot);
566
513
  _anchor = new Pixi.Point(0.5, 0.5);
567
514
  }
568
515
  this._anchor = _anchor;
569
516
  if (this.parent !== this.target.parent) {
570
- // console.log("Different parent - switching transformer location position");
571
517
  this.target.parent.addChild(this);
572
518
  }
573
- // console.log("Anchor - ", _anchor);
574
519
  this.update();
575
520
  this.visible = true;
576
521
  }
@@ -586,7 +531,6 @@ export class Transformer extends Pixi.Container {
586
531
  }
587
532
  update() {
588
533
  if (!this.target) {
589
- // console.log("no target, returning...");
590
534
  return;
591
535
  }
592
536
  // copy object translation/transformation
@@ -600,14 +544,12 @@ export class Transformer extends Pixi.Container {
600
544
  this.rotation = this.target.rotation;
601
545
  let _anchor;
602
546
  if (this.target.pivot) {
603
- // console.log("target has pivot", this.target.pivot);
604
547
  _anchor = new Pixi.Point(this.target.pivot.x / Math.abs(this.target.width / this.scale.x), this.target.pivot.y / Math.abs(this.target.height / this.scale.y));
605
548
  }
606
549
  else {
607
550
  _anchor = this._anchor || new Pixi.Point(0.5, 0.5);
608
551
  }
609
552
  this._anchor = _anchor;
610
- // console.log("Anchor - ", _anchor);
611
553
  this.left = -bounds.width * _anchor.x;
612
554
  this.top = -bounds.height * _anchor.y;
613
555
  this.bottom = bounds.height * (1 - _anchor.y);
@@ -615,7 +557,6 @@ export class Transformer extends Pixi.Container {
615
557
  // anchor mark
616
558
  this.generateAnchorMark?.(this.anchorMark);
617
559
  this.deScale(this.anchorMark);
618
- // this.anchorMark!.rotation = -this.rotation;
619
560
  const rotationHandlePos = {
620
561
  x: (this.left + this.right) / 2,
621
562
  y: this.top - 80 / Math.abs(this.scale.y),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@quinninc/pixi-transformer",
3
3
  "private": false,
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "repository": {
6
6
  "url": "https://github.com/Quinn-Care-Private-Limited/pixi-transformer"
7
7
  },