-
Notifications
You must be signed in to change notification settings - Fork 2
/
mjp-demo.html
171 lines (155 loc) · 5.69 KB
/
mjp-demo.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Javascript - Poisson Disc Sampling Algorithm</title>
<meta name="author" content="Matthew Page">
<meta name="description" content="Using blue noise to evenly distribute random points with Poisson Disc Sampling. Coded in Javascript">
<meta name="keywords" content="piosson, poison, pioson, poisson disk, poisson disk sampling, algorithm, javascript, es6, noise, random, blue noise">
<link href="css/mjp-tailwind.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Exo+2" rel="stylesheet">
</head>
<body class="overflow-hidden text-zinc-200">
<div class="grid grid-cols-2 gap-8">
<section class="random">
<h2 class="text-sm">❌ Points distributed by random selection only</h2>
<canvas id="random" class="w-full border border-white mt-1"></canvas>
</section>
<section class="poisson">
<h2 class="text-sm">✔️ Points distributed by Poisson Disc Sampling</h2>
<canvas id="poisson" class="w-full border border-white mt-1"></canvas>
</section>
</div>
<section class="slowmo">
<h2 class="text-sm mt-2">🐌 Slow Motion Poisson Disc Sampling</h2>
<canvas id="poissonSlow" class="mt-1"></canvas>
</section>
<p class="text-sm mt-2">🛠️ Draw random points with a minimum separation of (<em>r</em>)
<button onclick="poissonReset(5)">5</button>
<button onclick="poissonReset(10)">10</button>
<button onclick="poissonReset(15)">15</button>
<button onclick="poissonReset(25)">25</button>
</p>
<script src="js/poisson-disc.js" type="text/javascript"></script>
<script type="text/javascript">
const animationDelay = 100;
const width = parseInt(window.innerWidth);
const widthHalf = parseInt((window.innerWidth - 30) / 2);
const height = parseInt((window.innerHeight - 100) / 2);
const randomCanvas = document.getElementById("random");
const randomCTX = randomCanvas.getContext("2d");
const poissonCanvas = document.getElementById("poisson");
const poissonCTX = poissonCanvas.getContext("2d");
const poissonSlowCanvas = document.getElementById("poissonSlow");
const poissonSlowCTX = poissonSlowCanvas.getContext("2d");
randomCanvas.width = widthHalf;
randomCanvas.height = height;
poissonCanvas.width = widthHalf;
poissonCanvas.height = height;
poissonSlowCanvas.width = width;
poissonSlowCanvas.height = height;
let poissonR = 10;
let frame = 0;
let myPoisson = false;
let myPoissonSlow = new PoissonDisc(width, height, poissonR, 30, 2);
/**
* Main animation loop
*/
function animationLoop() {
if(frame % 25 == 1) {
myPoisson = new PoissonDisc(width, height, poissonR, 30, 2);
myPoisson.run();
drawPoisson();
drawRandom(myPoisson.points.length);
}
myPoissonSlow.step(5);
if(myPoissonSlow.active.length < 1) {
myPoissonSlow = new PoissonDisc(width, height, poissonR, 30, 2);
}
drawPoissonSlow();
frame+=1;
setTimeout(animationLoop, animationDelay);
}
/**
* Reset the examples
*/
function poissonReset(r) {
poissonR = parseInt(r);
myPoissonSlow = new PoissonDisc(width, height, poissonR, 30, 2);
myPoisson = new PoissonDisc(width, height, poissonR, 30, 2);
}
/**
* Draw the Slow Motion Poisson grid
*/
function drawPoissonSlow() {
poissonSlowCTX.clearRect(0, 0, width, height);
poissonSlowCTX.lineWidth = 1;
poissonSlowCTX.strokeStyle = "black";
poissonSlowCTX.beginPath();
for(let y = 0; y < myPoissonSlow.height; y+=myPoissonSlow.cellSize) {
poissonSlowCTX.moveTo(0, y);
poissonSlowCTX.lineTo(myPoissonSlow.width, y);
}
for(let x = 0; x < myPoissonSlow.width; x+=myPoissonSlow.cellSize) {
poissonSlowCTX.moveTo(x, 0);
poissonSlowCTX.lineTo(x, myPoissonSlow.height);
}
poissonSlowCTX.stroke();
poissonSlowCTX.fillStyle = "#fbbf24";
for(let i = 0; i < myPoissonSlow.points.length; i+=1) {
if(myPoissonSlow.points[i]) {
poissonSlowCTX.fillRect(myPoissonSlow.points[i].px-2, myPoissonSlow.points[i].py-2, 4, 4);
}
}
poissonSlowCTX.fillStyle = "#c084fc";
for(let i = 0; i < myPoissonSlow.active.length; i+=1) {
if(myPoissonSlow.active[i]) {
poissonSlowCTX.beginPath();
poissonSlowCTX.arc(myPoissonSlow.points[myPoissonSlow.active[i]].px, myPoissonSlow.points[myPoissonSlow.active[i]].py,
4, 0, 2 * Math.PI);
poissonSlowCTX.fill();
}
}
}
/**
* Draw the Poisson grid
*/
function drawPoisson() {
poissonCTX.clearRect(0, 0, width, height);
poissonCTX.fillStyle = "#fbbf24";
poissonCTX.strokeStyle = "black";
poissonCTX.lineWidth = 1;
for(let i = 0; i < myPoisson.points.length; i+=1) {
if(myPoisson.points[i]) {
poissonCTX.beginPath();
poissonCTX.arc(myPoisson.points[i].px, myPoisson.points[i].py, 3, 0, 2 * Math.PI);
poissonCTX.fill();
poissonCTX.stroke();
}
}
}
/**
* Draw the random grid with x number of points
*/
function drawRandom(x) {
randomCTX.clearRect(0, 0, width, height);
randomCTX.fillStyle = "#fbbf24";
randomCTX.strokeStyle = "black";
randomCTX.lineWidth = 1;
for(let n = 0; n < x; n++) {
let x = Math.floor(Math.random()*width);
let y = Math.floor(Math.random()*height);
randomCTX.beginPath();
randomCTX.arc(x, y, 3, 0, 2 * Math.PI);
randomCTX.fill();
randomCTX.stroke();
}
}
/**
* Start the animation
*/
animationLoop();
</script>
</body>
</html>