@quinninc/pixi-transformer 0.0.5 → 0.0.7
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 +4 -2
- package/dist/Transformer.d.ts +12 -2
- package/dist/Transformer.js +28 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@ Works with pixi.js v8 and above.
|
|
|
9
9
|
- Translate - Drag to move anywhere
|
|
10
10
|
- Scale - Scale on X, Y or both axis'
|
|
11
11
|
- Rotate - drag to rotate
|
|
12
|
-
- Anchor point - update anchor point of transforms (
|
|
13
|
-
- Snapping -
|
|
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
14
|
- Theming - custom border color, control size and color, and anchor point graphic
|
|
15
15
|
|
|
16
16
|
> Note - all transformations are applied wrt the pivot point, update pivot point to achieve the desired result.
|
|
@@ -57,5 +57,7 @@ obj.on("pointertap", (ev) => {
|
|
|
57
57
|
- `controlsSize` - (`number`) - handles' size in pixels. default - `10`
|
|
58
58
|
- `debug` - (`boolean`) - show scaling distance debug lines. default - `false`
|
|
59
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']`
|
|
60
62
|
|
|
61
63
|
**returns:** `PIXI.Container` (the select tool)
|
package/dist/Transformer.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Pixi } from "./pixi";
|
|
2
2
|
export declare class Transformer extends Pixi.Container {
|
|
3
3
|
private debug;
|
|
4
|
-
private
|
|
4
|
+
private _id;
|
|
5
5
|
private moveHandle;
|
|
6
6
|
private rotateHandle;
|
|
7
7
|
private scaleTHandle;
|
|
@@ -30,12 +30,16 @@ export declare class Transformer extends Pixi.Container {
|
|
|
30
30
|
private right;
|
|
31
31
|
private _anchor;
|
|
32
32
|
private dragging;
|
|
33
|
+
private snapKey;
|
|
34
|
+
private pivotKey;
|
|
33
35
|
/**
|
|
34
36
|
*
|
|
35
37
|
* @param p.lineColor - border line color
|
|
36
38
|
* @param p.handleColor - handles' fill color
|
|
37
39
|
* @param p.controlsSize - handles' size in pixels
|
|
38
40
|
* @param p.debug - show scaling distance debug lines
|
|
41
|
+
* @param p.snapKeys - key to hold to enable snapping rotation to 45 degrees // default ['ShiftLeft']
|
|
42
|
+
* @param p.pivotKeys - key to hold to move the pivot point of transformation // default ['AltLeft']
|
|
39
43
|
* @param p.generateAnchorMark - a function to generate your custom anchor mark
|
|
40
44
|
*
|
|
41
45
|
* @example
|
|
@@ -44,6 +48,8 @@ export declare class Transformer extends Pixi.Container {
|
|
|
44
48
|
* handleColor: 0xffffff,
|
|
45
49
|
* controlsSize: 10,
|
|
46
50
|
* debug: false,
|
|
51
|
+
* snapKeys: ['ShiftLeft'],
|
|
52
|
+
* pivotKeys: ['AltLeft'],
|
|
47
53
|
* generateAnchorMark: (g) => g.clear().circle(0,0,10).fill(0xff0000); // please use clear() to clear the geometry first every time. leave undefined for default anchor mark. set false to disable.
|
|
48
54
|
* });
|
|
49
55
|
*
|
|
@@ -53,16 +59,19 @@ export declare class Transformer extends Pixi.Container {
|
|
|
53
59
|
*
|
|
54
60
|
* selectTool.unselect();
|
|
55
61
|
*/
|
|
56
|
-
constructor({ id, lineColor, handleColor, controlsSize, debug, generateAnchorMark, }?: {
|
|
62
|
+
constructor({ id, lineColor, handleColor, controlsSize, debug, generateAnchorMark, snapKeys, pivotKeys, }?: {
|
|
57
63
|
id?: string;
|
|
58
64
|
lineColor?: string | number;
|
|
59
65
|
handleColor?: string | number;
|
|
60
66
|
controlsSize?: number;
|
|
61
67
|
debug?: boolean;
|
|
62
68
|
generateAnchorMark?: false | ((graphic: Pixi.Graphics) => void);
|
|
69
|
+
snapKeys?: string[];
|
|
70
|
+
pivotKeys?: string[];
|
|
63
71
|
});
|
|
64
72
|
private addToolTip;
|
|
65
73
|
private handleHandleEvents;
|
|
74
|
+
private handleKeyPresses;
|
|
66
75
|
private createHandle;
|
|
67
76
|
private createHandleIndicatorTo;
|
|
68
77
|
private deScale;
|
|
@@ -74,6 +83,7 @@ export declare class Transformer extends Pixi.Container {
|
|
|
74
83
|
setCursor(cursor: string): void;
|
|
75
84
|
private generateAnchorMark;
|
|
76
85
|
get _target(): Pixi.Container<Pixi.ContainerChild> | Pixi.Sprite | null;
|
|
86
|
+
get id(): string;
|
|
77
87
|
cleanup(): void;
|
|
78
88
|
private convertToLocal;
|
|
79
89
|
}
|
package/dist/Transformer.js
CHANGED
|
@@ -7,6 +7,8 @@ export class Transformer extends Pixi.Container {
|
|
|
7
7
|
* @param p.handleColor - handles' fill color
|
|
8
8
|
* @param p.controlsSize - handles' size in pixels
|
|
9
9
|
* @param p.debug - show scaling distance debug lines
|
|
10
|
+
* @param p.snapKeys - key to hold to enable snapping rotation to 45 degrees // default ['ShiftLeft']
|
|
11
|
+
* @param p.pivotKeys - key to hold to move the pivot point of transformation // default ['AltLeft']
|
|
10
12
|
* @param p.generateAnchorMark - a function to generate your custom anchor mark
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
@@ -15,6 +17,8 @@ export class Transformer extends Pixi.Container {
|
|
|
15
17
|
* handleColor: 0xffffff,
|
|
16
18
|
* controlsSize: 10,
|
|
17
19
|
* debug: false,
|
|
20
|
+
* snapKeys: ['ShiftLeft'],
|
|
21
|
+
* pivotKeys: ['AltLeft'],
|
|
18
22
|
* generateAnchorMark: (g) => g.clear().circle(0,0,10).fill(0xff0000); // please use clear() to clear the geometry first every time. leave undefined for default anchor mark. set false to disable.
|
|
19
23
|
* });
|
|
20
24
|
*
|
|
@@ -24,7 +28,7 @@ export class Transformer extends Pixi.Container {
|
|
|
24
28
|
*
|
|
25
29
|
* selectTool.unselect();
|
|
26
30
|
*/
|
|
27
|
-
constructor({ id = "transformer", lineColor = 0x66cfff, handleColor = 0xffffff, controlsSize = 10, debug = false, generateAnchorMark, } = {}) {
|
|
31
|
+
constructor({ id = "transformer", lineColor = 0x66cfff, handleColor = 0xffffff, controlsSize = 10, debug = false, generateAnchorMark, snapKeys = ["ShiftLeft"], pivotKeys = ["AltLeft"], } = {}) {
|
|
28
32
|
super();
|
|
29
33
|
// target Pixi.Container
|
|
30
34
|
this.target = null;
|
|
@@ -35,9 +39,11 @@ export class Transformer extends Pixi.Container {
|
|
|
35
39
|
this.right = 0;
|
|
36
40
|
this._anchor = new Pixi.Point(0, 0);
|
|
37
41
|
this.dragging = false;
|
|
42
|
+
this.snapKey = false;
|
|
43
|
+
this.pivotKey = false;
|
|
38
44
|
this.onUpdate = () => { };
|
|
39
45
|
const that = this;
|
|
40
|
-
this.
|
|
46
|
+
this._id = id;
|
|
41
47
|
this.debug = debug;
|
|
42
48
|
this.lineColor = lineColor;
|
|
43
49
|
this.handleColor = handleColor;
|
|
@@ -53,6 +59,9 @@ export class Transformer extends Pixi.Container {
|
|
|
53
59
|
this.zIndex = 1e7;
|
|
54
60
|
// Pixi.Container Properties
|
|
55
61
|
this.visible = false;
|
|
62
|
+
// keyPress states
|
|
63
|
+
this.handleKeyPresses("snapKey", snapKeys, that);
|
|
64
|
+
this.handleKeyPresses("pivotKey", pivotKeys, that);
|
|
56
65
|
// create border
|
|
57
66
|
this.border = new Pixi.Graphics({ label: "Border" });
|
|
58
67
|
this.addChild(this.border);
|
|
@@ -126,7 +135,7 @@ export class Transformer extends Pixi.Container {
|
|
|
126
135
|
this.addChild(this.moveHandle);
|
|
127
136
|
function changeAnchorPoint(ev) {
|
|
128
137
|
// console.log("TAPP this, that - ", this, that);
|
|
129
|
-
if (!
|
|
138
|
+
if (!that.pivotKey)
|
|
130
139
|
return;
|
|
131
140
|
if (!that.target)
|
|
132
141
|
return;
|
|
@@ -171,7 +180,7 @@ export class Transformer extends Pixi.Container {
|
|
|
171
180
|
// console.log("onMoveHandleDown this, that - ", this, that);
|
|
172
181
|
if (!that.target)
|
|
173
182
|
return;
|
|
174
|
-
if (
|
|
183
|
+
if (that.pivotKey) {
|
|
175
184
|
this.draggingPivot = true;
|
|
176
185
|
}
|
|
177
186
|
// data
|
|
@@ -354,7 +363,7 @@ export class Transformer extends Pixi.Container {
|
|
|
354
363
|
const endAngle = calcAngleRadians(relativeEndPoint.x, relativeEndPoint.y);
|
|
355
364
|
const deltaAngle = endAngle - startAngle;
|
|
356
365
|
let finalRotation = this.targetStartRotation + deltaAngle;
|
|
357
|
-
if (
|
|
366
|
+
if (that.snapKey) {
|
|
358
367
|
const snapAngle = 45;
|
|
359
368
|
let finalAngle = (finalRotation * 180) / Math.PI;
|
|
360
369
|
const proximity = Math.min(Math.abs(finalAngle % snapAngle), snapAngle - Math.abs(finalAngle % snapAngle));
|
|
@@ -430,6 +439,17 @@ export class Transformer extends Pixi.Container {
|
|
|
430
439
|
.on("pointerup", onHandleUp)
|
|
431
440
|
.on("pointerupoutside", onHandleUp);
|
|
432
441
|
}
|
|
442
|
+
handleKeyPresses(property, keyCodes, that) {
|
|
443
|
+
typeof window > "u" ||
|
|
444
|
+
(window.addEventListener("keydown", (e) => {
|
|
445
|
+
keyCodes.includes(e.code) && (that[property] = true);
|
|
446
|
+
keyCodes.includes(e.key) && (that[property] = true);
|
|
447
|
+
}),
|
|
448
|
+
window.addEventListener("keyup", (e) => {
|
|
449
|
+
keyCodes.includes(e.code) && (that[property] = false);
|
|
450
|
+
keyCodes.includes(e.key) && (that[property] = false);
|
|
451
|
+
}));
|
|
452
|
+
}
|
|
433
453
|
createHandle({ cursor, name, shape = "square", }) {
|
|
434
454
|
const handle = new Pixi.Graphics();
|
|
435
455
|
handle.label = this.id;
|
|
@@ -654,6 +674,9 @@ export class Transformer extends Pixi.Container {
|
|
|
654
674
|
get _target() {
|
|
655
675
|
return this.target;
|
|
656
676
|
}
|
|
677
|
+
get id() {
|
|
678
|
+
return this._id;
|
|
679
|
+
}
|
|
657
680
|
cleanup() { }
|
|
658
681
|
convertToLocal(point) {
|
|
659
682
|
if (this.target) {
|