In this lesson you will learn how to create the graph UI that is used in the graph theory math lessons.
The only HTML element needed is a canvas to draw and interact with the graph. Since the nodes of the graph are draggable, disable the canvas from being selected. Here is the HTML for the canvas:
<canvas width='400px' height='400px' id='myCanvas' style='border: solid black 2px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none;'></canvas>
Click the code or the description to see the connection.
var canvas var ctx
var WIDTH var HEIGHT
var nodes = [] var edges = []
var startX var startY
var state = {currState: "unselected", currNode: -1}
function clear() { ctx.clearRect(0,0,WIDTH,HEIGHT); }
function circ(x,y,r,state) { if (state == 0) { ctx.fillStyle = '#000000'; } else if (state == 1) { ctx.fillStyle = '#CCCCFF'; } else if (state == 2) { ctx.fillStyle = '#0000FF'; } else { ctx.fillStyle = '#AAAAAA'; } ctx.beginPath(); ctx.arc(x,y,r,0,2*Math.PI); ctx.closePath(); ctx.fill(); }
function init() { canvas = document.getElementById("myCanvas"); ctx = canvas.getContext("2d"); return setInterval(draw, 10); }
window.onload = function() {
WIDTH = 400; HEIGHT = 400;
for (var i = 0; i < 10; i++) { nodes[i] = {x: 35*i+20, y: 20, r: 15, state: '0'} }
for (var i = 0; i < nodes.length; i++) { edges[i] = [] for (var j = 0; j < nodes.length; j++) { edges[i][j] = 0 } }
init();
canvas.onmousedown = myDown; canvas.onmouseup = myUp;
}
function draw() {
clear();
for (var i = 0; i < nodes.length; i++) { circ(nodes[i].x, nodes[i].y, nodes[i].r, nodes[i].state) }
for (var i = 0; i < nodes.length; i++) { for (var j = 0; j < nodes.length; j++) { if (edges[i][j] == 1) {
ctx.beginPath() ctx.lineWidth = 3 ctx.moveTo(nodes[i].x,nodes[i].y) ctx.lineTo(nodes[j].x,nodes[j].y) ctx.stroke() ctx.closePath()
} } }
}
function myDown(e) {
x = e.pageX - canvas.offsetLeft; y = e.pageY - canvas.offsetTop;
if (state.currState == 'unselected') {
canvas.onmousemove = myMove;
for (var i = 0; i < nodes.length; i++) { if (nodes[i].x - nodes[i].r < x && x < nodes[i].x + nodes[i].r && nodes[i].y - nodes[i].r < y && y < nodes[i].y + nodes[i].r) { nodes[i].state = "1" startX = nodes[i].x startY = nodes[i].y break } }
}
else if (state.currState == 'selected') {
for (var i = 0; i < nodes.length; i++) { if (nodes[i].x - nodes[i].r < x && x < nodes[i].x + nodes[i].r && nodes[i].y - nodes[i].r < y && y < nodes[i].y + nodes[i].r) {
if (i == state.currNode) { for (var j = 0; j < nodes.length; j++) { edges[j][state.currNode] = 0 edges[state.currNode][j] = 0 } }
else { edges[i][state.currNode] = 1 }
} }
nodes[state.currNode].state = "0" state.currState = "unselected"
}
}
function myUp() {
for (var i = 0; i < nodes.length; i++) { if (nodes[i].state == 1) { if (nodes[i].x == startX && nodes[i].y == startY) {
nodes[i].state = '2' state.currState = "selected" state.currNode = i
}
else { nodes[i].state = '0' }
} }
canvas.onmousemove = null;
}
function myMove(e) {
x = e.pageX - canvas.offsetLeft; y = e.pageY - canvas.offsetTop;
for (var i = 0; i < nodes.length; i++) { if (nodes[i].state == "1" && x < WIDTH && y < HEIGHT) {
nodes[i].x = x nodes[i].y = y
} }
}