konva 8.1.4
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/CHANGELOG.md +1140 -0
- package/LICENSE +22 -0
- package/README.md +189 -0
- package/konva.js +18090 -0
- package/konva.min.js +12 -0
- package/lib/Animation.d.ts +24 -0
- package/lib/Animation.js +141 -0
- package/lib/Canvas.d.ts +32 -0
- package/lib/Canvas.js +107 -0
- package/lib/Container.d.ts +47 -0
- package/lib/Container.js +318 -0
- package/lib/Context.d.ts +76 -0
- package/lib/Context.js +520 -0
- package/lib/Core.d.ts +3 -0
- package/lib/Core.js +3 -0
- package/lib/DragAndDrop.d.ts +17 -0
- package/lib/DragAndDrop.js +104 -0
- package/lib/Factory.d.ts +11 -0
- package/lib/Factory.js +117 -0
- package/lib/FastLayer.d.ts +4 -0
- package/lib/FastLayer.js +12 -0
- package/lib/Global.d.ts +30 -0
- package/lib/Global.js +53 -0
- package/lib/Group.d.ts +6 -0
- package/lib/Group.js +13 -0
- package/lib/Layer.d.ts +60 -0
- package/lib/Layer.js +301 -0
- package/lib/Node.d.ts +318 -0
- package/lib/Node.js +1381 -0
- package/lib/PointerEvents.d.ts +11 -0
- package/lib/PointerEvents.js +37 -0
- package/lib/Shape.d.ts +184 -0
- package/lib/Shape.js +515 -0
- package/lib/Stage.d.ts +85 -0
- package/lib/Stage.js +687 -0
- package/lib/Tween.d.ts +86 -0
- package/lib/Tween.js +516 -0
- package/lib/Util.d.ts +87 -0
- package/lib/Util.js +742 -0
- package/lib/Validators.d.ts +11 -0
- package/lib/Validators.js +171 -0
- package/lib/_CoreInternals.d.ts +144 -0
- package/lib/_CoreInternals.js +34 -0
- package/lib/_FullInternals.d.ts +188 -0
- package/lib/_FullInternals.js +78 -0
- package/lib/filters/Blur.d.ts +2 -0
- package/lib/filters/Blur.js +697 -0
- package/lib/filters/Brighten.d.ts +2 -0
- package/lib/filters/Brighten.js +12 -0
- package/lib/filters/Contrast.d.ts +2 -0
- package/lib/filters/Contrast.js +34 -0
- package/lib/filters/Emboss.d.ts +2 -0
- package/lib/filters/Emboss.js +102 -0
- package/lib/filters/Enhance.d.ts +2 -0
- package/lib/filters/Enhance.js +83 -0
- package/lib/filters/Grayscale.d.ts +2 -0
- package/lib/filters/Grayscale.js +9 -0
- package/lib/filters/HSL.d.ts +2 -0
- package/lib/filters/HSL.js +24 -0
- package/lib/filters/HSV.d.ts +2 -0
- package/lib/filters/HSV.js +24 -0
- package/lib/filters/Invert.d.ts +2 -0
- package/lib/filters/Invert.js +8 -0
- package/lib/filters/Kaleidoscope.d.ts +2 -0
- package/lib/filters/Kaleidoscope.js +132 -0
- package/lib/filters/Mask.d.ts +2 -0
- package/lib/filters/Mask.js +140 -0
- package/lib/filters/Noise.d.ts +2 -0
- package/lib/filters/Noise.js +12 -0
- package/lib/filters/Pixelate.d.ts +2 -0
- package/lib/filters/Pixelate.js +60 -0
- package/lib/filters/Posterize.d.ts +2 -0
- package/lib/filters/Posterize.js +10 -0
- package/lib/filters/RGB.d.ts +2 -0
- package/lib/filters/RGB.js +39 -0
- package/lib/filters/RGBA.d.ts +2 -0
- package/lib/filters/RGBA.js +49 -0
- package/lib/filters/Sepia.d.ts +2 -0
- package/lib/filters/Sepia.js +11 -0
- package/lib/filters/Solarize.d.ts +2 -0
- package/lib/filters/Solarize.js +25 -0
- package/lib/filters/Threshold.d.ts +2 -0
- package/lib/filters/Threshold.js +10 -0
- package/lib/index-node.d.ts +2 -0
- package/lib/index-node.js +18 -0
- package/lib/index-types.d.ts +179 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/shapes/Arc.d.ts +19 -0
- package/lib/shapes/Arc.js +35 -0
- package/lib/shapes/Arrow.d.ts +24 -0
- package/lib/shapes/Arrow.js +94 -0
- package/lib/shapes/Circle.d.ts +13 -0
- package/lib/shapes/Circle.js +33 -0
- package/lib/shapes/Ellipse.d.ts +16 -0
- package/lib/shapes/Ellipse.js +37 -0
- package/lib/shapes/Image.d.ts +23 -0
- package/lib/shapes/Image.js +100 -0
- package/lib/shapes/Label.d.ts +35 -0
- package/lib/shapes/Label.js +166 -0
- package/lib/shapes/Line.d.ts +28 -0
- package/lib/shapes/Line.js +155 -0
- package/lib/shapes/Path.d.ts +37 -0
- package/lib/shapes/Path.js +627 -0
- package/lib/shapes/Rect.d.ts +9 -0
- package/lib/shapes/Rect.js +42 -0
- package/lib/shapes/RegularPolygon.d.ts +22 -0
- package/lib/shapes/RegularPolygon.js +65 -0
- package/lib/shapes/Ring.d.ts +16 -0
- package/lib/shapes/Ring.js +33 -0
- package/lib/shapes/Sprite.d.ts +30 -0
- package/lib/shapes/Sprite.js +108 -0
- package/lib/shapes/Star.d.ts +17 -0
- package/lib/shapes/Star.js +38 -0
- package/lib/shapes/Text.d.ts +59 -0
- package/lib/shapes/Text.js +347 -0
- package/lib/shapes/TextPath.d.ts +54 -0
- package/lib/shapes/TextPath.js +343 -0
- package/lib/shapes/Transformer.d.ts +112 -0
- package/lib/shapes/Transformer.js +944 -0
- package/lib/shapes/Wedge.d.ts +17 -0
- package/lib/shapes/Wedge.js +38 -0
- package/lib/types.d.ts +50 -0
- package/lib/types.js +22 -0
- package/package.json +111 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Transform } from '../Util';
|
|
2
|
+
import { Node } from '../Node';
|
|
3
|
+
import { Rect } from './Rect';
|
|
4
|
+
import { Group } from '../Group';
|
|
5
|
+
import { ContainerConfig } from '../Container';
|
|
6
|
+
import { GetSet, IRect, Vector2d } from '../types';
|
|
7
|
+
export interface Box extends IRect {
|
|
8
|
+
rotation: number;
|
|
9
|
+
}
|
|
10
|
+
export interface TransformerConfig extends ContainerConfig {
|
|
11
|
+
resizeEnabled?: boolean;
|
|
12
|
+
rotateEnabled?: boolean;
|
|
13
|
+
rotationSnaps?: Array<number>;
|
|
14
|
+
rotationSnapTolerance?: number;
|
|
15
|
+
rotateAnchorOffset?: number;
|
|
16
|
+
borderEnabled?: boolean;
|
|
17
|
+
borderStroke?: string;
|
|
18
|
+
borderStrokeWidth?: number;
|
|
19
|
+
borderDash?: Array<number>;
|
|
20
|
+
anchorFill?: string;
|
|
21
|
+
anchorStroke?: string;
|
|
22
|
+
anchorStrokeWidth?: number;
|
|
23
|
+
anchorSize?: number;
|
|
24
|
+
anchorCornerRadius?: number;
|
|
25
|
+
keepRatio?: boolean;
|
|
26
|
+
centeredScaling?: boolean;
|
|
27
|
+
enabledAnchors?: Array<string>;
|
|
28
|
+
flipEnabled?: boolean;
|
|
29
|
+
node?: Rect;
|
|
30
|
+
ignoreStroke?: boolean;
|
|
31
|
+
boundBoxFunc?: (oldBox: Box, newBox: Box) => Box;
|
|
32
|
+
useSingleNodeRotation?: boolean;
|
|
33
|
+
shouldOverdrawWholeArea?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare class Transformer extends Group {
|
|
36
|
+
_nodes: Array<Node>;
|
|
37
|
+
_movingAnchorName: string;
|
|
38
|
+
_transforming: boolean;
|
|
39
|
+
_anchorDragOffset: Vector2d;
|
|
40
|
+
sin: number;
|
|
41
|
+
cos: number;
|
|
42
|
+
_cursorChange: boolean;
|
|
43
|
+
constructor(config?: TransformerConfig);
|
|
44
|
+
attachTo(node: any): this;
|
|
45
|
+
setNode(node: any): this;
|
|
46
|
+
getNode(): Node<import("../Node").NodeConfig>;
|
|
47
|
+
setNodes(nodes?: Array<Node>): this;
|
|
48
|
+
_proxyDrag(node: Node): void;
|
|
49
|
+
getNodes(): Node<import("../Node").NodeConfig>[];
|
|
50
|
+
getActiveAnchor(): string;
|
|
51
|
+
detach(): void;
|
|
52
|
+
_resetTransformCache(): void;
|
|
53
|
+
_getNodeRect(): any;
|
|
54
|
+
__getNodeShape(node: Node, rot?: number, relative?: Node): {
|
|
55
|
+
rotation: number;
|
|
56
|
+
x: number;
|
|
57
|
+
y: number;
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
};
|
|
61
|
+
__getNodeRect(): {
|
|
62
|
+
x: number;
|
|
63
|
+
y: number;
|
|
64
|
+
width: number;
|
|
65
|
+
height: number;
|
|
66
|
+
rotation: number;
|
|
67
|
+
};
|
|
68
|
+
getX(): any;
|
|
69
|
+
getY(): any;
|
|
70
|
+
getWidth(): any;
|
|
71
|
+
getHeight(): any;
|
|
72
|
+
_createElements(): void;
|
|
73
|
+
_createAnchor(name: any): void;
|
|
74
|
+
_createBack(): void;
|
|
75
|
+
_handleMouseDown(e: any): void;
|
|
76
|
+
_handleMouseMove(e: any): void;
|
|
77
|
+
_handleMouseUp(e: any): void;
|
|
78
|
+
getAbsoluteTransform(): Transform;
|
|
79
|
+
_removeEvents(e?: any): void;
|
|
80
|
+
_fitNodesInto(newAttrs: any, evt?: any): void;
|
|
81
|
+
forceUpdate(): void;
|
|
82
|
+
_batchChangeChild(selector: string, attrs: any): void;
|
|
83
|
+
update(): void;
|
|
84
|
+
isTransforming(): boolean;
|
|
85
|
+
stopTransform(): void;
|
|
86
|
+
destroy(): this;
|
|
87
|
+
toObject(): any;
|
|
88
|
+
nodes: GetSet<Node[], this>;
|
|
89
|
+
enabledAnchors: GetSet<string[], this>;
|
|
90
|
+
rotationSnaps: GetSet<number[], this>;
|
|
91
|
+
anchorSize: GetSet<number, this>;
|
|
92
|
+
resizeEnabled: GetSet<boolean, this>;
|
|
93
|
+
rotateEnabled: GetSet<boolean, this>;
|
|
94
|
+
rotateAnchorOffset: GetSet<number, this>;
|
|
95
|
+
rotationSnapTolerance: GetSet<number, this>;
|
|
96
|
+
padding: GetSet<number, this>;
|
|
97
|
+
borderEnabled: GetSet<boolean, this>;
|
|
98
|
+
borderStroke: GetSet<string, this>;
|
|
99
|
+
borderStrokeWidth: GetSet<number, this>;
|
|
100
|
+
borderDash: GetSet<number[], this>;
|
|
101
|
+
anchorFill: GetSet<string, this>;
|
|
102
|
+
anchorStroke: GetSet<string, this>;
|
|
103
|
+
anchorCornerRadius: GetSet<number, this>;
|
|
104
|
+
anchorStrokeWidth: GetSet<number, this>;
|
|
105
|
+
keepRatio: GetSet<boolean, this>;
|
|
106
|
+
centeredScaling: GetSet<boolean, this>;
|
|
107
|
+
flipEnabled: GetSet<boolean, this>;
|
|
108
|
+
ignoreStroke: GetSet<boolean, this>;
|
|
109
|
+
boundBoxFunc: GetSet<(oldBox: Box, newBox: Box) => Box, this>;
|
|
110
|
+
shouldOverdrawWholeArea: GetSet<boolean, this>;
|
|
111
|
+
useSingleNodeRotation: GetSet<boolean, this>;
|
|
112
|
+
}
|
|
@@ -0,0 +1,944 @@
|
|
|
1
|
+
import { Util, Transform } from '../Util.js';
|
|
2
|
+
import { Factory } from '../Factory.js';
|
|
3
|
+
import { Node } from '../Node.js';
|
|
4
|
+
import { Shape } from '../Shape.js';
|
|
5
|
+
import { Rect } from './Rect.js';
|
|
6
|
+
import { Group } from '../Group.js';
|
|
7
|
+
import { Konva } from '../Global.js';
|
|
8
|
+
import { getBooleanValidator, getNumberValidator } from '../Validators.js';
|
|
9
|
+
import { _registerNode } from '../Global.js';
|
|
10
|
+
var EVENTS_NAME = 'tr-konva';
|
|
11
|
+
var ATTR_CHANGE_LIST = [
|
|
12
|
+
'resizeEnabledChange',
|
|
13
|
+
'rotateAnchorOffsetChange',
|
|
14
|
+
'rotateEnabledChange',
|
|
15
|
+
'enabledAnchorsChange',
|
|
16
|
+
'anchorSizeChange',
|
|
17
|
+
'borderEnabledChange',
|
|
18
|
+
'borderStrokeChange',
|
|
19
|
+
'borderStrokeWidthChange',
|
|
20
|
+
'borderDashChange',
|
|
21
|
+
'anchorStrokeChange',
|
|
22
|
+
'anchorStrokeWidthChange',
|
|
23
|
+
'anchorFillChange',
|
|
24
|
+
'anchorCornerRadiusChange',
|
|
25
|
+
'ignoreStrokeChange',
|
|
26
|
+
]
|
|
27
|
+
.map((e) => e + `.${EVENTS_NAME}`)
|
|
28
|
+
.join(' ');
|
|
29
|
+
var NODES_RECT = 'nodesRect';
|
|
30
|
+
var TRANSFORM_CHANGE_STR = [
|
|
31
|
+
'widthChange',
|
|
32
|
+
'heightChange',
|
|
33
|
+
'scaleXChange',
|
|
34
|
+
'scaleYChange',
|
|
35
|
+
'skewXChange',
|
|
36
|
+
'skewYChange',
|
|
37
|
+
'rotationChange',
|
|
38
|
+
'offsetXChange',
|
|
39
|
+
'offsetYChange',
|
|
40
|
+
'transformsEnabledChange',
|
|
41
|
+
'strokeWidthChange',
|
|
42
|
+
]
|
|
43
|
+
.map((e) => e + `.${EVENTS_NAME}`)
|
|
44
|
+
.join(' ');
|
|
45
|
+
var ANGLES = {
|
|
46
|
+
'top-left': -45,
|
|
47
|
+
'top-center': 0,
|
|
48
|
+
'top-right': 45,
|
|
49
|
+
'middle-right': -90,
|
|
50
|
+
'middle-left': 90,
|
|
51
|
+
'bottom-left': -135,
|
|
52
|
+
'bottom-center': 180,
|
|
53
|
+
'bottom-right': 135,
|
|
54
|
+
};
|
|
55
|
+
const TOUCH_DEVICE = 'ontouchstart' in Konva._global;
|
|
56
|
+
function getCursor(anchorName, rad) {
|
|
57
|
+
if (anchorName === 'rotater') {
|
|
58
|
+
return 'crosshair';
|
|
59
|
+
}
|
|
60
|
+
rad += Util.degToRad(ANGLES[anchorName] || 0);
|
|
61
|
+
var angle = ((Util.radToDeg(rad) % 360) + 360) % 360;
|
|
62
|
+
if (Util._inRange(angle, 315 + 22.5, 360) || Util._inRange(angle, 0, 22.5)) {
|
|
63
|
+
return 'ns-resize';
|
|
64
|
+
}
|
|
65
|
+
else if (Util._inRange(angle, 45 - 22.5, 45 + 22.5)) {
|
|
66
|
+
return 'nesw-resize';
|
|
67
|
+
}
|
|
68
|
+
else if (Util._inRange(angle, 90 - 22.5, 90 + 22.5)) {
|
|
69
|
+
return 'ew-resize';
|
|
70
|
+
}
|
|
71
|
+
else if (Util._inRange(angle, 135 - 22.5, 135 + 22.5)) {
|
|
72
|
+
return 'nwse-resize';
|
|
73
|
+
}
|
|
74
|
+
else if (Util._inRange(angle, 180 - 22.5, 180 + 22.5)) {
|
|
75
|
+
return 'ns-resize';
|
|
76
|
+
}
|
|
77
|
+
else if (Util._inRange(angle, 225 - 22.5, 225 + 22.5)) {
|
|
78
|
+
return 'nesw-resize';
|
|
79
|
+
}
|
|
80
|
+
else if (Util._inRange(angle, 270 - 22.5, 270 + 22.5)) {
|
|
81
|
+
return 'ew-resize';
|
|
82
|
+
}
|
|
83
|
+
else if (Util._inRange(angle, 315 - 22.5, 315 + 22.5)) {
|
|
84
|
+
return 'nwse-resize';
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
Util.error('Transformer has unknown angle for cursor detection: ' + angle);
|
|
88
|
+
return 'pointer';
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
var ANCHORS_NAMES = [
|
|
92
|
+
'top-left',
|
|
93
|
+
'top-center',
|
|
94
|
+
'top-right',
|
|
95
|
+
'middle-right',
|
|
96
|
+
'middle-left',
|
|
97
|
+
'bottom-left',
|
|
98
|
+
'bottom-center',
|
|
99
|
+
'bottom-right',
|
|
100
|
+
];
|
|
101
|
+
var MAX_SAFE_INTEGER = 100000000;
|
|
102
|
+
function getCenter(shape) {
|
|
103
|
+
return {
|
|
104
|
+
x: shape.x +
|
|
105
|
+
(shape.width / 2) * Math.cos(shape.rotation) +
|
|
106
|
+
(shape.height / 2) * Math.sin(-shape.rotation),
|
|
107
|
+
y: shape.y +
|
|
108
|
+
(shape.height / 2) * Math.cos(shape.rotation) +
|
|
109
|
+
(shape.width / 2) * Math.sin(shape.rotation),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function rotateAroundPoint(shape, angleRad, point) {
|
|
113
|
+
const x = point.x +
|
|
114
|
+
(shape.x - point.x) * Math.cos(angleRad) -
|
|
115
|
+
(shape.y - point.y) * Math.sin(angleRad);
|
|
116
|
+
const y = point.y +
|
|
117
|
+
(shape.x - point.x) * Math.sin(angleRad) +
|
|
118
|
+
(shape.y - point.y) * Math.cos(angleRad);
|
|
119
|
+
return Object.assign(Object.assign({}, shape), { rotation: shape.rotation + angleRad, x,
|
|
120
|
+
y });
|
|
121
|
+
}
|
|
122
|
+
function rotateAroundCenter(shape, deltaRad) {
|
|
123
|
+
const center = getCenter(shape);
|
|
124
|
+
return rotateAroundPoint(shape, deltaRad, center);
|
|
125
|
+
}
|
|
126
|
+
function getSnap(snaps, newRotationRad, tol) {
|
|
127
|
+
let snapped = newRotationRad;
|
|
128
|
+
for (let i = 0; i < snaps.length; i++) {
|
|
129
|
+
const angle = Konva.getAngle(snaps[i]);
|
|
130
|
+
const absDiff = Math.abs(angle - newRotationRad) % (Math.PI * 2);
|
|
131
|
+
const dif = Math.min(absDiff, Math.PI * 2 - absDiff);
|
|
132
|
+
if (dif < tol) {
|
|
133
|
+
snapped = angle;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return snapped;
|
|
137
|
+
}
|
|
138
|
+
export class Transformer extends Group {
|
|
139
|
+
constructor(config) {
|
|
140
|
+
super(config);
|
|
141
|
+
this._transforming = false;
|
|
142
|
+
this._createElements();
|
|
143
|
+
this._handleMouseMove = this._handleMouseMove.bind(this);
|
|
144
|
+
this._handleMouseUp = this._handleMouseUp.bind(this);
|
|
145
|
+
this.update = this.update.bind(this);
|
|
146
|
+
this.on(ATTR_CHANGE_LIST, this.update);
|
|
147
|
+
if (this.getNode()) {
|
|
148
|
+
this.update();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
attachTo(node) {
|
|
152
|
+
this.setNode(node);
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
setNode(node) {
|
|
156
|
+
Util.warn('tr.setNode(shape), tr.node(shape) and tr.attachTo(shape) methods are deprecated. Please use tr.nodes(nodesArray) instead.');
|
|
157
|
+
return this.setNodes([node]);
|
|
158
|
+
}
|
|
159
|
+
getNode() {
|
|
160
|
+
return this._nodes && this._nodes[0];
|
|
161
|
+
}
|
|
162
|
+
setNodes(nodes = []) {
|
|
163
|
+
if (this._nodes && this._nodes.length) {
|
|
164
|
+
this.detach();
|
|
165
|
+
}
|
|
166
|
+
this._nodes = nodes;
|
|
167
|
+
if (nodes.length === 1 && this.useSingleNodeRotation()) {
|
|
168
|
+
this.rotation(nodes[0].getAbsoluteRotation());
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
this.rotation(0);
|
|
172
|
+
}
|
|
173
|
+
this._nodes.forEach((node) => {
|
|
174
|
+
const additionalEvents = node._attrsAffectingSize
|
|
175
|
+
.map((prop) => prop + 'Change.' + EVENTS_NAME)
|
|
176
|
+
.join(' ');
|
|
177
|
+
const onChange = () => {
|
|
178
|
+
if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
|
|
179
|
+
this.rotation(this.nodes()[0].getAbsoluteRotation());
|
|
180
|
+
}
|
|
181
|
+
this._resetTransformCache();
|
|
182
|
+
if (!this._transforming && !this.isDragging()) {
|
|
183
|
+
this.update();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
node.on(additionalEvents, onChange);
|
|
187
|
+
node.on(TRANSFORM_CHANGE_STR, onChange);
|
|
188
|
+
node.on(`absoluteTransformChange.${EVENTS_NAME}`, onChange);
|
|
189
|
+
node.on(`xChange.${EVENTS_NAME} yChange.${EVENTS_NAME}`, onChange);
|
|
190
|
+
this._proxyDrag(node);
|
|
191
|
+
});
|
|
192
|
+
this._resetTransformCache();
|
|
193
|
+
var elementsCreated = !!this.findOne('.top-left');
|
|
194
|
+
if (elementsCreated) {
|
|
195
|
+
this.update();
|
|
196
|
+
}
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
_proxyDrag(node) {
|
|
200
|
+
let lastPos;
|
|
201
|
+
node.on(`dragstart.${EVENTS_NAME}`, (e) => {
|
|
202
|
+
lastPos = node.getAbsolutePosition();
|
|
203
|
+
if (!this.isDragging() && node !== this.findOne('.back')) {
|
|
204
|
+
this.startDrag(e, false);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
node.on(`dragmove.${EVENTS_NAME}`, (e) => {
|
|
208
|
+
if (!lastPos) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const abs = node.getAbsolutePosition();
|
|
212
|
+
const dx = abs.x - lastPos.x;
|
|
213
|
+
const dy = abs.y - lastPos.y;
|
|
214
|
+
this.nodes().forEach((otherNode) => {
|
|
215
|
+
if (otherNode === node) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (otherNode.isDragging()) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const otherAbs = otherNode.getAbsolutePosition();
|
|
222
|
+
otherNode.setAbsolutePosition({
|
|
223
|
+
x: otherAbs.x + dx,
|
|
224
|
+
y: otherAbs.y + dy,
|
|
225
|
+
});
|
|
226
|
+
otherNode.startDrag(e);
|
|
227
|
+
});
|
|
228
|
+
lastPos = null;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
getNodes() {
|
|
232
|
+
return this._nodes || [];
|
|
233
|
+
}
|
|
234
|
+
getActiveAnchor() {
|
|
235
|
+
return this._movingAnchorName;
|
|
236
|
+
}
|
|
237
|
+
detach() {
|
|
238
|
+
if (this._nodes) {
|
|
239
|
+
this._nodes.forEach((node) => {
|
|
240
|
+
node.off('.' + EVENTS_NAME);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
this._nodes = [];
|
|
244
|
+
this._resetTransformCache();
|
|
245
|
+
}
|
|
246
|
+
_resetTransformCache() {
|
|
247
|
+
this._clearCache(NODES_RECT);
|
|
248
|
+
this._clearCache('transform');
|
|
249
|
+
this._clearSelfAndDescendantCache('absoluteTransform');
|
|
250
|
+
}
|
|
251
|
+
_getNodeRect() {
|
|
252
|
+
return this._getCache(NODES_RECT, this.__getNodeRect);
|
|
253
|
+
}
|
|
254
|
+
__getNodeShape(node, rot = this.rotation(), relative) {
|
|
255
|
+
var rect = node.getClientRect({
|
|
256
|
+
skipTransform: true,
|
|
257
|
+
skipShadow: true,
|
|
258
|
+
skipStroke: this.ignoreStroke(),
|
|
259
|
+
});
|
|
260
|
+
var absScale = node.getAbsoluteScale(relative);
|
|
261
|
+
var absPos = node.getAbsolutePosition(relative);
|
|
262
|
+
var dx = rect.x * absScale.x - node.offsetX() * absScale.x;
|
|
263
|
+
var dy = rect.y * absScale.y - node.offsetY() * absScale.y;
|
|
264
|
+
const rotation = (Konva.getAngle(node.getAbsoluteRotation()) + Math.PI * 2) %
|
|
265
|
+
(Math.PI * 2);
|
|
266
|
+
const box = {
|
|
267
|
+
x: absPos.x + dx * Math.cos(rotation) + dy * Math.sin(-rotation),
|
|
268
|
+
y: absPos.y + dy * Math.cos(rotation) + dx * Math.sin(rotation),
|
|
269
|
+
width: rect.width * absScale.x,
|
|
270
|
+
height: rect.height * absScale.y,
|
|
271
|
+
rotation: rotation,
|
|
272
|
+
};
|
|
273
|
+
return rotateAroundPoint(box, -Konva.getAngle(rot), {
|
|
274
|
+
x: 0,
|
|
275
|
+
y: 0,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
__getNodeRect() {
|
|
279
|
+
var node = this.getNode();
|
|
280
|
+
if (!node) {
|
|
281
|
+
return {
|
|
282
|
+
x: -MAX_SAFE_INTEGER,
|
|
283
|
+
y: -MAX_SAFE_INTEGER,
|
|
284
|
+
width: 0,
|
|
285
|
+
height: 0,
|
|
286
|
+
rotation: 0,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
const totalPoints = [];
|
|
290
|
+
this.nodes().map((node) => {
|
|
291
|
+
const box = node.getClientRect({
|
|
292
|
+
skipTransform: true,
|
|
293
|
+
skipShadow: true,
|
|
294
|
+
skipStroke: this.ignoreStroke(),
|
|
295
|
+
});
|
|
296
|
+
var points = [
|
|
297
|
+
{ x: box.x, y: box.y },
|
|
298
|
+
{ x: box.x + box.width, y: box.y },
|
|
299
|
+
{ x: box.x + box.width, y: box.y + box.height },
|
|
300
|
+
{ x: box.x, y: box.y + box.height },
|
|
301
|
+
];
|
|
302
|
+
var trans = node.getAbsoluteTransform();
|
|
303
|
+
points.forEach(function (point) {
|
|
304
|
+
var transformed = trans.point(point);
|
|
305
|
+
totalPoints.push(transformed);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
const tr = new Transform();
|
|
309
|
+
tr.rotate(-Konva.getAngle(this.rotation()));
|
|
310
|
+
var minX, minY, maxX, maxY;
|
|
311
|
+
totalPoints.forEach(function (point) {
|
|
312
|
+
var transformed = tr.point(point);
|
|
313
|
+
if (minX === undefined) {
|
|
314
|
+
minX = maxX = transformed.x;
|
|
315
|
+
minY = maxY = transformed.y;
|
|
316
|
+
}
|
|
317
|
+
minX = Math.min(minX, transformed.x);
|
|
318
|
+
minY = Math.min(minY, transformed.y);
|
|
319
|
+
maxX = Math.max(maxX, transformed.x);
|
|
320
|
+
maxY = Math.max(maxY, transformed.y);
|
|
321
|
+
});
|
|
322
|
+
tr.invert();
|
|
323
|
+
const p = tr.point({ x: minX, y: minY });
|
|
324
|
+
return {
|
|
325
|
+
x: p.x,
|
|
326
|
+
y: p.y,
|
|
327
|
+
width: maxX - minX,
|
|
328
|
+
height: maxY - minY,
|
|
329
|
+
rotation: Konva.getAngle(this.rotation()),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
getX() {
|
|
333
|
+
return this._getNodeRect().x;
|
|
334
|
+
}
|
|
335
|
+
getY() {
|
|
336
|
+
return this._getNodeRect().y;
|
|
337
|
+
}
|
|
338
|
+
getWidth() {
|
|
339
|
+
return this._getNodeRect().width;
|
|
340
|
+
}
|
|
341
|
+
getHeight() {
|
|
342
|
+
return this._getNodeRect().height;
|
|
343
|
+
}
|
|
344
|
+
_createElements() {
|
|
345
|
+
this._createBack();
|
|
346
|
+
ANCHORS_NAMES.forEach(function (name) {
|
|
347
|
+
this._createAnchor(name);
|
|
348
|
+
}.bind(this));
|
|
349
|
+
this._createAnchor('rotater');
|
|
350
|
+
}
|
|
351
|
+
_createAnchor(name) {
|
|
352
|
+
var anchor = new Rect({
|
|
353
|
+
stroke: 'rgb(0, 161, 255)',
|
|
354
|
+
fill: 'white',
|
|
355
|
+
strokeWidth: 1,
|
|
356
|
+
name: name + ' _anchor',
|
|
357
|
+
dragDistance: 0,
|
|
358
|
+
draggable: true,
|
|
359
|
+
hitStrokeWidth: TOUCH_DEVICE ? 10 : 'auto',
|
|
360
|
+
});
|
|
361
|
+
var self = this;
|
|
362
|
+
anchor.on('mousedown touchstart', function (e) {
|
|
363
|
+
self._handleMouseDown(e);
|
|
364
|
+
});
|
|
365
|
+
anchor.on('dragstart', (e) => {
|
|
366
|
+
anchor.stopDrag();
|
|
367
|
+
e.cancelBubble = true;
|
|
368
|
+
});
|
|
369
|
+
anchor.on('dragend', (e) => {
|
|
370
|
+
e.cancelBubble = true;
|
|
371
|
+
});
|
|
372
|
+
anchor.on('mouseenter', () => {
|
|
373
|
+
var rad = Konva.getAngle(this.rotation());
|
|
374
|
+
var cursor = getCursor(name, rad);
|
|
375
|
+
anchor.getStage().content &&
|
|
376
|
+
(anchor.getStage().content.style.cursor = cursor);
|
|
377
|
+
this._cursorChange = true;
|
|
378
|
+
});
|
|
379
|
+
anchor.on('mouseout', () => {
|
|
380
|
+
anchor.getStage().content &&
|
|
381
|
+
(anchor.getStage().content.style.cursor = '');
|
|
382
|
+
this._cursorChange = false;
|
|
383
|
+
});
|
|
384
|
+
this.add(anchor);
|
|
385
|
+
}
|
|
386
|
+
_createBack() {
|
|
387
|
+
var back = new Shape({
|
|
388
|
+
name: 'back',
|
|
389
|
+
width: 0,
|
|
390
|
+
height: 0,
|
|
391
|
+
draggable: true,
|
|
392
|
+
sceneFunc(ctx) {
|
|
393
|
+
var tr = this.getParent();
|
|
394
|
+
var padding = tr.padding();
|
|
395
|
+
ctx.beginPath();
|
|
396
|
+
ctx.rect(-padding, -padding, this.width() + padding * 2, this.height() + padding * 2);
|
|
397
|
+
ctx.moveTo(this.width() / 2, -padding);
|
|
398
|
+
if (tr.rotateEnabled()) {
|
|
399
|
+
ctx.lineTo(this.width() / 2, -tr.rotateAnchorOffset() * Util._sign(this.height()) - padding);
|
|
400
|
+
}
|
|
401
|
+
ctx.fillStrokeShape(this);
|
|
402
|
+
},
|
|
403
|
+
hitFunc: (ctx, shape) => {
|
|
404
|
+
if (!this.shouldOverdrawWholeArea()) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
var padding = this.padding();
|
|
408
|
+
ctx.beginPath();
|
|
409
|
+
ctx.rect(-padding, -padding, shape.width() + padding * 2, shape.height() + padding * 2);
|
|
410
|
+
ctx.fillStrokeShape(shape);
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
this.add(back);
|
|
414
|
+
this._proxyDrag(back);
|
|
415
|
+
back.on('dragstart', (e) => {
|
|
416
|
+
e.cancelBubble = true;
|
|
417
|
+
});
|
|
418
|
+
back.on('dragmove', (e) => {
|
|
419
|
+
e.cancelBubble = true;
|
|
420
|
+
});
|
|
421
|
+
back.on('dragend', (e) => {
|
|
422
|
+
e.cancelBubble = true;
|
|
423
|
+
});
|
|
424
|
+
this.on('dragmove', (e) => {
|
|
425
|
+
this.update();
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
_handleMouseDown(e) {
|
|
429
|
+
this._movingAnchorName = e.target.name().split(' ')[0];
|
|
430
|
+
var attrs = this._getNodeRect();
|
|
431
|
+
var width = attrs.width;
|
|
432
|
+
var height = attrs.height;
|
|
433
|
+
var hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
|
|
434
|
+
this.sin = Math.abs(height / hypotenuse);
|
|
435
|
+
this.cos = Math.abs(width / hypotenuse);
|
|
436
|
+
if (typeof window !== 'undefined') {
|
|
437
|
+
window.addEventListener('mousemove', this._handleMouseMove);
|
|
438
|
+
window.addEventListener('touchmove', this._handleMouseMove);
|
|
439
|
+
window.addEventListener('mouseup', this._handleMouseUp, true);
|
|
440
|
+
window.addEventListener('touchend', this._handleMouseUp, true);
|
|
441
|
+
}
|
|
442
|
+
this._transforming = true;
|
|
443
|
+
var ap = e.target.getAbsolutePosition();
|
|
444
|
+
var pos = e.target.getStage().getPointerPosition();
|
|
445
|
+
this._anchorDragOffset = {
|
|
446
|
+
x: pos.x - ap.x,
|
|
447
|
+
y: pos.y - ap.y,
|
|
448
|
+
};
|
|
449
|
+
this._fire('transformstart', { evt: e, target: this.getNode() });
|
|
450
|
+
this._nodes.forEach((target) => {
|
|
451
|
+
target._fire('transformstart', { evt: e, target });
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
_handleMouseMove(e) {
|
|
455
|
+
var x, y, newHypotenuse;
|
|
456
|
+
var anchorNode = this.findOne('.' + this._movingAnchorName);
|
|
457
|
+
var stage = anchorNode.getStage();
|
|
458
|
+
stage.setPointersPositions(e);
|
|
459
|
+
const pp = stage.getPointerPosition();
|
|
460
|
+
var newNodePos = {
|
|
461
|
+
x: pp.x - this._anchorDragOffset.x,
|
|
462
|
+
y: pp.y - this._anchorDragOffset.y,
|
|
463
|
+
};
|
|
464
|
+
const oldAbs = anchorNode.getAbsolutePosition();
|
|
465
|
+
anchorNode.setAbsolutePosition(newNodePos);
|
|
466
|
+
const newAbs = anchorNode.getAbsolutePosition();
|
|
467
|
+
if (oldAbs.x === newAbs.x && oldAbs.y === newAbs.y) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (this._movingAnchorName === 'rotater') {
|
|
471
|
+
var attrs = this._getNodeRect();
|
|
472
|
+
x = anchorNode.x() - attrs.width / 2;
|
|
473
|
+
y = -anchorNode.y() + attrs.height / 2;
|
|
474
|
+
let delta = Math.atan2(-y, x) + Math.PI / 2;
|
|
475
|
+
if (attrs.height < 0) {
|
|
476
|
+
delta -= Math.PI;
|
|
477
|
+
}
|
|
478
|
+
var oldRotation = Konva.getAngle(this.rotation());
|
|
479
|
+
const newRotation = oldRotation + delta;
|
|
480
|
+
const tol = Konva.getAngle(this.rotationSnapTolerance());
|
|
481
|
+
const snappedRot = getSnap(this.rotationSnaps(), newRotation, tol);
|
|
482
|
+
const diff = snappedRot - attrs.rotation;
|
|
483
|
+
const shape = rotateAroundCenter(attrs, diff);
|
|
484
|
+
this._fitNodesInto(shape, e);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
var keepProportion = this.keepRatio() || e.shiftKey;
|
|
488
|
+
var centeredScaling = this.centeredScaling() || e.altKey;
|
|
489
|
+
if (this._movingAnchorName === 'top-left') {
|
|
490
|
+
if (keepProportion) {
|
|
491
|
+
var comparePoint = centeredScaling
|
|
492
|
+
? {
|
|
493
|
+
x: this.width() / 2,
|
|
494
|
+
y: this.height() / 2,
|
|
495
|
+
}
|
|
496
|
+
: {
|
|
497
|
+
x: this.findOne('.bottom-right').x(),
|
|
498
|
+
y: this.findOne('.bottom-right').y(),
|
|
499
|
+
};
|
|
500
|
+
newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
|
|
501
|
+
Math.pow(comparePoint.y - anchorNode.y(), 2));
|
|
502
|
+
var reverseX = this.findOne('.top-left').x() > comparePoint.x ? -1 : 1;
|
|
503
|
+
var reverseY = this.findOne('.top-left').y() > comparePoint.y ? -1 : 1;
|
|
504
|
+
x = newHypotenuse * this.cos * reverseX;
|
|
505
|
+
y = newHypotenuse * this.sin * reverseY;
|
|
506
|
+
this.findOne('.top-left').x(comparePoint.x - x);
|
|
507
|
+
this.findOne('.top-left').y(comparePoint.y - y);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
else if (this._movingAnchorName === 'top-center') {
|
|
511
|
+
this.findOne('.top-left').y(anchorNode.y());
|
|
512
|
+
}
|
|
513
|
+
else if (this._movingAnchorName === 'top-right') {
|
|
514
|
+
if (keepProportion) {
|
|
515
|
+
var comparePoint = centeredScaling
|
|
516
|
+
? {
|
|
517
|
+
x: this.width() / 2,
|
|
518
|
+
y: this.height() / 2,
|
|
519
|
+
}
|
|
520
|
+
: {
|
|
521
|
+
x: this.findOne('.bottom-left').x(),
|
|
522
|
+
y: this.findOne('.bottom-left').y(),
|
|
523
|
+
};
|
|
524
|
+
newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
|
|
525
|
+
Math.pow(comparePoint.y - anchorNode.y(), 2));
|
|
526
|
+
var reverseX = this.findOne('.top-right').x() < comparePoint.x ? -1 : 1;
|
|
527
|
+
var reverseY = this.findOne('.top-right').y() > comparePoint.y ? -1 : 1;
|
|
528
|
+
x = newHypotenuse * this.cos * reverseX;
|
|
529
|
+
y = newHypotenuse * this.sin * reverseY;
|
|
530
|
+
this.findOne('.top-right').x(comparePoint.x + x);
|
|
531
|
+
this.findOne('.top-right').y(comparePoint.y - y);
|
|
532
|
+
}
|
|
533
|
+
var pos = anchorNode.position();
|
|
534
|
+
this.findOne('.top-left').y(pos.y);
|
|
535
|
+
this.findOne('.bottom-right').x(pos.x);
|
|
536
|
+
}
|
|
537
|
+
else if (this._movingAnchorName === 'middle-left') {
|
|
538
|
+
this.findOne('.top-left').x(anchorNode.x());
|
|
539
|
+
}
|
|
540
|
+
else if (this._movingAnchorName === 'middle-right') {
|
|
541
|
+
this.findOne('.bottom-right').x(anchorNode.x());
|
|
542
|
+
}
|
|
543
|
+
else if (this._movingAnchorName === 'bottom-left') {
|
|
544
|
+
if (keepProportion) {
|
|
545
|
+
var comparePoint = centeredScaling
|
|
546
|
+
? {
|
|
547
|
+
x: this.width() / 2,
|
|
548
|
+
y: this.height() / 2,
|
|
549
|
+
}
|
|
550
|
+
: {
|
|
551
|
+
x: this.findOne('.top-right').x(),
|
|
552
|
+
y: this.findOne('.top-right').y(),
|
|
553
|
+
};
|
|
554
|
+
newHypotenuse = Math.sqrt(Math.pow(comparePoint.x - anchorNode.x(), 2) +
|
|
555
|
+
Math.pow(anchorNode.y() - comparePoint.y, 2));
|
|
556
|
+
var reverseX = comparePoint.x < anchorNode.x() ? -1 : 1;
|
|
557
|
+
var reverseY = anchorNode.y() < comparePoint.y ? -1 : 1;
|
|
558
|
+
x = newHypotenuse * this.cos * reverseX;
|
|
559
|
+
y = newHypotenuse * this.sin * reverseY;
|
|
560
|
+
anchorNode.x(comparePoint.x - x);
|
|
561
|
+
anchorNode.y(comparePoint.y + y);
|
|
562
|
+
}
|
|
563
|
+
pos = anchorNode.position();
|
|
564
|
+
this.findOne('.top-left').x(pos.x);
|
|
565
|
+
this.findOne('.bottom-right').y(pos.y);
|
|
566
|
+
}
|
|
567
|
+
else if (this._movingAnchorName === 'bottom-center') {
|
|
568
|
+
this.findOne('.bottom-right').y(anchorNode.y());
|
|
569
|
+
}
|
|
570
|
+
else if (this._movingAnchorName === 'bottom-right') {
|
|
571
|
+
if (keepProportion) {
|
|
572
|
+
var comparePoint = centeredScaling
|
|
573
|
+
? {
|
|
574
|
+
x: this.width() / 2,
|
|
575
|
+
y: this.height() / 2,
|
|
576
|
+
}
|
|
577
|
+
: {
|
|
578
|
+
x: this.findOne('.top-left').x(),
|
|
579
|
+
y: this.findOne('.top-left').y(),
|
|
580
|
+
};
|
|
581
|
+
newHypotenuse = Math.sqrt(Math.pow(anchorNode.x() - comparePoint.x, 2) +
|
|
582
|
+
Math.pow(anchorNode.y() - comparePoint.y, 2));
|
|
583
|
+
var reverseX = this.findOne('.bottom-right').x() < comparePoint.x ? -1 : 1;
|
|
584
|
+
var reverseY = this.findOne('.bottom-right').y() < comparePoint.y ? -1 : 1;
|
|
585
|
+
x = newHypotenuse * this.cos * reverseX;
|
|
586
|
+
y = newHypotenuse * this.sin * reverseY;
|
|
587
|
+
this.findOne('.bottom-right').x(comparePoint.x + x);
|
|
588
|
+
this.findOne('.bottom-right').y(comparePoint.y + y);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
console.error(new Error('Wrong position argument of selection resizer: ' +
|
|
593
|
+
this._movingAnchorName));
|
|
594
|
+
}
|
|
595
|
+
var centeredScaling = this.centeredScaling() || e.altKey;
|
|
596
|
+
if (centeredScaling) {
|
|
597
|
+
var topLeft = this.findOne('.top-left');
|
|
598
|
+
var bottomRight = this.findOne('.bottom-right');
|
|
599
|
+
var topOffsetX = topLeft.x();
|
|
600
|
+
var topOffsetY = topLeft.y();
|
|
601
|
+
var bottomOffsetX = this.getWidth() - bottomRight.x();
|
|
602
|
+
var bottomOffsetY = this.getHeight() - bottomRight.y();
|
|
603
|
+
bottomRight.move({
|
|
604
|
+
x: -topOffsetX,
|
|
605
|
+
y: -topOffsetY,
|
|
606
|
+
});
|
|
607
|
+
topLeft.move({
|
|
608
|
+
x: bottomOffsetX,
|
|
609
|
+
y: bottomOffsetY,
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
var absPos = this.findOne('.top-left').getAbsolutePosition();
|
|
613
|
+
x = absPos.x;
|
|
614
|
+
y = absPos.y;
|
|
615
|
+
var width = this.findOne('.bottom-right').x() - this.findOne('.top-left').x();
|
|
616
|
+
var height = this.findOne('.bottom-right').y() - this.findOne('.top-left').y();
|
|
617
|
+
this._fitNodesInto({
|
|
618
|
+
x: x,
|
|
619
|
+
y: y,
|
|
620
|
+
width: width,
|
|
621
|
+
height: height,
|
|
622
|
+
rotation: Konva.getAngle(this.rotation()),
|
|
623
|
+
}, e);
|
|
624
|
+
}
|
|
625
|
+
_handleMouseUp(e) {
|
|
626
|
+
this._removeEvents(e);
|
|
627
|
+
}
|
|
628
|
+
getAbsoluteTransform() {
|
|
629
|
+
return this.getTransform();
|
|
630
|
+
}
|
|
631
|
+
_removeEvents(e) {
|
|
632
|
+
if (this._transforming) {
|
|
633
|
+
this._transforming = false;
|
|
634
|
+
if (typeof window !== 'undefined') {
|
|
635
|
+
window.removeEventListener('mousemove', this._handleMouseMove);
|
|
636
|
+
window.removeEventListener('touchmove', this._handleMouseMove);
|
|
637
|
+
window.removeEventListener('mouseup', this._handleMouseUp, true);
|
|
638
|
+
window.removeEventListener('touchend', this._handleMouseUp, true);
|
|
639
|
+
}
|
|
640
|
+
var node = this.getNode();
|
|
641
|
+
this._fire('transformend', { evt: e, target: node });
|
|
642
|
+
if (node) {
|
|
643
|
+
this._nodes.forEach((target) => {
|
|
644
|
+
target._fire('transformend', { evt: e, target });
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
this._movingAnchorName = null;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
_fitNodesInto(newAttrs, evt) {
|
|
651
|
+
var oldAttrs = this._getNodeRect();
|
|
652
|
+
const minSize = 1;
|
|
653
|
+
if (Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
|
|
654
|
+
this.update();
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (Util._inRange(newAttrs.height, -this.padding() * 2 - minSize, minSize)) {
|
|
658
|
+
this.update();
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
const allowNegativeScale = this.flipEnabled();
|
|
662
|
+
var t = new Transform();
|
|
663
|
+
t.rotate(Konva.getAngle(this.rotation()));
|
|
664
|
+
if (this._movingAnchorName &&
|
|
665
|
+
newAttrs.width < 0 &&
|
|
666
|
+
this._movingAnchorName.indexOf('left') >= 0) {
|
|
667
|
+
const offset = t.point({
|
|
668
|
+
x: -this.padding() * 2,
|
|
669
|
+
y: 0,
|
|
670
|
+
});
|
|
671
|
+
newAttrs.x += offset.x;
|
|
672
|
+
newAttrs.y += offset.y;
|
|
673
|
+
newAttrs.width += this.padding() * 2;
|
|
674
|
+
this._movingAnchorName = this._movingAnchorName.replace('left', 'right');
|
|
675
|
+
this._anchorDragOffset.x -= offset.x;
|
|
676
|
+
this._anchorDragOffset.y -= offset.y;
|
|
677
|
+
if (!allowNegativeScale) {
|
|
678
|
+
this.update();
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
else if (this._movingAnchorName &&
|
|
683
|
+
newAttrs.width < 0 &&
|
|
684
|
+
this._movingAnchorName.indexOf('right') >= 0) {
|
|
685
|
+
const offset = t.point({
|
|
686
|
+
x: this.padding() * 2,
|
|
687
|
+
y: 0,
|
|
688
|
+
});
|
|
689
|
+
this._movingAnchorName = this._movingAnchorName.replace('right', 'left');
|
|
690
|
+
this._anchorDragOffset.x -= offset.x;
|
|
691
|
+
this._anchorDragOffset.y -= offset.y;
|
|
692
|
+
newAttrs.width += this.padding() * 2;
|
|
693
|
+
if (!allowNegativeScale) {
|
|
694
|
+
this.update();
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
if (this._movingAnchorName &&
|
|
699
|
+
newAttrs.height < 0 &&
|
|
700
|
+
this._movingAnchorName.indexOf('top') >= 0) {
|
|
701
|
+
const offset = t.point({
|
|
702
|
+
x: 0,
|
|
703
|
+
y: -this.padding() * 2,
|
|
704
|
+
});
|
|
705
|
+
newAttrs.x += offset.x;
|
|
706
|
+
newAttrs.y += offset.y;
|
|
707
|
+
this._movingAnchorName = this._movingAnchorName.replace('top', 'bottom');
|
|
708
|
+
this._anchorDragOffset.x -= offset.x;
|
|
709
|
+
this._anchorDragOffset.y -= offset.y;
|
|
710
|
+
newAttrs.height += this.padding() * 2;
|
|
711
|
+
if (!allowNegativeScale) {
|
|
712
|
+
this.update();
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
else if (this._movingAnchorName &&
|
|
717
|
+
newAttrs.height < 0 &&
|
|
718
|
+
this._movingAnchorName.indexOf('bottom') >= 0) {
|
|
719
|
+
const offset = t.point({
|
|
720
|
+
x: 0,
|
|
721
|
+
y: this.padding() * 2,
|
|
722
|
+
});
|
|
723
|
+
this._movingAnchorName = this._movingAnchorName.replace('bottom', 'top');
|
|
724
|
+
this._anchorDragOffset.x -= offset.x;
|
|
725
|
+
this._anchorDragOffset.y -= offset.y;
|
|
726
|
+
newAttrs.height += this.padding() * 2;
|
|
727
|
+
if (!allowNegativeScale) {
|
|
728
|
+
this.update();
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (this.boundBoxFunc()) {
|
|
733
|
+
const bounded = this.boundBoxFunc()(oldAttrs, newAttrs);
|
|
734
|
+
if (bounded) {
|
|
735
|
+
newAttrs = bounded;
|
|
736
|
+
}
|
|
737
|
+
else {
|
|
738
|
+
Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
const baseSize = 10000000;
|
|
742
|
+
const oldTr = new Transform();
|
|
743
|
+
oldTr.translate(oldAttrs.x, oldAttrs.y);
|
|
744
|
+
oldTr.rotate(oldAttrs.rotation);
|
|
745
|
+
oldTr.scale(oldAttrs.width / baseSize, oldAttrs.height / baseSize);
|
|
746
|
+
const newTr = new Transform();
|
|
747
|
+
newTr.translate(newAttrs.x, newAttrs.y);
|
|
748
|
+
newTr.rotate(newAttrs.rotation);
|
|
749
|
+
newTr.scale(newAttrs.width / baseSize, newAttrs.height / baseSize);
|
|
750
|
+
const delta = newTr.multiply(oldTr.invert());
|
|
751
|
+
this._nodes.forEach((node) => {
|
|
752
|
+
var _a;
|
|
753
|
+
const parentTransform = node.getParent().getAbsoluteTransform();
|
|
754
|
+
const localTransform = node.getTransform().copy();
|
|
755
|
+
localTransform.translate(node.offsetX(), node.offsetY());
|
|
756
|
+
const newLocalTransform = new Transform();
|
|
757
|
+
newLocalTransform
|
|
758
|
+
.multiply(parentTransform.copy().invert())
|
|
759
|
+
.multiply(delta)
|
|
760
|
+
.multiply(parentTransform)
|
|
761
|
+
.multiply(localTransform);
|
|
762
|
+
const attrs = newLocalTransform.decompose();
|
|
763
|
+
node.setAttrs(attrs);
|
|
764
|
+
this._fire('transform', { evt: evt, target: node });
|
|
765
|
+
node._fire('transform', { evt: evt, target: node });
|
|
766
|
+
(_a = node.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
|
|
767
|
+
});
|
|
768
|
+
this.rotation(Util._getRotation(newAttrs.rotation));
|
|
769
|
+
this._resetTransformCache();
|
|
770
|
+
this.update();
|
|
771
|
+
this.getLayer().batchDraw();
|
|
772
|
+
}
|
|
773
|
+
forceUpdate() {
|
|
774
|
+
this._resetTransformCache();
|
|
775
|
+
this.update();
|
|
776
|
+
}
|
|
777
|
+
_batchChangeChild(selector, attrs) {
|
|
778
|
+
const anchor = this.findOne(selector);
|
|
779
|
+
anchor.setAttrs(attrs);
|
|
780
|
+
}
|
|
781
|
+
update() {
|
|
782
|
+
var _a;
|
|
783
|
+
var attrs = this._getNodeRect();
|
|
784
|
+
this.rotation(Util._getRotation(attrs.rotation));
|
|
785
|
+
var width = attrs.width;
|
|
786
|
+
var height = attrs.height;
|
|
787
|
+
var enabledAnchors = this.enabledAnchors();
|
|
788
|
+
var resizeEnabled = this.resizeEnabled();
|
|
789
|
+
var padding = this.padding();
|
|
790
|
+
var anchorSize = this.anchorSize();
|
|
791
|
+
this.find('._anchor').forEach((node) => {
|
|
792
|
+
node.setAttrs({
|
|
793
|
+
width: anchorSize,
|
|
794
|
+
height: anchorSize,
|
|
795
|
+
offsetX: anchorSize / 2,
|
|
796
|
+
offsetY: anchorSize / 2,
|
|
797
|
+
stroke: this.anchorStroke(),
|
|
798
|
+
strokeWidth: this.anchorStrokeWidth(),
|
|
799
|
+
fill: this.anchorFill(),
|
|
800
|
+
cornerRadius: this.anchorCornerRadius(),
|
|
801
|
+
});
|
|
802
|
+
});
|
|
803
|
+
this._batchChangeChild('.top-left', {
|
|
804
|
+
x: 0,
|
|
805
|
+
y: 0,
|
|
806
|
+
offsetX: anchorSize / 2 + padding,
|
|
807
|
+
offsetY: anchorSize / 2 + padding,
|
|
808
|
+
visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0,
|
|
809
|
+
});
|
|
810
|
+
this._batchChangeChild('.top-center', {
|
|
811
|
+
x: width / 2,
|
|
812
|
+
y: 0,
|
|
813
|
+
offsetY: anchorSize / 2 + padding,
|
|
814
|
+
visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0,
|
|
815
|
+
});
|
|
816
|
+
this._batchChangeChild('.top-right', {
|
|
817
|
+
x: width,
|
|
818
|
+
y: 0,
|
|
819
|
+
offsetX: anchorSize / 2 - padding,
|
|
820
|
+
offsetY: anchorSize / 2 + padding,
|
|
821
|
+
visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0,
|
|
822
|
+
});
|
|
823
|
+
this._batchChangeChild('.middle-left', {
|
|
824
|
+
x: 0,
|
|
825
|
+
y: height / 2,
|
|
826
|
+
offsetX: anchorSize / 2 + padding,
|
|
827
|
+
visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0,
|
|
828
|
+
});
|
|
829
|
+
this._batchChangeChild('.middle-right', {
|
|
830
|
+
x: width,
|
|
831
|
+
y: height / 2,
|
|
832
|
+
offsetX: anchorSize / 2 - padding,
|
|
833
|
+
visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0,
|
|
834
|
+
});
|
|
835
|
+
this._batchChangeChild('.bottom-left', {
|
|
836
|
+
x: 0,
|
|
837
|
+
y: height,
|
|
838
|
+
offsetX: anchorSize / 2 + padding,
|
|
839
|
+
offsetY: anchorSize / 2 - padding,
|
|
840
|
+
visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0,
|
|
841
|
+
});
|
|
842
|
+
this._batchChangeChild('.bottom-center', {
|
|
843
|
+
x: width / 2,
|
|
844
|
+
y: height,
|
|
845
|
+
offsetY: anchorSize / 2 - padding,
|
|
846
|
+
visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0,
|
|
847
|
+
});
|
|
848
|
+
this._batchChangeChild('.bottom-right', {
|
|
849
|
+
x: width,
|
|
850
|
+
y: height,
|
|
851
|
+
offsetX: anchorSize / 2 - padding,
|
|
852
|
+
offsetY: anchorSize / 2 - padding,
|
|
853
|
+
visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
|
|
854
|
+
});
|
|
855
|
+
this._batchChangeChild('.rotater', {
|
|
856
|
+
x: width / 2,
|
|
857
|
+
y: -this.rotateAnchorOffset() * Util._sign(height) - padding,
|
|
858
|
+
visible: this.rotateEnabled(),
|
|
859
|
+
});
|
|
860
|
+
this._batchChangeChild('.back', {
|
|
861
|
+
width: width,
|
|
862
|
+
height: height,
|
|
863
|
+
visible: this.borderEnabled(),
|
|
864
|
+
stroke: this.borderStroke(),
|
|
865
|
+
strokeWidth: this.borderStrokeWidth(),
|
|
866
|
+
dash: this.borderDash(),
|
|
867
|
+
x: 0,
|
|
868
|
+
y: 0,
|
|
869
|
+
});
|
|
870
|
+
(_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
|
|
871
|
+
}
|
|
872
|
+
isTransforming() {
|
|
873
|
+
return this._transforming;
|
|
874
|
+
}
|
|
875
|
+
stopTransform() {
|
|
876
|
+
if (this._transforming) {
|
|
877
|
+
this._removeEvents();
|
|
878
|
+
var anchorNode = this.findOne('.' + this._movingAnchorName);
|
|
879
|
+
if (anchorNode) {
|
|
880
|
+
anchorNode.stopDrag();
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
destroy() {
|
|
885
|
+
if (this.getStage() && this._cursorChange) {
|
|
886
|
+
this.getStage().content && (this.getStage().content.style.cursor = '');
|
|
887
|
+
}
|
|
888
|
+
Group.prototype.destroy.call(this);
|
|
889
|
+
this.detach();
|
|
890
|
+
this._removeEvents();
|
|
891
|
+
return this;
|
|
892
|
+
}
|
|
893
|
+
toObject() {
|
|
894
|
+
return Node.prototype.toObject.call(this);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
function validateAnchors(val) {
|
|
898
|
+
if (!(val instanceof Array)) {
|
|
899
|
+
Util.warn('enabledAnchors value should be an array');
|
|
900
|
+
}
|
|
901
|
+
if (val instanceof Array) {
|
|
902
|
+
val.forEach(function (name) {
|
|
903
|
+
if (ANCHORS_NAMES.indexOf(name) === -1) {
|
|
904
|
+
Util.warn('Unknown anchor name: ' +
|
|
905
|
+
name +
|
|
906
|
+
'. Available names are: ' +
|
|
907
|
+
ANCHORS_NAMES.join(', '));
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
return val || [];
|
|
912
|
+
}
|
|
913
|
+
Transformer.prototype.className = 'Transformer';
|
|
914
|
+
_registerNode(Transformer);
|
|
915
|
+
Factory.addGetterSetter(Transformer, 'enabledAnchors', ANCHORS_NAMES, validateAnchors);
|
|
916
|
+
Factory.addGetterSetter(Transformer, 'flipEnabled', true, getBooleanValidator());
|
|
917
|
+
Factory.addGetterSetter(Transformer, 'resizeEnabled', true);
|
|
918
|
+
Factory.addGetterSetter(Transformer, 'anchorSize', 10, getNumberValidator());
|
|
919
|
+
Factory.addGetterSetter(Transformer, 'rotateEnabled', true);
|
|
920
|
+
Factory.addGetterSetter(Transformer, 'rotationSnaps', []);
|
|
921
|
+
Factory.addGetterSetter(Transformer, 'rotateAnchorOffset', 50, getNumberValidator());
|
|
922
|
+
Factory.addGetterSetter(Transformer, 'rotationSnapTolerance', 5, getNumberValidator());
|
|
923
|
+
Factory.addGetterSetter(Transformer, 'borderEnabled', true);
|
|
924
|
+
Factory.addGetterSetter(Transformer, 'anchorStroke', 'rgb(0, 161, 255)');
|
|
925
|
+
Factory.addGetterSetter(Transformer, 'anchorStrokeWidth', 1, getNumberValidator());
|
|
926
|
+
Factory.addGetterSetter(Transformer, 'anchorFill', 'white');
|
|
927
|
+
Factory.addGetterSetter(Transformer, 'anchorCornerRadius', 0, getNumberValidator());
|
|
928
|
+
Factory.addGetterSetter(Transformer, 'borderStroke', 'rgb(0, 161, 255)');
|
|
929
|
+
Factory.addGetterSetter(Transformer, 'borderStrokeWidth', 1, getNumberValidator());
|
|
930
|
+
Factory.addGetterSetter(Transformer, 'borderDash');
|
|
931
|
+
Factory.addGetterSetter(Transformer, 'keepRatio', true);
|
|
932
|
+
Factory.addGetterSetter(Transformer, 'centeredScaling', false);
|
|
933
|
+
Factory.addGetterSetter(Transformer, 'ignoreStroke', false);
|
|
934
|
+
Factory.addGetterSetter(Transformer, 'padding', 0, getNumberValidator());
|
|
935
|
+
Factory.addGetterSetter(Transformer, 'node');
|
|
936
|
+
Factory.addGetterSetter(Transformer, 'nodes');
|
|
937
|
+
Factory.addGetterSetter(Transformer, 'boundBoxFunc');
|
|
938
|
+
Factory.addGetterSetter(Transformer, 'shouldOverdrawWholeArea', false);
|
|
939
|
+
Factory.addGetterSetter(Transformer, 'useSingleNodeRotation', true);
|
|
940
|
+
Factory.backCompat(Transformer, {
|
|
941
|
+
lineEnabled: 'borderEnabled',
|
|
942
|
+
rotateHandlerOffset: 'rotateAnchorOffset',
|
|
943
|
+
enabledHandlers: 'enabledAnchors',
|
|
944
|
+
});
|