Skip to content

Commit 71d7b31

Browse files
author
Glitch (glitch-hello-website)
committed
πŸ’½πŸ‘™ Checkpoint
./script.js:411873/4548 ./style.css:411873/19 ./voronoi.js:411873/71 ./index.html:411873/14
1 parent c3ceea1 commit 71d7b31

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

β€Žindex.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<link rel="stylesheet" href="/style.css" />
2222

2323
<!-- The website JavaScript file -->
24-
<script src="/script.js" defer></script>
24+
<script type="module" src="/script.js" defer></script>
2525
</head>
2626
<body>
2727
<div id="fullpage">

β€Žscript.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,63 @@
1-
import { Voronoi } from "./voronoi.min.js";
1+
import { Voronoi } from "./voronoi.js";
2+
3+
const randomInt = () => {
4+
const ranges = [
5+
{ min: 500, max: 700 },
6+
{ min: 1000, max: 5000 },
7+
{ min: 5000, max: 7000 },
8+
];
9+
const { min, max } = ranges[Math.floor(Math.random() * ranges.length)];
10+
return Math.floor(Math.random() * (max - min + 1)) + min;
11+
};
12+
const numPoints = randomInt();
13+
14+
const points = [];
15+
16+
function drawVoronoi(canvasId,nPoints) {
17+
const canvas = document.getElementById(canvasId);
18+
const ctx = canvas.getContext("2d");
19+
const canvasWidth = canvas.clientWidth;
20+
const canvasHeight = canvas.clientHeight;
21+
22+
// Set canvas size
23+
canvas.width = canvasWidth;
24+
canvas.height = canvasHeight;
25+
26+
for (let i = 0; i < nPoints; i++) {
27+
points.push({
28+
x: Math.random() * canvasWidth,
29+
y: Math.random() * canvasHeight,
30+
});
31+
}
32+
33+
const voronoi = new Voronoi();
34+
const bbox = { xl: 0, xr: canvasWidth, yt: 0, yb: canvasHeight };
35+
const diagram = voronoi.compute(points, bbox);
36+
37+
// Draw Voronoi cells
38+
diagram.cells.forEach((cell) => {
39+
ctx.beginPath();
40+
ctx.moveTo(
41+
cell.halfedges[0].getStartpoint().x,
42+
cell.halfedges[0].getStartpoint().y
43+
);
44+
45+
cell.halfedges.forEach((edge) => {
46+
const startPoint = edge.getStartpoint();
47+
ctx.lineTo(startPoint.x, startPoint.y);
48+
});
49+
50+
ctx.closePath();
51+
52+
const r = ([cell.site.voronoiId] * 3) % 255;
53+
const g = [cell.site.voronoiId * 5 + 1] % 255;
54+
const b = ((r * g) / (r + g)) % 255;
55+
ctx.strokeStyle = `rgba(${r}, ${g}, ${b}, 1)`;
56+
ctx.stroke();
57+
});
58+
}
59+
60+
window.addEventListener("resize", drawVoronoi("background",numPoints));
61+
62+
// Initialize on page load
63+
window.addEventListener("load", drawVoronoi("background",numPoints));

β€Žstyle.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ canvas#background{
2323
height:100%;
2424
width:100%;
2525
border:1px solid black;
26-
background: red;
2726
grid-area: 1 / 1 / 4 / 4;
2827
z-index: -1;
2928

β€Žvoronoi.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,3 @@ Voronoi.prototype.compute = function(sites, bbox) {
17181718
};
17191719

17201720
/******************************************************************************/
1721-
1722-
if ( typeof module !== 'undefined' ) {
1723-
module.exports = Voronoi;
1724-
}

0 commit comments

Comments
Β (0)