|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +///// Globals ///// |
| 4 | +let ManhattanDistance = false; |
| 5 | +let generatingPoints = []; |
| 6 | +let voronoiCanvas = document.querySelector('#canvas'); |
| 7 | +let voronoiContext = voronoiCanvas && voronoiCanvas.getContext && voronoiCanvas.getContext('2d'); |
| 8 | + |
| 9 | +///// Main Functions ///// |
| 10 | +function getPointFromEvent(ev) { |
| 11 | + let rect = voronoiCanvas.getBoundingClientRect(); |
| 12 | + return { |
| 13 | + color: 'rgb(' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ')', |
| 14 | + x: ev.clientX - rect.left, |
| 15 | + y: ev.clientY - rect.top |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function updateVoronoiDiagram() { |
| 20 | + voronoiCanvas.width = voronoiCanvas.clientWidth; |
| 21 | + voronoiCanvas.height = voronoiCanvas.clientHeight; |
| 22 | + |
| 23 | + // If there are no points, no reason to redraw |
| 24 | + if (generatingPoints.length === 0) { |
| 25 | + voronoiContext.fillStyle = 'rgb(0, 0, 0)'; |
| 26 | + voronoiContext.fillRect(0, 0, voronoiCanvas.width, voronoiCanvas.height); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // Pick a color for each pixel based on the closest point |
| 31 | + for (let x = 0; x < voronoiCanvas.width; x += 1) { |
| 32 | + for (let y = 0; y < voronoiCanvas.height; y += 1) { |
| 33 | + let currentDistance = 0; |
| 34 | + let closestDistance = Infinity; |
| 35 | + |
| 36 | + generatingPoints.forEach(function (currentPoint) { |
| 37 | + if (ManhattanDistance) { |
| 38 | + currentDistance = (Math.abs(x - currentPoint.x) + Math.abs(y - currentPoint.y)); |
| 39 | + } else { |
| 40 | + currentDistance = Math.sqrt(Math.pow(x - currentPoint.x, 2) + Math.pow(y - currentPoint.y, 2)); |
| 41 | + } |
| 42 | + |
| 43 | + if (currentDistance < closestDistance) { |
| 44 | + closestDistance = currentDistance; |
| 45 | + voronoiContext.fillStyle = currentPoint.color; |
| 46 | + } |
| 47 | + }); |
| 48 | + voronoiContext.fillRect(x, y, 1, 1); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Draw the circle around the point |
| 53 | + generatingPoints.forEach(function (currentPoint) { |
| 54 | + // Black circle |
| 55 | + voronoiContext.beginPath(); |
| 56 | + voronoiContext.arc(currentPoint.x, currentPoint.y, 7, 0, 2 * Math.PI, false); |
| 57 | + voronoiContext.fillStyle = 'black'; |
| 58 | + voronoiContext.fill(); |
| 59 | + |
| 60 | + // White circle |
| 61 | + voronoiContext.beginPath(); |
| 62 | + voronoiContext.arc(currentPoint.x, currentPoint.y, 5, 0, 2 * Math.PI, false); |
| 63 | + voronoiContext.fillStyle = 'white'; |
| 64 | + voronoiContext.fill(); |
| 65 | + |
| 66 | + // Colored circle |
| 67 | + voronoiContext.beginPath(); |
| 68 | + voronoiContext.arc(currentPoint.x, currentPoint.y, 3, 0, 2 * Math.PI, false); |
| 69 | + voronoiContext.fillStyle = currentPoint.color; |
| 70 | + voronoiContext.fill(); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +function addPoint(ev) { |
| 75 | + generatingPoints.push(getPointFromEvent(ev)); |
| 76 | + updateVoronoiDiagram(); |
| 77 | +} |
| 78 | + |
| 79 | +function swapDistanceCalculation() { |
| 80 | + ManhattanDistance = !ManhattanDistance; |
| 81 | + if (ManhattanDistance) { |
| 82 | + document.body.style.backgroundColor = 'rgb(66, 7, 122)'; |
| 83 | + } else { |
| 84 | + document.body.style.backgroundColor = 'rgb(22, 74, 119)'; |
| 85 | + } |
| 86 | + |
| 87 | + updateVoronoiDiagram(); |
| 88 | +} |
| 89 | + |
| 90 | +///// Event listeners ///// |
| 91 | +voronoiCanvas.addEventListener('click', (ev) => addPoint(ev), false); |
| 92 | +document.querySelector('#title').addEventListener('click', swapDistanceCalculation, false); |
| 93 | +window.addEventListener('resize', updateVoronoiDiagram, false); |
| 94 | + |
| 95 | +///// Run! ///// |
| 96 | +// Finally draw for the first time |
| 97 | +updateVoronoiDiagram(); |
0 commit comments