|
@@ -1,134 +0,0 @@
|
|
|
-import { Transformer as BaseTransformer, TransformerConfig } from "konva/lib/shapes/Transformer";
|
|
|
-import * as Util_1 from "konva/lib/Util";
|
|
|
-import * as Global_1 from "konva/lib/Global";
|
|
|
-import { KonvaEventObject } from "konva/lib/Node";
|
|
|
-
|
|
|
-export class Transformer extends BaseTransformer {
|
|
|
- constructor(config?: TransformerConfig) {
|
|
|
- super(config)
|
|
|
- }
|
|
|
-
|
|
|
- __getNodeRect(): { x: number; y: number; width: number; height: number; rotation: number; } {
|
|
|
- return super.__getNodeRect()
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- _fitNodesInto(newAttrs: any, evt: KonvaEventObject<any>) {
|
|
|
- const oldAttrs = this._getNodeRect();
|
|
|
- const minSize = 1;
|
|
|
- if (Util_1.Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
|
|
|
- this.update();
|
|
|
- return;
|
|
|
- }
|
|
|
- if (Util_1.Util._inRange(newAttrs.height, -this.padding() * 2 - minSize, minSize)) {
|
|
|
- this.update();
|
|
|
- return;
|
|
|
- }
|
|
|
- const t = new Util_1.Transform();
|
|
|
- t.rotate(Global_1.Konva.getAngle(this.rotation()));
|
|
|
- if (this._movingAnchorName &&
|
|
|
- newAttrs.width < 0 &&
|
|
|
- this._movingAnchorName.indexOf('left') >= 0) {
|
|
|
- const offset = t.point({
|
|
|
- x: -this.padding() * 2,
|
|
|
- y: 0,
|
|
|
- });
|
|
|
- newAttrs.x += offset.x;
|
|
|
- newAttrs.y += offset.y;
|
|
|
- newAttrs.width += this.padding() * 2;
|
|
|
- this._movingAnchorName = this._movingAnchorName.replace('left', 'right');
|
|
|
- this._anchorDragOffset.x -= offset.x;
|
|
|
- this._anchorDragOffset.y -= offset.y;
|
|
|
- }
|
|
|
- else if (this._movingAnchorName &&
|
|
|
- newAttrs.width < 0 &&
|
|
|
- this._movingAnchorName.indexOf('right') >= 0) {
|
|
|
- const offset = t.point({
|
|
|
- x: this.padding() * 2,
|
|
|
- y: 0,
|
|
|
- });
|
|
|
- this._movingAnchorName = this._movingAnchorName.replace('right', 'left');
|
|
|
- this._anchorDragOffset.x -= offset.x;
|
|
|
- this._anchorDragOffset.y -= offset.y;
|
|
|
- newAttrs.width += this.padding() * 2;
|
|
|
- }
|
|
|
- if (this._movingAnchorName &&
|
|
|
- newAttrs.height < 0 &&
|
|
|
- this._movingAnchorName.indexOf('top') >= 0) {
|
|
|
- const offset = t.point({
|
|
|
- x: 0,
|
|
|
- y: -this.padding() * 2,
|
|
|
- });
|
|
|
- newAttrs.x += offset.x;
|
|
|
- newAttrs.y += offset.y;
|
|
|
- this._movingAnchorName = this._movingAnchorName.replace('top', 'bottom');
|
|
|
- this._anchorDragOffset.x -= offset.x;
|
|
|
- this._anchorDragOffset.y -= offset.y;
|
|
|
- newAttrs.height += this.padding() * 2;
|
|
|
- }
|
|
|
- else if (this._movingAnchorName &&
|
|
|
- newAttrs.height < 0 &&
|
|
|
- this._movingAnchorName.indexOf('bottom') >= 0) {
|
|
|
- const offset = t.point({
|
|
|
- x: 0,
|
|
|
- y: this.padding() * 2,
|
|
|
- });
|
|
|
- this._movingAnchorName = this._movingAnchorName.replace('bottom', 'top');
|
|
|
- this._anchorDragOffset.x -= offset.x;
|
|
|
- this._anchorDragOffset.y -= offset.y;
|
|
|
- newAttrs.height += this.padding() * 2;
|
|
|
- }
|
|
|
- if (this.boundBoxFunc()) {
|
|
|
- const bounded = this.boundBoxFunc()(oldAttrs, newAttrs);
|
|
|
- if (bounded) {
|
|
|
- newAttrs = bounded;
|
|
|
- }
|
|
|
- else {
|
|
|
- Util_1.Util.warn('boundBoxFunc returned falsy. You should return new bound rect from it!');
|
|
|
- }
|
|
|
- }
|
|
|
- const baseSize = 10000000;
|
|
|
- const oldTr = new Util_1.Transform();
|
|
|
- oldTr.translate(oldAttrs.x, oldAttrs.y);
|
|
|
- oldTr.rotate(oldAttrs.rotation);
|
|
|
- oldTr.scale(oldAttrs.width / baseSize, oldAttrs.height / baseSize);
|
|
|
- const newTr = new Util_1.Transform();
|
|
|
- const newScaleX = newAttrs.width / baseSize;
|
|
|
- const newScaleY = newAttrs.height / baseSize;
|
|
|
- if (this.flipEnabled() === false) {
|
|
|
- newTr.translate(newAttrs.x, newAttrs.y);
|
|
|
- newTr.rotate(newAttrs.rotation);
|
|
|
- newTr.translate(newAttrs.width < 0 ? newAttrs.width : 0, newAttrs.height < 0 ? newAttrs.height : 0);
|
|
|
- newTr.scale(Math.abs(newScaleX), Math.abs(newScaleY));
|
|
|
- }
|
|
|
- else {
|
|
|
- newTr.translate(newAttrs.x, newAttrs.y);
|
|
|
- newTr.rotate(newAttrs.rotation);
|
|
|
- newTr.scale(newScaleX, newScaleY);
|
|
|
- }
|
|
|
- const delta = newTr.multiply(oldTr.invert());
|
|
|
- this._nodes.forEach((node) => {
|
|
|
- var _a;
|
|
|
- const parentTransform = node.getParent()!.getAbsoluteTransform();
|
|
|
- const localTransform = node.getTransform().copy();
|
|
|
- localTransform.translate(node.offsetX(), node.offsetY());
|
|
|
- const newLocalTransform = new Util_1.Transform();
|
|
|
- newLocalTransform
|
|
|
- .multiply(parentTransform.copy().invert())
|
|
|
- .multiply(delta)
|
|
|
- .multiply(parentTransform)
|
|
|
- .multiply(localTransform);
|
|
|
- const attrs = newLocalTransform.decompose();
|
|
|
- node.setAttrs(attrs);
|
|
|
- (_a = node.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
|
|
|
- });
|
|
|
- this.rotation(Util_1.Util._getRotation(newAttrs.rotation));
|
|
|
- this._nodes.forEach((node) => {
|
|
|
- this._fire('transform', { evt: evt, target: node });
|
|
|
- node._fire('transform', { evt: evt, target: node });
|
|
|
- });
|
|
|
- this._resetTransformCache();
|
|
|
- this.update();
|
|
|
- this.getLayer()!.batchDraw();
|
|
|
- }
|
|
|
-}
|