Skip to content

Commit

Permalink
update component connections when rotated
Browse files Browse the repository at this point in the history
  • Loading branch information
espmaniac authored Oct 10, 2024
1 parent 939d758 commit dbaea86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
5 changes: 4 additions & 1 deletion component.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class Component {

ctx.fillText(this.value, this.x + (this.width - valueMetric.width) / 2, this.y - (valueMetric.actualBoundingBoxAscent + valueMetric.actualBoundingBoxDescent) / 2);
ctx.fillText(this.name, this.x + (this.width - nameMetric.width) / 2, this.y - (nameMetric.actualBoundingBoxAscent + nameMetric.actualBoundingBoxDescent) * 2);

ctx.restore();
}

Expand All @@ -71,6 +70,10 @@ class Component {
if (onMove) return;

/* update onMouseUp */
this.update();
}

update() {
let rotateLeft = rotatePoint(
{x: this.x - this.width, y: this.y + this.height / 2},
{x: this.rotationPointX(), y: this.rotationPointY()},
Expand Down
57 changes: 33 additions & 24 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ document.addEventListener('keypress', function(e) { // rotate

selectedComponents[0].angle += 45;
selectedComponents[0].angle %= 360;

if (!isDragging) {
selectedComponents[0].update();
updateComponentConnections(selectedComponents[0]);
}

renderAll();
}
Expand Down Expand Up @@ -533,6 +538,33 @@ function junction(x,y, node1,node2) {
}
}

function updateComponentConnections(component) {
deleteNode(component.node1); // delete all connection
deleteNode(component.node2); // delete all connection
connectNodes(component.node1, component.node2);

for (let i = 0; i < wires.length; ++i) {
connectComponentLine(component, wires[i]);
}

for (let i in components) {
connectComponentComponent(component, components[i]);

}

// Some connected nodes may have more than or equal to 3 connections and no junction
// this solution seems to fix that problem
if (component.node1.connections.length >= 3) {
let pos = component.node1;
junction(pos.x, pos.y, component.node1, component.node1.connections[1].node);
}

if (component.node2.connections.length >= 3) {
let pos = component.node2;
junction(pos.x, pos.y, component.node2, component.node2.connections[1].node);
}
}

function deleteJunction(j) {
if (!j) return;
for (let i = 0; i < junctions.length; ++i) {
Expand Down Expand Up @@ -611,36 +643,13 @@ canvas.addEventListener('mouseup', function(event) {
if (selectedComponents.length > 0) {
let component = selectedComponents[0];

deleteNode(component.node1); // delete all connection
deleteNode(component.node2); // delete all connection
connectNodes(component.node1, component.node2);

component.move(
snapToGrid((mouseX / zoom) - mouseOffsetX),
snapToGrid((mouseY / zoom) - mouseOffsetY),
false, // onMove
);

for (let i = 0; i < wires.length; ++i) {
connectComponentLine(component, wires[i]);
}

for (let i in components) {
connectComponentComponent(component, components[i]);

}

// Some connected nodes may have more than or equal to 3 connections and no junction
// this solution seems to fix that problem
if (component.node1.connections.length >= 3) {
let pos = component.node1;
junction(pos.x, pos.y, component.node1, component.node1.connections[1].node);
}

if (component.node2.connections.length >= 3) {
let pos = component.node2;
junction(pos.x, pos.y, component.node2, component.node2.connections[1].node);
}
updateComponentConnections(component);

}
else {
Expand Down
10 changes: 5 additions & 5 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class Node {
}

hitTest(x, y) {
let r = 5;
let dx1 = x - this.x;
let dy1 = y - this.y;
const r = 5;
let dx = x - this.x;
let dy = y - this.y;

let dist1 = Math.sqrt((dx1 * dx1) + (dy1 * dy1));
let dist = Math.sqrt((dx * dx) + (dy * dy));

return (dist1 <= 5);
return (dist <= r);
}
}

Expand Down

0 comments on commit dbaea86

Please sign in to comment.