Last active
November 20, 2024 18:45
-
-
Save andrewgilmartin/f0b1a839ec28e74aa096edb188cbcd33 to your computer and use it in GitHub Desktop.
Node Line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A colleague built something better using only CSS! https://codepen.io/ericfreese/pen/mdNZwRx