konva 10.3.1 → 10.3.2
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 +33 -43
- package/konva.js +382 -122
- package/konva.min.js +4 -4
- package/lib/Canvas.d.ts +1 -0
- package/lib/Canvas.js +6 -0
- package/lib/Container.js +2 -2
- package/lib/DragAndDrop.d.ts +13 -10
- package/lib/DragAndDrop.js +18 -13
- package/lib/Global.js +1 -1
- package/lib/Layer.d.ts +1 -0
- package/lib/Layer.js +15 -4
- package/lib/Node.d.ts +3 -2
- package/lib/Node.js +31 -14
- package/lib/PointerEvents.js +12 -2
- package/lib/Shape.js +5 -5
- package/lib/Stage.d.ts +2 -0
- package/lib/Stage.js +34 -19
- package/lib/Util.d.ts +1 -1
- package/lib/Util.js +22 -16
- package/lib/_CoreInternals.d.ts +4 -3
- package/lib/_FullInternals.d.ts +4 -3
- package/lib/shapes/Text.d.ts +2 -0
- package/lib/shapes/Text.js +18 -0
- package/lib/shapes/Transformer.d.ts +3 -2
- package/lib/shapes/Transformer.js +47 -34
- package/package.json +1 -1
package/lib/_FullInternals.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ export declare const Konva: {
|
|
|
62
62
|
isObject(val: any): val is object;
|
|
63
63
|
isValidSelector(selector: any): boolean;
|
|
64
64
|
_sign(number: number): 1 | -1;
|
|
65
|
-
requestAnimFrame(callback: Function): void;
|
|
65
|
+
requestAnimFrame(callback: Function, win?: Window | null): void;
|
|
66
66
|
createCanvasElement(): HTMLCanvasElement;
|
|
67
67
|
createImageElement(): HTMLImageElement;
|
|
68
68
|
_isInDocument(el: any): boolean;
|
|
@@ -167,8 +167,9 @@ export declare const Konva: {
|
|
|
167
167
|
startEvent?: any;
|
|
168
168
|
dragStatus: "ready" | "dragging" | "stopped";
|
|
169
169
|
}>;
|
|
170
|
-
|
|
171
|
-
|
|
170
|
+
_listenToWindow(win: Window): void;
|
|
171
|
+
_drag(evt: any, win?: Window): void;
|
|
172
|
+
_endDragBefore(evt?: any, win?: Window): void;
|
|
172
173
|
_endDragAfter(evt: any): void;
|
|
173
174
|
};
|
|
174
175
|
Shape: typeof import("./Shape.ts").Shape;
|
package/lib/shapes/Text.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ export declare class Text extends Shape<TextConfig> {
|
|
|
40
40
|
_partialText: string;
|
|
41
41
|
_partialTextX: number;
|
|
42
42
|
_partialTextY: number;
|
|
43
|
+
_partialFillStyle?: string | CanvasGradient | CanvasPattern;
|
|
44
|
+
_partialStrokeStyle?: string | CanvasGradient | CanvasPattern;
|
|
43
45
|
textWidth: number;
|
|
44
46
|
textHeight: number;
|
|
45
47
|
constructor(config?: TextConfig);
|
package/lib/shapes/Text.js
CHANGED
|
@@ -101,10 +101,16 @@ function getDummyContext() {
|
|
|
101
101
|
return dummyContext;
|
|
102
102
|
}
|
|
103
103
|
function _fillFunc(context) {
|
|
104
|
+
if (this._partialFillStyle) {
|
|
105
|
+
context.setAttr('fillStyle', this._partialFillStyle);
|
|
106
|
+
}
|
|
104
107
|
context.fillText(this._partialText, this._partialTextX, this._partialTextY);
|
|
105
108
|
}
|
|
106
109
|
function _strokeFunc(context) {
|
|
107
110
|
context.setAttr('miterLimit', 2);
|
|
111
|
+
if (this._partialStrokeStyle) {
|
|
112
|
+
context.setAttr('strokeStyle', this._partialStrokeStyle);
|
|
113
|
+
}
|
|
108
114
|
context.strokeText(this._partialText, this._partialTextX, this._partialTextY);
|
|
109
115
|
}
|
|
110
116
|
function checkDefaultFill(config) {
|
|
@@ -156,6 +162,8 @@ export class Text extends Shape {
|
|
|
156
162
|
alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
|
|
157
163
|
}
|
|
158
164
|
context.translate(padding, alignY + padding);
|
|
165
|
+
const fillStyleBefore = charRenderFunc ? context.fillStyle : undefined;
|
|
166
|
+
const strokeStyleBefore = charRenderFunc ? context.strokeStyle : undefined;
|
|
159
167
|
for (n = 0; n < textArrLen; n++) {
|
|
160
168
|
let lineTranslateX = 0;
|
|
161
169
|
let lineTranslateY = 0;
|
|
@@ -213,9 +221,19 @@ export class Text extends Shape {
|
|
|
213
221
|
width: this.measureSize(letter).width,
|
|
214
222
|
context,
|
|
215
223
|
});
|
|
224
|
+
const fillStyleAfter = context.fillStyle;
|
|
225
|
+
if (fillStyleAfter !== fillStyleBefore) {
|
|
226
|
+
this._partialFillStyle = fillStyleAfter;
|
|
227
|
+
}
|
|
228
|
+
const strokeStyleAfter = context.strokeStyle;
|
|
229
|
+
if (strokeStyleAfter !== strokeStyleBefore) {
|
|
230
|
+
this._partialStrokeStyle = strokeStyleAfter;
|
|
231
|
+
}
|
|
216
232
|
}
|
|
217
233
|
context.fillStrokeShape(this);
|
|
218
234
|
if (charRenderFunc) {
|
|
235
|
+
this._partialFillStyle = undefined;
|
|
236
|
+
this._partialStrokeStyle = undefined;
|
|
219
237
|
context.restore();
|
|
220
238
|
}
|
|
221
239
|
lineTranslateX += this.measureSize(letter).width + letterSpacing;
|
|
@@ -42,6 +42,7 @@ export declare class Transformer extends Group {
|
|
|
42
42
|
_nodes: Array<Node>;
|
|
43
43
|
_movingAnchorName: string | null;
|
|
44
44
|
_transforming: boolean;
|
|
45
|
+
_transformWindow: Window | null;
|
|
45
46
|
_anchorDragOffset: Vector2d;
|
|
46
47
|
sin: number;
|
|
47
48
|
cos: number;
|
|
@@ -87,8 +88,8 @@ export declare class Transformer extends Group {
|
|
|
87
88
|
_handleMouseUp(e: any): void;
|
|
88
89
|
getAbsoluteTransform(): Transform;
|
|
89
90
|
_removeEvents(e?: any): void;
|
|
90
|
-
_fitNodesInto(newAttrs: any, evt?: any): void;
|
|
91
|
-
_doFitNodesInto(newAttrs: any, evt?: any): void;
|
|
91
|
+
_fitNodesInto(newAttrs: any, evt?: any, anchorProjected?: boolean): void;
|
|
92
|
+
_doFitNodesInto(newAttrs: any, evt?: any, anchorProjected?: boolean): void;
|
|
92
93
|
_scheduleUpdate(): void;
|
|
93
94
|
forceUpdate(): void;
|
|
94
95
|
_batchChangeChild(selector: string, attrs: any): void;
|
|
@@ -146,6 +146,7 @@ export class Transformer extends Group {
|
|
|
146
146
|
super(config);
|
|
147
147
|
this._movingAnchorName = null;
|
|
148
148
|
this._transforming = false;
|
|
149
|
+
this._transformWindow = null;
|
|
149
150
|
this._elementsCreated = false;
|
|
150
151
|
this._updateScheduled = false;
|
|
151
152
|
this._createElements();
|
|
@@ -476,6 +477,7 @@ export class Transformer extends Group {
|
|
|
476
477
|
});
|
|
477
478
|
}
|
|
478
479
|
_handleMouseDown(e) {
|
|
480
|
+
var _a;
|
|
479
481
|
if (this._transforming) {
|
|
480
482
|
return;
|
|
481
483
|
}
|
|
@@ -486,11 +488,13 @@ export class Transformer extends Group {
|
|
|
486
488
|
const hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
|
|
487
489
|
this.sin = Math.abs(height / hypotenuse);
|
|
488
490
|
this.cos = Math.abs(width / hypotenuse);
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
491
|
+
const win = (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow();
|
|
492
|
+
this._transformWindow = win || null;
|
|
493
|
+
if (win) {
|
|
494
|
+
win.addEventListener('mousemove', this._handleMouseMove);
|
|
495
|
+
win.addEventListener('touchmove', this._handleMouseMove);
|
|
496
|
+
win.addEventListener('mouseup', this._handleMouseUp, true);
|
|
497
|
+
win.addEventListener('touchend', this._handleMouseUp, true);
|
|
494
498
|
}
|
|
495
499
|
this._transforming = true;
|
|
496
500
|
const ap = e.target.getAbsolutePosition();
|
|
@@ -554,8 +558,10 @@ export class Transformer extends Group {
|
|
|
554
558
|
keepProportion = this.keepRatio() || e.shiftKey;
|
|
555
559
|
}
|
|
556
560
|
let centeredScaling = this.centeredScaling() || e.altKey;
|
|
561
|
+
let anchorProjected = false;
|
|
557
562
|
if (this._movingAnchorName === 'top-left') {
|
|
558
563
|
if (keepProportion) {
|
|
564
|
+
anchorProjected = true;
|
|
559
565
|
const comparePoint = centeredScaling
|
|
560
566
|
? {
|
|
561
567
|
x: this.width() / 2,
|
|
@@ -580,6 +586,7 @@ export class Transformer extends Group {
|
|
|
580
586
|
}
|
|
581
587
|
else if (this._movingAnchorName === 'top-right') {
|
|
582
588
|
if (keepProportion) {
|
|
589
|
+
anchorProjected = true;
|
|
583
590
|
const comparePoint = centeredScaling
|
|
584
591
|
? {
|
|
585
592
|
x: this.width() / 2,
|
|
@@ -610,6 +617,7 @@ export class Transformer extends Group {
|
|
|
610
617
|
}
|
|
611
618
|
else if (this._movingAnchorName === 'bottom-left') {
|
|
612
619
|
if (keepProportion) {
|
|
620
|
+
anchorProjected = true;
|
|
613
621
|
const comparePoint = centeredScaling
|
|
614
622
|
? {
|
|
615
623
|
x: this.width() / 2,
|
|
@@ -637,6 +645,7 @@ export class Transformer extends Group {
|
|
|
637
645
|
}
|
|
638
646
|
else if (this._movingAnchorName === 'bottom-right') {
|
|
639
647
|
if (keepProportion) {
|
|
648
|
+
anchorProjected = true;
|
|
640
649
|
const comparePoint = centeredScaling
|
|
641
650
|
? {
|
|
642
651
|
x: this.width() / 2,
|
|
@@ -688,7 +697,7 @@ export class Transformer extends Group {
|
|
|
688
697
|
width: width,
|
|
689
698
|
height: height,
|
|
690
699
|
rotation: Konva.getAngle(this.rotation()),
|
|
691
|
-
}, e);
|
|
700
|
+
}, e, anchorProjected);
|
|
692
701
|
}
|
|
693
702
|
_handleMouseUp(e) {
|
|
694
703
|
this._removeEvents(e);
|
|
@@ -700,11 +709,13 @@ export class Transformer extends Group {
|
|
|
700
709
|
var _a;
|
|
701
710
|
if (this._transforming) {
|
|
702
711
|
this._transforming = false;
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
712
|
+
const win = this._transformWindow;
|
|
713
|
+
this._transformWindow = null;
|
|
714
|
+
if (win) {
|
|
715
|
+
win.removeEventListener('mousemove', this._handleMouseMove);
|
|
716
|
+
win.removeEventListener('touchmove', this._handleMouseMove);
|
|
717
|
+
win.removeEventListener('mouseup', this._handleMouseUp, true);
|
|
718
|
+
win.removeEventListener('touchend', this._handleMouseUp, true);
|
|
708
719
|
}
|
|
709
720
|
const node = this.getNode();
|
|
710
721
|
activeTransformersCount--;
|
|
@@ -720,17 +731,17 @@ export class Transformer extends Group {
|
|
|
720
731
|
this._movingAnchorName = null;
|
|
721
732
|
}
|
|
722
733
|
}
|
|
723
|
-
_fitNodesInto(newAttrs, evt) {
|
|
734
|
+
_fitNodesInto(newAttrs, evt, anchorProjected = false) {
|
|
724
735
|
const prevAutoDraw = Konva.autoDrawEnabled;
|
|
725
736
|
Konva.autoDrawEnabled = false;
|
|
726
737
|
try {
|
|
727
|
-
return this._doFitNodesInto(newAttrs, evt);
|
|
738
|
+
return this._doFitNodesInto(newAttrs, evt, anchorProjected);
|
|
728
739
|
}
|
|
729
740
|
finally {
|
|
730
741
|
Konva.autoDrawEnabled = prevAutoDraw;
|
|
731
742
|
}
|
|
732
743
|
}
|
|
733
|
-
_doFitNodesInto(newAttrs, evt) {
|
|
744
|
+
_doFitNodesInto(newAttrs, evt, anchorProjected = false) {
|
|
734
745
|
const oldAttrs = this._getNodeRect();
|
|
735
746
|
const minSize = 1;
|
|
736
747
|
if (Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
|
|
@@ -743,57 +754,52 @@ export class Transformer extends Group {
|
|
|
743
754
|
}
|
|
744
755
|
const t = new Transform();
|
|
745
756
|
t.rotate(Konva.getAngle(this.rotation()));
|
|
757
|
+
const flipPadding = anchorProjected ? 0 : this.padding() * 2;
|
|
758
|
+
let widthFlip = null;
|
|
759
|
+
let heightFlip = null;
|
|
746
760
|
if (this._movingAnchorName &&
|
|
747
761
|
newAttrs.width < 0 &&
|
|
748
762
|
this._movingAnchorName.indexOf('left') >= 0) {
|
|
749
763
|
const offset = t.point({
|
|
750
|
-
x: -
|
|
764
|
+
x: -flipPadding,
|
|
751
765
|
y: 0,
|
|
752
766
|
});
|
|
753
767
|
newAttrs.x += offset.x;
|
|
754
768
|
newAttrs.y += offset.y;
|
|
755
|
-
newAttrs.width +=
|
|
756
|
-
|
|
757
|
-
this._anchorDragOffset.x -= offset.x;
|
|
758
|
-
this._anchorDragOffset.y -= offset.y;
|
|
769
|
+
newAttrs.width += flipPadding;
|
|
770
|
+
widthFlip = { axis: 'width', from: 'left', to: 'right', offset };
|
|
759
771
|
}
|
|
760
772
|
else if (this._movingAnchorName &&
|
|
761
773
|
newAttrs.width < 0 &&
|
|
762
774
|
this._movingAnchorName.indexOf('right') >= 0) {
|
|
763
775
|
const offset = t.point({
|
|
764
|
-
x:
|
|
776
|
+
x: flipPadding,
|
|
765
777
|
y: 0,
|
|
766
778
|
});
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
this._anchorDragOffset.y -= offset.y;
|
|
770
|
-
newAttrs.width += this.padding() * 2;
|
|
779
|
+
newAttrs.width += flipPadding;
|
|
780
|
+
widthFlip = { axis: 'width', from: 'right', to: 'left', offset };
|
|
771
781
|
}
|
|
772
782
|
if (this._movingAnchorName &&
|
|
773
783
|
newAttrs.height < 0 &&
|
|
774
784
|
this._movingAnchorName.indexOf('top') >= 0) {
|
|
775
785
|
const offset = t.point({
|
|
776
786
|
x: 0,
|
|
777
|
-
y: -
|
|
787
|
+
y: -flipPadding,
|
|
778
788
|
});
|
|
779
789
|
newAttrs.x += offset.x;
|
|
780
790
|
newAttrs.y += offset.y;
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
this._anchorDragOffset.y -= offset.y;
|
|
784
|
-
newAttrs.height += this.padding() * 2;
|
|
791
|
+
newAttrs.height += flipPadding;
|
|
792
|
+
heightFlip = { axis: 'height', from: 'top', to: 'bottom', offset };
|
|
785
793
|
}
|
|
786
794
|
else if (this._movingAnchorName &&
|
|
787
795
|
newAttrs.height < 0 &&
|
|
788
796
|
this._movingAnchorName.indexOf('bottom') >= 0) {
|
|
789
797
|
const offset = t.point({
|
|
790
798
|
x: 0,
|
|
791
|
-
y:
|
|
799
|
+
y: flipPadding,
|
|
792
800
|
});
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
this._anchorDragOffset.y -= offset.y;
|
|
796
|
-
newAttrs.height += this.padding() * 2;
|
|
801
|
+
newAttrs.height += flipPadding;
|
|
802
|
+
heightFlip = { axis: 'height', from: 'bottom', to: 'top', offset };
|
|
797
803
|
}
|
|
798
804
|
if (this.boundBoxFunc()) {
|
|
799
805
|
const bounded = this.boundBoxFunc()(oldAttrs, newAttrs);
|
|
@@ -804,6 +810,13 @@ export class Transformer extends Group {
|
|
|
804
810
|
Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
|
|
805
811
|
}
|
|
806
812
|
}
|
|
813
|
+
for (const flip of [widthFlip, heightFlip]) {
|
|
814
|
+
if (flip && newAttrs[flip.axis] < 0 && this._movingAnchorName) {
|
|
815
|
+
this._movingAnchorName = this._movingAnchorName.replace(flip.from, flip.to);
|
|
816
|
+
this._anchorDragOffset.x -= flip.offset.x;
|
|
817
|
+
this._anchorDragOffset.y -= flip.offset.y;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
807
820
|
const baseSize = 10000000;
|
|
808
821
|
const oldTr = new Transform();
|
|
809
822
|
oldTr.translate(oldAttrs.x, oldAttrs.y);
|