@quinninc/pixi-transformer 0.1.1 → 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,35 +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
238
  if (wasDragging &&
262
239
  this.dragDistance > that.movedThreshold / scale &&
263
- that.target &&
264
- that.onMoveEndCallback) {
265
- console.log("Move ended, calling onMoveEndCallback (no args)");
266
- that.onMoveEndCallback();
240
+ that.target) {
241
+ that.emit("pixi-transformer-moveend");
267
242
  }
268
- // --- End Trigger ---
243
+ // --- End Emit ---
269
244
  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
245
+ upEvent.stopPropagation();
275
246
  }
276
247
  // ------------</ MoveTool>---------------
277
248
  // ------------<ScaleXYTool>---------------
@@ -287,14 +258,12 @@ export class Transformer extends Pixi.Container {
287
258
  .on("pointerup", onScaleXYToolUp);
288
259
  }
289
260
  function onScaleXYToolDown(downEvent) {
290
- // console.log("onScaleToolDown this, that - ", this, that);
291
261
  if (!that.target)
292
262
  return;
293
263
  this.targetResolutionStart = that.target.resolution;
294
264
  this.on("globalpointermove", onScaleXYToolMove);
295
265
  }
296
266
  function onScaleXYToolMove(moveEvent) {
297
- // console.log("onScaleToolMove this, that - ", this, that);
298
267
  if (!this.dragging || !that.target)
299
268
  return;
300
269
  const eventPoint = that.convertToLocal(moveEvent.global);
@@ -307,7 +276,6 @@ export class Transformer extends Pixi.Container {
307
276
  that.update();
308
277
  }
309
278
  function onScaleXYToolUp() {
310
- // console.log("onScaleToolUp this, that - ", this, that);
311
279
  if (that.target) {
312
280
  that.target.resolution =
313
281
  this.targetResolutionStart * this.rescaleFactor;
@@ -323,18 +291,15 @@ export class Transformer extends Pixi.Container {
323
291
  .on("pointerupoutside", onScaleYToolUp);
324
292
  }
325
293
  function onScaleYToolDown(downEvent) {
326
- // console.log("onVScaleToolDown this, that - ", this, that);
327
294
  if (!that.target)
328
295
  return;
329
296
  this.on("globalpointermove", onScaleYToolMove);
330
297
  }
331
298
  function onScaleYToolMove(moveEvent) {
332
- // console.log("onVScaleToolMove this - ", this);
333
299
  if (!this.dragging || !that.target)
334
300
  return;
335
301
  const eventPoint = that.convertToLocal(moveEvent.global);
336
302
  const referencePoint = that.target.position.clone();
337
- // referencePoint.x = that.target.position.x + that.target.width / 2;
338
303
  const distStart = calcDistance(this.eventStartPos, referencePoint);
339
304
  const distEnd = calcDistance(eventPoint, referencePoint);
340
305
  const direction = getDirection(referencePoint, this.eventStartPos, eventPoint);
@@ -343,7 +308,6 @@ export class Transformer extends Pixi.Container {
343
308
  that.update();
344
309
  }
345
310
  function onScaleYToolUp(upEvent) {
346
- // console.log("onVScaleToolUp this, that - ", this, that);
347
311
  this.off("globalpointermove", onScaleYToolMove);
348
312
  }
349
313
  // ------------</ ScaleYTool>---------------
@@ -356,18 +320,15 @@ export class Transformer extends Pixi.Container {
356
320
  }
357
321
  this.addChild(this.scaleRHandle);
358
322
  function onScaleXToolDown(downEvent) {
359
- // console.log("onHScaleToolDown this, that - ", this, that);
360
323
  if (!that.target)
361
324
  return;
362
325
  this.on("globalpointermove", onScaleXToolMove);
363
326
  }
364
327
  function onScaleXToolMove(moveEvent) {
365
- // console.log("onHScaleToolMove this - ", this);
366
328
  if (!this.dragging || !that.target)
367
329
  return;
368
330
  const eventPoint = that.convertToLocal(moveEvent.global);
369
331
  const referencePoint = that.target.position.clone();
370
- // referencePoint.y = that.target.position.y + that.target.height / 2;
371
332
  const distStart = calcDistance(this.eventStartPos, referencePoint);
372
333
  const distEnd = calcDistance(eventPoint, referencePoint);
373
334
  const direction = getDirection(referencePoint, this.eventStartPos, eventPoint);
@@ -376,7 +337,6 @@ export class Transformer extends Pixi.Container {
376
337
  that.update();
377
338
  }
378
339
  function onScaleXToolUp(upEvent) {
379
- // console.log("onHScaleToolUp this, that - ", this, that);
380
340
  this.off("globalpointermove", onScaleXToolMove);
381
341
  }
382
342
  // ------------</ ScaleXTool>---------------
@@ -386,13 +346,11 @@ export class Transformer extends Pixi.Container {
386
346
  .on("pointerup", onRotateToolUp)
387
347
  .on("pointerupoutside", onRotateToolUp);
388
348
  function onRotateToolDown(downEvent) {
389
- // console.log("onRotateToolDown this, that - ", this, that);
390
349
  if (!that.target)
391
350
  return;
392
351
  this.on("globalpointermove", onRotateToolMove);
393
352
  }
394
353
  function onRotateToolMove(moveEvent) {
395
- // console.log("onRotateToolMove this, that - ", this, that);
396
354
  if (!this.dragging || !that.target)
397
355
  return;
398
356
  const eventPoint = that.convertToLocal(moveEvent.global);
@@ -423,7 +381,6 @@ export class Transformer extends Pixi.Container {
423
381
  that.update();
424
382
  }
425
383
  function onRotateToolUp(upEvent) {
426
- // console.log("onRotateToolUp this, that - ", this, that);
427
384
  this.off("globalpointermove", onRotateToolMove);
428
385
  }
429
386
  // ------------</ RotateTool>---------------
@@ -447,9 +404,7 @@ export class Transformer extends Pixi.Container {
447
404
  });
448
405
  }
449
406
  handleHandleEvents(handle, that) {
450
- // console.log("handleHandleEvents this, handle - ", this, handle.label);
451
407
  function onHandleDown(e) {
452
- // console.log("onHandleDown this - ", this);
453
408
  that.dragging = true;
454
409
  handle.dragging = true;
455
410
  handle.eventStartPos = that.convertToLocal(e.global.clone());
@@ -462,7 +417,6 @@ export class Transformer extends Pixi.Container {
462
417
  }
463
418
  }
464
419
  function onHandleMove(e) {
465
- // console.log("onHandleMove this - ", this);
466
420
  const eventPoint = that.convertToLocal(e.global.clone());
467
421
  if (that.debug) {
468
422
  that.createHandleIndicatorTo(that.toPoint, eventPoint.x, eventPoint.y);
@@ -470,7 +424,6 @@ export class Transformer extends Pixi.Container {
470
424
  }
471
425
  }
472
426
  function onHandleUp() {
473
- // console.log("onHandleUp this - ", this);
474
427
  that.update();
475
428
  handle.off("globalpointermove", onHandleMove);
476
429
  if (that.debug) {
@@ -550,25 +503,19 @@ export class Transformer extends Pixi.Container {
550
503
  if (this.target) {
551
504
  this.unselect();
552
505
  }
553
- // console.log("select this - ", this);
554
506
  this.target = target;
555
507
  let _anchor;
556
508
  if (this.target.pivot) {
557
- // console.log("target has pivot", this.target.pivot);
558
- // 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
559
509
  _anchor = new Pixi.Point(this.target.pivot.x / Math.abs(this.target.width / this.target.scale.x), this.target.pivot.y /
560
510
  Math.abs(this.target.height / this.target.scale.y));
561
511
  }
562
512
  else {
563
- // console.log("no-pivot|no-anchor", this.target.pivot);
564
513
  _anchor = new Pixi.Point(0.5, 0.5);
565
514
  }
566
515
  this._anchor = _anchor;
567
516
  if (this.parent !== this.target.parent) {
568
- // console.log("Different parent - switching transformer location position");
569
517
  this.target.parent.addChild(this);
570
518
  }
571
- // console.log("Anchor - ", _anchor);
572
519
  this.update();
573
520
  this.visible = true;
574
521
  }
@@ -584,7 +531,6 @@ export class Transformer extends Pixi.Container {
584
531
  }
585
532
  update() {
586
533
  if (!this.target) {
587
- // console.log("no target, returning...");
588
534
  return;
589
535
  }
590
536
  // copy object translation/transformation
@@ -598,14 +544,12 @@ export class Transformer extends Pixi.Container {
598
544
  this.rotation = this.target.rotation;
599
545
  let _anchor;
600
546
  if (this.target.pivot) {
601
- // console.log("target has pivot", this.target.pivot);
602
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));
603
548
  }
604
549
  else {
605
550
  _anchor = this._anchor || new Pixi.Point(0.5, 0.5);
606
551
  }
607
552
  this._anchor = _anchor;
608
- // console.log("Anchor - ", _anchor);
609
553
  this.left = -bounds.width * _anchor.x;
610
554
  this.top = -bounds.height * _anchor.y;
611
555
  this.bottom = bounds.height * (1 - _anchor.y);
@@ -613,7 +557,6 @@ export class Transformer extends Pixi.Container {
613
557
  // anchor mark
614
558
  this.generateAnchorMark?.(this.anchorMark);
615
559
  this.deScale(this.anchorMark);
616
- // this.anchorMark!.rotation = -this.rotation;
617
560
  const rotationHandlePos = {
618
561
  x: (this.left + this.right) / 2,
619
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.1",
4
+ "version": "0.1.3",
5
5
  "repository": {
6
6
  "url": "https://github.com/Quinn-Care-Private-Limited/pixi-transformer"
7
7
  },