fsm-designer/js/components/start-connection.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-02-27 20:22:38 +00:00
class StartConnection {
constructor(state, start) {
2019-04-03 14:15:15 +00:00
this.id = guid();
2019-02-27 20:22:38 +00:00
this.state = state;
this.deltaX = 0;
this.deltaY = 0;
2019-04-03 14:15:15 +00:00
this._text = '';
2019-02-27 20:22:38 +00:00
2019-04-03 14:15:15 +00:00
if (start) {
2019-02-27 20:22:38 +00:00
this.setAnchorPoint(start.x, start.y);
}
}
2019-04-03 14:15:15 +00:00
get text() {
return this._text;
}
set text(value) {
documents[activeDocument].addChange('startconnection', this.id, 'edit', ['_text'], [this._text], true);
this._text = value;
documents[activeDocument].addChange('startconnection', this.id, 'edit', ['_text'], [this._text]);
}
2019-02-27 20:22:38 +00:00
setAnchorPoint(x, y) {
this.deltaX = x - this.state.x;
this.deltaY = y - this.state.y;
2019-04-03 14:15:15 +00:00
if (Math.abs(this.deltaX) < settings.snapToPadding) {
2019-02-27 20:22:38 +00:00
this.deltaX = 0;
}
2019-04-03 14:15:15 +00:00
if (Math.abs(this.deltaY) < settings.snapToPadding) {
2019-02-27 20:22:38 +00:00
this.deltaY = 0;
}
}
getEndPoints() {
const startX = this.state.x + this.deltaX;
const startY = this.state.y + this.deltaY;
const end = this.state.closestPointOnCircle(startX, startY);
return {
start: {x: startX, y: startY},
end,
};
}
draw() {
const points = this.getEndPoints();
2019-02-27 20:22:38 +00:00
ctx.strokeStyle = ctx.fillStyle = settings.colors.getColor(this);
2019-02-27 20:22:38 +00:00
ctx.beginPath();
2019-02-27 20:22:38 +00:00
ctx.moveTo(points.start.x, points.start.y);
ctx.lineTo(points.end.x, points.end.y);
2019-02-27 20:22:38 +00:00
ctx.stroke();
ctx.closePath();
const textAngle = Math.atan2(points.start.y - points.end.y, points.start.x - points.end.x);
ctx.drawText(this.text, points.start.x, points.start.y, textAngle, selectedObject === this);
ctx.drawArrow(points.end.x, points.end.y, Math.atan2(-this.deltaY, -this.deltaX))
}
containsPoint(x, y) {
const points = this.getEndPoints();
const dx = points.end.x - points.start.x,
dy = points.end.y - points.start.y,
length = Math.sqrt(dx ** 2 + dy ** 2),
percent = (dx * (x - points.start.x) + dy * (y - points.start.y)) / (length ** 2),
distance = (dx * (y - points.start.y) - dy * (x - points.start.x)) / length;
return (percent > 0 && percent < 1 && Math.abs(distance) < settings.hitTargetPadding);
2019-02-27 20:22:38 +00:00
}
}