- Time to add edges. Make functions
function center(rect) {
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 }
}
function createLineEdge() {
let start = undefined
let end = undefined
return {
connect: (s, e) => {
start = s
end = e
},
draw: () => {
const canvas = document.getElementById('graphpanel')
const ctx = canvas.getContext('2d')
ctx.beginPath()
const p = ... // Just pick the center of the bounds for now
const q = ... // Not the "connection points" that graphed2 uses
ctx.moveTo(p.x, p.y)
ctx.lineTo(q.x, q.y)
ctx.stroke()
}
}
}
- Add this method (translated from
Graph.java
) to the Graph
class:
connect(e, p1, p2) {
const n1 = this.findNode(p1)
const n2 = this.findNode(p2)
if (n1 !== undefined && n2 !== undefined) {
e.connect(n1, n2)
this.edges.push(e)
return true
}
return false
}
- Add an edge to the graph:
const e = createLineEdge()
graph.connect(e, { x: 20, y: 20 }, { x: 40, y: 40 })
- There is one more thing that you need to do to get the edge to actually show up. Hint:
Graph.draw
.
- Now you should be able to drag a node and see the edge redraw.