Skip to content

Instantly share code, notes, and snippets.

@andrewgilmartin
Last active November 20, 2024 18:45
Show Gist options
  • Select an option

  • Save andrewgilmartin/f0b1a839ec28e74aa096edb188cbcd33 to your computer and use it in GitHub Desktop.

Select an option

Save andrewgilmartin/f0b1a839ec28e74aa096edb188cbcd33 to your computer and use it in GitHub Desktop.
Node Line
<!DOCTYPE html>
<html>
<body>
<H1>Node Line</H1>
<div id="c1"></div>
<script>
function createNodeLineElement(nodes, nodeWidth = 10, lineColor = 'gray', lineWidth = 4) {
const n = nodeWidth;
const n2 = nodeWidth / 2;
const n4 = nodeWidth / 4;
const canvasElement = document.createElement("canvas");
canvasElement.height = n + lineWidth + lineWidth;
canvasElement.width = n * (nodes.length * 2 - 1) + lineWidth + lineWidth;
const ctx = canvasElement.getContext("2d");
ctx.translate(lineWidth, lineWidth);
// draw route line
ctx.fillStyle = lineColor;
ctx.fillRect(n2, n4, n * (nodes.length * 2 - 2), n2 );
// draw route's nodes
nodes.forEach((node, index) => {
ctx.beginPath();
ctx.arc(n2 + n * index * 2, n2, n2, 0, 2 * Math.PI);
ctx.closePath();
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
ctx.stroke();
ctx.fillStyle = node.color;
ctx.fill();
});
return canvasElement;
}
const nodeLineElement = createNodeLineElement(
[
{color: '#a6e85f'},
{color: '#a6e85f'},
{color: '#a6e85f'},
{color: '#a6e85f'},
{color: '#a6e85f'},
{color: 'red'},
{color: 'white'}
],
10,
'gray',
4
);
const parentElement = document.getElementById("c1");
parentElement.appendChild(nodeLineElement);
</script>
</body>
</html>
@andrewgilmartin
Copy link
Author

A colleague built something better using only CSS! https://codepen.io/ericfreese/pen/mdNZwRx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment