PNet/js/graphics/SVGArrow.ts

56 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2023-06-22 17:47:04 +02:00
import { HTMLappendIt, Positionable, svgNamespace } from "../types";
2023-06-28 18:22:29 +02:00
import { Tragable } from "./tragable";
2023-06-22 17:47:04 +02:00
2023-06-28 18:22:29 +02:00
export class SVGArrow extends Tragable implements HTMLappendIt {
2023-06-22 17:50:11 +02:00
group: SVGElement;
lineText: SVGElement;
line: SVGElement;
2023-06-22 17:47:04 +02:00
from: Positionable;
to: Positionable;
constructor(from: Positionable, to: Positionable) {
2023-06-28 18:22:29 +02:00
super();
2023-06-22 17:47:04 +02:00
this.from = from;
this.to = to;
this.group = document.createElementNS(svgNamespace, 'g');
this.group.classList.add("PEdge");
this.line = document.createElementNS(svgNamespace, 'line');
this.line.setAttribute('marker-end', "url(#arrowhead)");
this.group.setAttribute('transform', `translate(0, 0)`);
this.lineText = document.createElementNS(svgNamespace, 'text');
this.lineText.classList.add("lineText");
this.lineText.setAttribute('x', '0');
this.lineText.setAttribute('y', '0');
2023-06-28 16:06:31 +02:00
this.lineText.textContent = "1";
2023-06-22 17:47:04 +02:00
this.group.appendChild(this.line);
this.group.appendChild(this.lineText);
2023-06-28 18:22:29 +02:00
this.initOnlyClick(this.group);
2023-06-22 17:47:04 +02:00
this.updatePosition();
}
updatePosition() {
let dx = this.to.x - this.from.x;
let dy = this.to.y - this.from.y;
let l = Math.sqrt(dx * dx + dy * dy);
dx /= l;
dy /= l;
2023-06-22 17:50:11 +02:00
this.line.setAttribute('x1', (this.from.x + dx * 30) + "");
this.line.setAttribute('y1', (this.from.y + dy * 30) + "");
this.line.setAttribute('x2', (this.to.x - dx * 40) + "");
this.line.setAttribute('y2', (this.to.y - dy * 40) + "");
2023-06-28 18:02:41 +02:00
this.lineText.setAttribute('x', (this.from.x + this.to.x) / 2 + "");
this.lineText.setAttribute('y', (this.from.y + this.to.y) / 2 + "");
2023-06-22 17:47:04 +02:00
}
appendIt(svg) {
svg.appendChild(this.group);
}
2023-06-28 18:22:29 +02:00
setDimension(txt: string) {
this.lineText.textContent = txt;
}
2023-06-22 17:47:04 +02:00
}