Skip to content

Commit

Permalink
Add getAnchorShape
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Nov 8, 2024
1 parent 65710ef commit b26defe
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 114 deletions.
19 changes: 6 additions & 13 deletions src/tools/arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {logger} from '../utils/logger';
import {
getLineShape,
DRAW_DEBUG,
getDefaultAnchor
getDefaultAnchor,
getAnchorShape
} from './drawBounds';
import {LabelFactory} from './labelFactory';

Expand Down Expand Up @@ -243,12 +244,8 @@ export class ArrowFactory {
// associated shape
const kline = this.#getShape(group);
// find anchors
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const end = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const begin = getAnchorShape(group, 0);
const end = getAnchorShape(group, 1);

// math shape
// compensate for possible shape drag
Expand Down Expand Up @@ -477,12 +474,8 @@ export class ArrowFactory {
return;
}
// find anchors
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const end = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const begin = getAnchorShape(group, 0);
const end = getAnchorShape(group, 1);

// update 'self' (undo case)
switch (anchor.id()) {
Expand Down
35 changes: 10 additions & 25 deletions src/tools/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {defaults} from '../app/defaults';
import {
isNodeNameShape,
DRAW_DEBUG,
getDefaultAnchor
getDefaultAnchor,
getAnchorShape
} from './drawBounds';
import {LabelFactory} from './labelFactory';

Expand Down Expand Up @@ -194,18 +195,10 @@ export class CircleFactory {
}

// find special points
const left = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const right = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const bottom = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const top = group.getChildren(function (node) {
return node.id() === 'anchor3';
})[0];
const left = getAnchorShape(group, 0);
const right = getAnchorShape(group, 1);
const bottom = getAnchorShape(group, 2);
const top = getAnchorShape(group, 3);

// update 'self' (undo case) and special points
switch (anchor.id()) {
Expand Down Expand Up @@ -425,18 +418,10 @@ export class CircleFactory {
kcircle.radius(radius);

// find anchors
const left = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const right = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const bottom = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const top = group.getChildren(function (node) {
return node.id() === 'anchor3';
})[0];
const left = getAnchorShape(group, 0);
const right = getAnchorShape(group, 1);
const bottom = getAnchorShape(group, 2);
const top = getAnchorShape(group, 3);

const swapX = right.x() < left.x() ? -1 : 1;
const swapY = top.y() < bottom.y() ? 1 : -1;
Expand Down
17 changes: 17 additions & 0 deletions src/tools/drawBounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ export function getLineShape(group) {
return kshape;
}

/**
* Get a Konva.Ellipse anchor shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @param {number} index The anchor index.
* @returns {Konva.Ellipse|undefined} The anchor shape.
*/
export function getAnchorShape(group, index) {
const kshape = group.getChildren(function (node) {
return node.id() === 'anchor' + index;
})[0];
if (!(kshape instanceof Konva.Ellipse)) {
return;
}
return kshape;
}

/**
* @callback testFn
* @param {Konva.Node} node The node.
Expand Down
35 changes: 10 additions & 25 deletions src/tools/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {defaults} from '../app/defaults';
import {
isNodeNameShape,
DRAW_DEBUG,
getDefaultAnchor
getDefaultAnchor,
getAnchorShape
} from './drawBounds';
import {LabelFactory} from './labelFactory';

Expand Down Expand Up @@ -195,18 +196,10 @@ export class EllipseFactory {
}

// find special points
const left = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const right = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const bottom = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const top = group.getChildren(function (node) {
return node.id() === 'anchor3';
})[0];
const left = getAnchorShape(group, 0);
const right = getAnchorShape(group, 1);
const bottom = getAnchorShape(group, 2);
const top = getAnchorShape(group, 3);

// update 'self' (undo case) and special points
switch (anchor.id()) {
Expand Down Expand Up @@ -452,18 +445,10 @@ export class EllipseFactory {
});

// find anchors
const left = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const right = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const bottom = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const top = group.getChildren(function (node) {
return node.id() === 'anchor3';
})[0];
const left = getAnchorShape(group, 0);
const right = getAnchorShape(group, 1);
const bottom = getAnchorShape(group, 2);
const top = getAnchorShape(group, 3);

const swapX = right.x() < left.x() ? -1 : 1;
const swapY = top.y() < bottom.y() ? 1 : -1;
Expand Down
27 changes: 8 additions & 19 deletions src/tools/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {defaults} from '../app/defaults';
import {
getLineShape,
DRAW_DEBUG,
getDefaultAnchor
getDefaultAnchor,
getAnchorShape
} from './drawBounds';
import {LabelFactory} from './labelFactory';

Expand Down Expand Up @@ -245,15 +246,9 @@ export class ProtractorFactory {
// associated shape
const kline = this.#getShape(group);
// find special points
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const mid = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const end = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const begin = getAnchorShape(group, 0);
const mid = getAnchorShape(group, 1);
const end = getAnchorShape(group, 2);

// math shape
// compensate for possible shape drag
Expand Down Expand Up @@ -494,15 +489,9 @@ export class ProtractorFactory {
}

// find special points
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const mid = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const end = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const begin = getAnchorShape(group, 0);
const mid = getAnchorShape(group, 1);
const end = getAnchorShape(group, 2);

// update special points
switch (anchor.id()) {
Expand Down
27 changes: 8 additions & 19 deletions src/tools/rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {defaults} from '../app/defaults';
import {
isNodeNameShape,
DRAW_DEBUG,
getDefaultAnchor
getDefaultAnchor,
getAnchorShape
} from './drawBounds';
import {LabelFactory} from './labelFactory';

Expand Down Expand Up @@ -238,12 +239,8 @@ export class RectangleFactory {
return;
}
// find anchors
const topLeft = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const bottomRight = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const topLeft = getAnchorShape(group, 0);
const bottomRight = getAnchorShape(group, 2);

const pointTopLeft = new Point2D(
topLeft.x(),
Expand Down Expand Up @@ -403,18 +400,10 @@ export class RectangleFactory {
});

// find anchors
const topLeft = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const topRight = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const bottomRight = group.getChildren(function (node) {
return node.id() === 'anchor2';
})[0];
const bottomLeft = group.getChildren(function (node) {
return node.id() === 'anchor3';
})[0];
const topLeft = getAnchorShape(group, 0);
const topRight = getAnchorShape(group, 1);
const bottomRight = getAnchorShape(group, 2);
const bottomLeft = getAnchorShape(group, 3);

// update 'self' (undo case) and other anchors
switch (anchor.id()) {
Expand Down
19 changes: 6 additions & 13 deletions src/tools/ruler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {defaults} from '../app/defaults';
import {
getLineShape,
DRAW_DEBUG,
getDefaultAnchor
getDefaultAnchor,
getAnchorShape
} from './drawBounds';
import {LabelFactory} from './labelFactory';

Expand Down Expand Up @@ -238,12 +239,8 @@ export class RulerFactory {
// associated shape
const kline = this.#getShape(group);
// find anchors
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const end = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const begin = getAnchorShape(group, 0);
const end = getAnchorShape(group, 1);

// math shape
// compensate for possible shape drag
Expand Down Expand Up @@ -487,12 +484,8 @@ export class RulerFactory {
return;
}
// find anchors
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
})[0];
const end = group.getChildren(function (node) {
return node.id() === 'anchor1';
})[0];
const begin = getAnchorShape(group, 0);
const end = getAnchorShape(group, 1);

// update 'self' (undo case)
switch (anchor.id()) {
Expand Down

0 comments on commit b26defe

Please sign in to comment.