-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
139 lines (118 loc) · 3.77 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html>
<head>
<title>Voronoi</title>
<style>
#drawing-area {
border: 1px solid black;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="drawing-area"></canvas>
<script>
document.addEventListener('DOMContentLoaded', () => {
class Canvas {
constructor(canvas, width, height) {
this._canvas = canvas;
this._context = canvas.getContext('2d');
this.resize(width, height);
}
resize(width, height) {
this._canvas.width = width;
this._canvas.height = height;
this.width = width;
this.height = height;
}
clear() {
this._context.clearRect(0, 0, this.width, this.height);
}
line(x0, y0, x1, y1) {
this._context.moveTo(x0, y0);
this._context.lineTo(x1, y1);
this._context.stroke();
}
circle(x, y, radius) {
this._context.beginPath();
this._context.arc(x, y, radius, 0, 2 * Math.PI);
this._context.fillStyle = 'black';
this._context.fill();
this._context.stroke();
}
point(x, y, color) {
this._context.fillStyle = color;
this._context.fillRect(x, y, 1, 1);
}
}
class Seed {
constructor(x, y, dx, dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
}
const rand = (min, max) => Math.random() * (max - min + 1) + min;
const randSeed = (width, height) => (
new Seed(
rand(0, width),
rand(0, height),
(Math.random() - 0.5) * 5,
(Math.random() - 0.5) * 5,
)
);
const distance = (x0, y0, x1, y1) => (Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)));
const updateSeeds = (seeds) => {
for (let seed of seeds) {
seed.x += seed.dx;
seed.y += seed.dy;
if (seed.x <= 0 || seed.x >= canvas.width) {
seed.dx *= -1;
}
if (seed.y <= 0 || seed.y >= canvas.height) {
seed.dy *= -1;
}
}
}
const drawVoronoi = (canvas, seeds) => {
for (let x = 0; x < canvas.width; x++) {
for (let y = 0; y < canvas.height; y++) {
let imin = 0;
let minDist = distance(seeds[imin].x, seeds[imin].y, x, y);
for (let i = 1; i < seeds.length; i++) {
const currDist = distance(seeds[i].x, seeds[i].y, x, y);
if (currDist < minDist) {
imin = i;
minDist = currDist;
}
}
canvas.point(x, y, colors[imin]);
}
}
}
const drawSeeds = (canvas, seeds) => {
for (let seed of seeds) {
canvas.circle(seed.x, seed.y, 3);
}
}
// ---------------------------------------------------------------------
const canvas = new Canvas(document.querySelector('#drawing-area'), 400, 400);
const seeds = [...Array(10)].map(() => randSeed(canvas.width, canvas.height));
const colors = [
'#cda434', '#025669', '#c51d34', '#5d9b9b', '#316650',
'#3d642d', '#f75e25', '#e7ebda', '#763c28', '#6d3f5b',
];
if (colors.length < seeds.length) {
throw new Error('colors.length < seeds.length');
}
setInterval(() => {
updateSeeds(seeds);
drawVoronoi(canvas, seeds);
drawSeeds(canvas, seeds);
}, 100);
});
</script>
</body>
</html>