konva 10.7.1 → 10.8.0
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/konva.js +133 -33
- package/konva.min.js +4 -4
- package/lib/Global.js +2 -2
- package/lib/Shape.js +10 -6
- package/lib/shapes/Transformer.d.ts +10 -2
- package/lib/shapes/Transformer.js +120 -24
- package/package.json +1 -1
package/lib/Global.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Konva JavaScript Framework v10.
|
|
2
|
+
* Konva JavaScript Framework v10.8.0
|
|
3
3
|
* http://konvajs.org/
|
|
4
4
|
* Licensed under the MIT
|
|
5
5
|
* Date: @@date
|
|
@@ -29,7 +29,7 @@ export const glob = typeof global !== 'undefined'
|
|
|
29
29
|
: {};
|
|
30
30
|
export const Konva = {
|
|
31
31
|
_global: glob,
|
|
32
|
-
version: '10.
|
|
32
|
+
version: '10.8.0',
|
|
33
33
|
isBrowser: detectBrowser(),
|
|
34
34
|
isUnminified: /param/.test(function (param) { }.toString()),
|
|
35
35
|
dblClickWindow: 400,
|
package/lib/Shape.js
CHANGED
|
@@ -372,12 +372,16 @@ export class Shape extends Node {
|
|
|
372
372
|
const forDrawing = config._forDrawing;
|
|
373
373
|
// Painted bounds are measured in the canvas that draws the shape
|
|
374
374
|
// (relativeTo): non-scaling strokes and shadows use that canvas's axes.
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
375
|
+
let fillRect = forDrawing
|
|
376
|
+
? this._getSelfRectForDrawing()
|
|
377
|
+
: this.getSelfRect();
|
|
378
|
+
if (forDrawing && !this.strokeScaleEnabled()) {
|
|
379
|
+
fillRect = this._transformedRect(fillRect, relativeTo);
|
|
380
|
+
}
|
|
381
|
+
else if (fillRect.width < 0 || fillRect.height < 0) {
|
|
382
|
+
// The identity transform normalizes negative sizes.
|
|
383
|
+
fillRect = new Transform()._getTransformedRect(fillRect);
|
|
384
|
+
}
|
|
381
385
|
const applyStroke = !config.skipStroke && this.hasStroke();
|
|
382
386
|
const strokeWidth = applyStroke
|
|
383
387
|
? forDrawing
|
|
@@ -19,6 +19,7 @@ export interface TransformerConfig extends ContainerConfig {
|
|
|
19
19
|
rotateAnchorOffset?: number;
|
|
20
20
|
rotateAnchorAngle?: number;
|
|
21
21
|
rotateAnchorCursor?: string;
|
|
22
|
+
rotateMode?: 'handle' | 'corners' | 'both';
|
|
22
23
|
borderEnabled?: boolean;
|
|
23
24
|
borderStroke?: string;
|
|
24
25
|
borderStrokeWidth?: number;
|
|
@@ -55,6 +56,7 @@ export interface TransformerConfig extends ContainerConfig {
|
|
|
55
56
|
* @param {Number} [config.rotateAnchorOffset] Default is 50
|
|
56
57
|
* @param {Number} [config.rotateAnchorAngle] Angle of the rotate anchor around the box, in degrees. Default is 0 (top)
|
|
57
58
|
* @param {String} [config.rotateAnchorCursor] Default is crosshair
|
|
59
|
+
* @param {String} [config.rotateMode] How the user rotates: 'handle' (the rotate anchor), 'corners' (invisible zones just outside the corners) or 'both'. Default is 'handle'
|
|
58
60
|
* @param {Number} [config.padding] Default is 0
|
|
59
61
|
* @param {Boolean} [config.borderEnabled] Should we draw border? Default is true
|
|
60
62
|
* @param {String} [config.borderStroke] Border stroke color
|
|
@@ -66,7 +68,7 @@ export interface TransformerConfig extends ContainerConfig {
|
|
|
66
68
|
* @param {Number} [config.anchorStrokeWidth] Anchor stroke size
|
|
67
69
|
* @param {Number} [config.anchorSize] Default is 10
|
|
68
70
|
* @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
|
|
69
|
-
* @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? Default is 'default'
|
|
71
|
+
* @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? 'none' also turns off the Shift rotation snap. Default is 'default'
|
|
70
72
|
* @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
|
|
71
73
|
* @param {Array} [config.enabledAnchors] Array of names of enabled handles
|
|
72
74
|
* @param {Boolean} [config.flipEnabled] Can we flip/mirror shape on transform?. True by default
|
|
@@ -105,6 +107,10 @@ export declare class Transformer extends Group {
|
|
|
105
107
|
_fitting: boolean;
|
|
106
108
|
_transformWindow: Window | null;
|
|
107
109
|
_anchorDragOffset: Vector2d;
|
|
110
|
+
_rotateStart?: {
|
|
111
|
+
pointerAngle: number;
|
|
112
|
+
rotation: number;
|
|
113
|
+
};
|
|
108
114
|
sin: number;
|
|
109
115
|
cos: number;
|
|
110
116
|
_cursorChange: boolean;
|
|
@@ -179,7 +185,8 @@ export declare class Transformer extends Group {
|
|
|
179
185
|
getWidth(): any;
|
|
180
186
|
getHeight(): any;
|
|
181
187
|
_createElements(): void;
|
|
182
|
-
_createAnchor(name:
|
|
188
|
+
_createAnchor(name: string, className?: string): Rect;
|
|
189
|
+
_setAnchorCursor(name: string, rad?: number): void;
|
|
183
190
|
_createBack(): void;
|
|
184
191
|
_handleMouseDown(e: any): void;
|
|
185
192
|
_handleMouseMove(e: any): void;
|
|
@@ -236,6 +243,7 @@ export declare class Transformer extends Group {
|
|
|
236
243
|
rotateAnchorAngle: GetSet<number, this>;
|
|
237
244
|
rotationSnapTolerance: GetSet<number, this>;
|
|
238
245
|
rotateAnchorCursor: GetSet<string, this>;
|
|
246
|
+
rotateMode: GetSet<'handle' | 'corners' | 'both', this>;
|
|
239
247
|
padding: GetSet<number, this>;
|
|
240
248
|
borderEnabled: GetSet<boolean, this>;
|
|
241
249
|
borderStroke: GetSet<string, this>;
|
|
@@ -14,6 +14,7 @@ const ATTR_CHANGE_LIST = [
|
|
|
14
14
|
'rotateAnchorOffsetChange',
|
|
15
15
|
'rotateAnchorAngleChange',
|
|
16
16
|
'rotateEnabledChange',
|
|
17
|
+
'rotateModeChange',
|
|
17
18
|
'enabledAnchorsChange',
|
|
18
19
|
'anchorSizeChange',
|
|
19
20
|
'borderEnabledChange',
|
|
@@ -62,10 +63,37 @@ const ANGLES = {
|
|
|
62
63
|
'bottom-right': 135,
|
|
63
64
|
};
|
|
64
65
|
const TOUCH_DEVICE = 'ontouchstart' in Konva._global;
|
|
66
|
+
// invisible zones just outside the corners, the rotate area of rotateMode 'corners'
|
|
67
|
+
const ROTATE_ZONE_NAMES = [
|
|
68
|
+
'rotater-top-left',
|
|
69
|
+
'rotater-top-right',
|
|
70
|
+
'rotater-bottom-left',
|
|
71
|
+
'rotater-bottom-right',
|
|
72
|
+
];
|
|
73
|
+
function isRotateAnchor(anchorName) {
|
|
74
|
+
return anchorName.startsWith('rotater');
|
|
75
|
+
}
|
|
76
|
+
// with Shift pressed the rotation snaps to this step
|
|
77
|
+
const SHIFT_ROTATION_STEP = Math.PI / 12;
|
|
78
|
+
// curved double arrow drawn for the top-right corner, rotated for others
|
|
79
|
+
function getRotateZoneCursor(deg, fallback) {
|
|
80
|
+
const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24">' +
|
|
81
|
+
`<g transform="rotate(${deg} 12 12)" stroke-linejoin="round">` +
|
|
82
|
+
'<path d="M5 8 A11 11 0 0 1 16 19" fill="none" stroke="white" stroke-width="4"/>' +
|
|
83
|
+
'<path d="M2 8 L6 4.5 L6 11.5 Z M16 22 L12.5 18 L19.5 18 Z" fill="black" stroke="white"/>' +
|
|
84
|
+
'<path d="M5 8 A11 11 0 0 1 16 19" fill="none" stroke="black" stroke-width="1.5"/>' +
|
|
85
|
+
'</g></svg>';
|
|
86
|
+
return `url("data:image/svg+xml,${encodeURIComponent(svg)}") 12 12, ${fallback}`;
|
|
87
|
+
}
|
|
65
88
|
function getCursor(anchorName, rad, rotateCursor) {
|
|
66
89
|
if (anchorName === 'rotater') {
|
|
67
90
|
return rotateCursor;
|
|
68
91
|
}
|
|
92
|
+
if (isRotateAnchor(anchorName)) {
|
|
93
|
+
const corner = anchorName.slice('rotater-'.length);
|
|
94
|
+
const deg = Math.round(Util.radToDeg(rad) + ANGLES[corner] - 45);
|
|
95
|
+
return getRotateZoneCursor(deg, rotateCursor);
|
|
96
|
+
}
|
|
69
97
|
rad += Util.degToRad(ANGLES[anchorName] || 0);
|
|
70
98
|
const angle = ((Util.radToDeg(rad) % 360) + 360) % 360;
|
|
71
99
|
if (Util._inRange(angle, 315 + 22.5, 360) || Util._inRange(angle, 0, 22.5)) {
|
|
@@ -207,6 +235,7 @@ const activeTransformers = new Set();
|
|
|
207
235
|
* @param {Number} [config.rotateAnchorOffset] Default is 50
|
|
208
236
|
* @param {Number} [config.rotateAnchorAngle] Angle of the rotate anchor around the box, in degrees. Default is 0 (top)
|
|
209
237
|
* @param {String} [config.rotateAnchorCursor] Default is crosshair
|
|
238
|
+
* @param {String} [config.rotateMode] How the user rotates: 'handle' (the rotate anchor), 'corners' (invisible zones just outside the corners) or 'both'. Default is 'handle'
|
|
210
239
|
* @param {Number} [config.padding] Default is 0
|
|
211
240
|
* @param {Boolean} [config.borderEnabled] Should we draw border? Default is true
|
|
212
241
|
* @param {String} [config.borderStroke] Border stroke color
|
|
@@ -218,7 +247,7 @@ const activeTransformers = new Set();
|
|
|
218
247
|
* @param {Number} [config.anchorStrokeWidth] Anchor stroke size
|
|
219
248
|
* @param {Number} [config.anchorSize] Default is 10
|
|
220
249
|
* @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
|
|
221
|
-
* @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? Default is 'default'
|
|
250
|
+
* @param {String} [config.shiftBehavior] How does transformer react on shift key press when we are moving edges? 'none' also turns off the Shift rotation snap. Default is 'default'
|
|
222
251
|
* @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
|
|
223
252
|
* @param {Array} [config.enabledAnchors] Array of names of enabled handles
|
|
224
253
|
* @param {Boolean} [config.flipEnabled] Can we flip/mirror shape on transform?. True by default
|
|
@@ -619,18 +648,27 @@ export class Transformer extends Group {
|
|
|
619
648
|
}
|
|
620
649
|
_createElements() {
|
|
621
650
|
this._createBack();
|
|
651
|
+
// below the resize anchors, so a resize anchor wins where they overlap
|
|
652
|
+
ROTATE_ZONE_NAMES.forEach((name) => {
|
|
653
|
+
// invisible, but its fill keeps it in the hit graph
|
|
654
|
+
this._createAnchor(name, '_rotate-zone').setAttrs({
|
|
655
|
+
fill: 'transparent',
|
|
656
|
+
strokeEnabled: false,
|
|
657
|
+
visible: false,
|
|
658
|
+
});
|
|
659
|
+
});
|
|
622
660
|
ANCHORS_NAMES.forEach((name) => {
|
|
623
661
|
this._createAnchor(name);
|
|
624
662
|
});
|
|
625
663
|
this._createAnchor('rotater');
|
|
626
664
|
this._elementsCreated = true;
|
|
627
665
|
}
|
|
628
|
-
_createAnchor(name) {
|
|
666
|
+
_createAnchor(name, className = '_anchor') {
|
|
629
667
|
const anchor = new Rect({
|
|
630
668
|
stroke: 'rgb(0, 161, 255)',
|
|
631
669
|
fill: 'white',
|
|
632
670
|
strokeWidth: 1,
|
|
633
|
-
name: name + '
|
|
671
|
+
name: name + ' ' + className,
|
|
634
672
|
dragDistance: 0,
|
|
635
673
|
// make it draggable,
|
|
636
674
|
// so activating the anchor will not start drag&drop of any parent
|
|
@@ -653,11 +691,7 @@ export class Transformer extends Group {
|
|
|
653
691
|
anchor.on('mouseenter', () => {
|
|
654
692
|
if (this.isTransforming())
|
|
655
693
|
return;
|
|
656
|
-
|
|
657
|
-
const rotateCursor = this.rotateAnchorCursor();
|
|
658
|
-
const cursor = getCursor(name, rad, rotateCursor);
|
|
659
|
-
anchor.getStage().content &&
|
|
660
|
-
(anchor.getStage().content.style.cursor = cursor);
|
|
694
|
+
this._setAnchorCursor(name);
|
|
661
695
|
this._cursorChange = true;
|
|
662
696
|
});
|
|
663
697
|
anchor.on('mouseout', () => {
|
|
@@ -668,6 +702,14 @@ export class Transformer extends Group {
|
|
|
668
702
|
(anchor.getStage().content.style.cursor = '');
|
|
669
703
|
});
|
|
670
704
|
this.add(anchor);
|
|
705
|
+
return anchor;
|
|
706
|
+
}
|
|
707
|
+
_setAnchorCursor(name, rad = Konva.getAngle(this.rotation())) {
|
|
708
|
+
var _a;
|
|
709
|
+
const content = (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a.content;
|
|
710
|
+
if (content) {
|
|
711
|
+
content.style.cursor = getCursor(name, rad, this.rotateAnchorCursor());
|
|
712
|
+
}
|
|
671
713
|
}
|
|
672
714
|
_createBack() {
|
|
673
715
|
const back = new Shape({
|
|
@@ -683,7 +725,9 @@ export class Transformer extends Group {
|
|
|
683
725
|
const height = shape.height();
|
|
684
726
|
ctx.beginPath();
|
|
685
727
|
ctx.rect(-padding, -padding, width + padding * 2, height + padding * 2);
|
|
686
|
-
if (tr.rotateEnabled() &&
|
|
728
|
+
if (tr.rotateEnabled() &&
|
|
729
|
+
tr.rotateLineVisible() &&
|
|
730
|
+
tr.rotateMode() !== 'corners') {
|
|
687
731
|
const line = getRotaterLine(tr, width, height);
|
|
688
732
|
ctx.moveTo(line.edgeX, line.edgeY);
|
|
689
733
|
ctx.lineTo(line.endX, line.endY);
|
|
@@ -755,6 +799,13 @@ export class Transformer extends Group {
|
|
|
755
799
|
x: pos.x - ap.x,
|
|
756
800
|
y: pos.y - ap.y,
|
|
757
801
|
};
|
|
802
|
+
if (isRotateAnchor(this._movingAnchorName)) {
|
|
803
|
+
const center = getCenter(attrs);
|
|
804
|
+
this._rotateStart = {
|
|
805
|
+
pointerAngle: Math.atan2(pos.y - center.y, pos.x - center.x),
|
|
806
|
+
rotation: attrs.rotation,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
758
809
|
activeTransformers.add(this);
|
|
759
810
|
this._fire('transformstart', { evt: e.evt, target: this.getNode() });
|
|
760
811
|
this._nodes.forEach((target) => {
|
|
@@ -790,19 +841,33 @@ export class Transformer extends Group {
|
|
|
790
841
|
if (oldAbs.x === newAbs.x && oldAbs.y === newAbs.y) {
|
|
791
842
|
return;
|
|
792
843
|
}
|
|
793
|
-
//
|
|
794
|
-
if (this._movingAnchorName
|
|
844
|
+
// rotation is working very differently, so do it first
|
|
845
|
+
if (isRotateAnchor(this._movingAnchorName)) {
|
|
795
846
|
const attrs = this._getNodeRect();
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
//
|
|
799
|
-
//
|
|
800
|
-
const
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
const
|
|
805
|
-
const
|
|
847
|
+
const center = getCenter(attrs);
|
|
848
|
+
const start = this._rotateStart;
|
|
849
|
+
// the grabbed point turns around the center by as much as the box,
|
|
850
|
+
// so the box follows the pointer wherever it was grabbed
|
|
851
|
+
const point = {
|
|
852
|
+
x: newAbs.x + this._anchorDragOffset.x,
|
|
853
|
+
y: newAbs.y + this._anchorDragOffset.y,
|
|
854
|
+
};
|
|
855
|
+
const pointerAngle = Math.atan2(point.y - center.y, point.x - center.x);
|
|
856
|
+
const newRotation = start.rotation + pointerAngle - start.pointerAngle;
|
|
857
|
+
let snappedRot;
|
|
858
|
+
if (e.shiftKey && this.shiftBehavior() !== 'none') {
|
|
859
|
+
snappedRot =
|
|
860
|
+
Math.round(newRotation / SHIFT_ROTATION_STEP) * SHIFT_ROTATION_STEP;
|
|
861
|
+
}
|
|
862
|
+
else {
|
|
863
|
+
const tol = Konva.getAngle(this.rotationSnapTolerance());
|
|
864
|
+
snappedRot = getSnap(this.rotationSnaps(), newRotation, tol);
|
|
865
|
+
}
|
|
866
|
+
// a corner zone cursor shows the direction of the turned corner,
|
|
867
|
+
// set before the transform event so its handlers can override it
|
|
868
|
+
if (this._movingAnchorName !== 'rotater') {
|
|
869
|
+
this._setAnchorCursor(this._movingAnchorName, snappedRot);
|
|
870
|
+
}
|
|
806
871
|
const diff = snappedRot - attrs.rotation;
|
|
807
872
|
const shape = rotateAroundCenter(attrs, diff);
|
|
808
873
|
this._fitNodesInto(shape, e);
|
|
@@ -1232,7 +1297,7 @@ export class Transformer extends Group {
|
|
|
1232
1297
|
}
|
|
1233
1298
|
const styleFunc = this.anchorStyleFunc();
|
|
1234
1299
|
if (styleFunc) {
|
|
1235
|
-
Object.values(this._anchors).forEach((node) => styleFunc(node));
|
|
1300
|
+
Object.values(this._anchors).forEach((node) => node.hasName('_anchor') && styleFunc(node));
|
|
1236
1301
|
}
|
|
1237
1302
|
(_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
|
|
1238
1303
|
}
|
|
@@ -1256,7 +1321,7 @@ export class Transformer extends Group {
|
|
|
1256
1321
|
const resizeEnabled = this.resizeEnabled();
|
|
1257
1322
|
const padding = this.padding();
|
|
1258
1323
|
const anchorSize = this.anchorSize();
|
|
1259
|
-
const anchors = Object.values(this._anchors);
|
|
1324
|
+
const anchors = Object.values(this._anchors).filter((node) => node.hasName('_anchor'));
|
|
1260
1325
|
anchors.forEach((node) => {
|
|
1261
1326
|
node.setAttrs({
|
|
1262
1327
|
width: anchorSize,
|
|
@@ -1321,11 +1386,25 @@ export class Transformer extends Group {
|
|
|
1321
1386
|
offsetY: anchorSize / 2 - padding,
|
|
1322
1387
|
visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
|
|
1323
1388
|
});
|
|
1389
|
+
const rotateMode = this.rotateMode();
|
|
1324
1390
|
const rotaterLine = getRotaterLine(this, width, height);
|
|
1325
1391
|
this._anchors['rotater'].setAttrs({
|
|
1326
1392
|
x: rotaterLine.endX,
|
|
1327
1393
|
y: rotaterLine.endY,
|
|
1328
|
-
visible: this.rotateEnabled(),
|
|
1394
|
+
visible: this.rotateEnabled() && rotateMode !== 'corners',
|
|
1395
|
+
});
|
|
1396
|
+
// each zone is a square that touches its corner from the outside
|
|
1397
|
+
const zoneSize = anchorSize * 2;
|
|
1398
|
+
const zonesVisible = this.rotateEnabled() && rotateMode !== 'handle';
|
|
1399
|
+
const near = -padding - zoneSize;
|
|
1400
|
+
ROTATE_ZONE_NAMES.forEach((name) => {
|
|
1401
|
+
this._anchors[name].setAttrs({
|
|
1402
|
+
x: name.includes('left') ? near : width + padding,
|
|
1403
|
+
y: name.includes('top') ? near : height + padding,
|
|
1404
|
+
width: zoneSize,
|
|
1405
|
+
height: zoneSize,
|
|
1406
|
+
visible: zonesVisible,
|
|
1407
|
+
});
|
|
1329
1408
|
});
|
|
1330
1409
|
this._back.setAttrs({
|
|
1331
1410
|
width: width,
|
|
@@ -1564,6 +1643,23 @@ Factory.addGetterSetter(Transformer, 'rotateAnchorAngle', 0, getNumberValidator(
|
|
|
1564
1643
|
* transformer.rotateAnchorCursor('grab');
|
|
1565
1644
|
*/
|
|
1566
1645
|
Factory.addGetterSetter(Transformer, 'rotateAnchorCursor', 'crosshair');
|
|
1646
|
+
/**
|
|
1647
|
+
* get/set how the user rotates the nodes. 'handle' shows the rotate anchor,
|
|
1648
|
+
* 'corners' makes invisible zones just outside the box corners rotate,
|
|
1649
|
+
* 'both' enables both. Hold Shift while rotating to snap to 15 degree steps, unless shiftBehavior is 'none'.
|
|
1650
|
+
* While a corner zone rotates, `getActiveAnchor()` returns its name, such as 'rotater-top-left'.
|
|
1651
|
+
* @name Konva.Transformer#rotateMode
|
|
1652
|
+
* @method
|
|
1653
|
+
* @param {String} mode
|
|
1654
|
+
* @returns {String}
|
|
1655
|
+
* @example
|
|
1656
|
+
* // get
|
|
1657
|
+
* var rotateMode = transformer.rotateMode();
|
|
1658
|
+
*
|
|
1659
|
+
* // set
|
|
1660
|
+
* transformer.rotateMode('corners');
|
|
1661
|
+
*/
|
|
1662
|
+
Factory.addGetterSetter(Transformer, 'rotateMode', 'handle');
|
|
1567
1663
|
/**
|
|
1568
1664
|
* get/set distance for rotation tolerance
|
|
1569
1665
|
* @name Konva.Transformer#rotationSnapTolerance
|