percolation

This commit is contained in:
John Gatward
2026-03-17 15:48:13 +00:00
parent 6bef2094cb
commit c50e97987b
19 changed files with 10570 additions and 6 deletions

View File

@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Percolation</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="container">
<a href="/#projects" class="back-button">← Back</a>
<header>
<h1>Percolation</h1>
<p class="subtitle">A simulation written in C &amp; Raylib.</p>
</header>
<section>
<p>
<strong>Percolation</strong> is a simple model for connectivity on a grid. Each cell is either open or closed.
This simulation stops as soon as there is a path from the top of the grid to the bottom through the open cells.
</p>
<div class="info-box">
<p>
In this simulation, cells are randomly opened on a grid. Percolation occurs when there exists
a connected path of open cells from the top to the bottom of the grid. It is a neat way to visualise
threshold behaviour: below a certain probability nothing connects, and above it large connected regions
suddenly begin to appear.
</p>
</div>
</section>
<div class="canvas-container">
<div class="canvas-toolbar">
<p>Canvas</p>
<button id="fullscreenButton" class="action-button" type="button">Fullscreen</button>
</div>
<div class="canvas-shell">
<canvas id="canvas" aria-label="Percolation simulation canvas" oncontextmenu="event.preventDefault()"></canvas>
</div>
<div id="status" class="status">Downloading...</div>
<progress id="progress" value="0" max="100" hidden></progress>
<details>
<summary>Show console output</summary>
<label class="visually-hidden" for="output">Console output</label>
<textarea id="output" rows="8" readonly></textarea>
</details>
</div>
<section>
<h3>Technical Details</h3>
<p>
The simulation uses a <strong>disjoint-set (Union-Find)</strong> style connectivity algorithm. As random cells open,
each open cell is union-ed with its open neighbours, and two virtual nodes represent the top and bottom edges
of the grid. Percolation is detected the moment those two virtual nodes become connected.
</p>
<p>
This makes each update fast and scalable even for larger grids.
</p>
</section>
<footer>
<p>Built with C, Raylib, and WebAssembly</p>
</footer>
</div>
<script src="script.js"></script>
<script async src="index.js"></script>
</body>
</html>

10120
projects/percolation/index.js Normal file

File diff suppressed because it is too large Load Diff

BIN
projects/percolation/index.wasm Executable file

Binary file not shown.

View File

@@ -0,0 +1,78 @@
const statusElement = document.getElementById('status');
const progressElement = document.getElementById('progress');
const canvasElement = document.getElementById('canvas');
const outputElement = document.getElementById('output');
const fullscreenButton = document.getElementById('fullscreenButton');
outputElement.value = '';
canvasElement.addEventListener('webglcontextlost', (event) => {
event.preventDefault();
setStatus('WebGL context lost. Reload the page to restart the simulation.');
}, false);
function setStatus(text) {
if (!setStatus.last) {
setStatus.last = { time: Date.now(), text: '' };
}
if (text === setStatus.last.text) {
return;
}
const match = text && text.match(/([^(]+)\((\d+(?:\.\d+)?)\/(\d+)\)/);
const now = Date.now();
if (match && now - setStatus.last.time < 30) {
return;
}
setStatus.last.time = now;
setStatus.last.text = text;
if (match) {
statusElement.textContent = match[1].trim();
progressElement.value = Number.parseInt(match[2], 10) * 100;
progressElement.max = Number.parseInt(match[3], 10) * 100;
progressElement.hidden = false;
} else {
statusElement.textContent = text || '';
progressElement.hidden = !text;
if (!text) {
progressElement.removeAttribute('value');
}
}
}
var Module = {
canvas: canvasElement,
print: (...args) => {
console.log(...args);
outputElement.value += `${args.join(' ')}\n`;
outputElement.scrollTop = outputElement.scrollHeight;
},
printErr: (...args) => {
console.error(...args);
outputElement.value += `[err] ${args.join(' ')}\n`;
outputElement.scrollTop = outputElement.scrollHeight;
},
setStatus,
totalDependencies: 0,
monitorRunDependencies(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
setStatus(left ? `Preparing... (${this.totalDependencies - left}/${this.totalDependencies})` : 'Running...');
if (!left) {
setTimeout(() => setStatus(''), 250);
}
}
};
fullscreenButton.addEventListener('click', () => {
if (typeof Module.requestFullscreen === 'function') {
Module.requestFullscreen(false, false);
}
});
globalThis.onerror = () => {
setStatus('Exception thrown, see JavaScript console');
};

View File

@@ -0,0 +1,197 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: white;
color: black;
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.6;
padding: 20px;
}
.container {
max-width: 900px;
margin: 0 auto;
}
header {
text-align: center;
margin-bottom: 30px;
border-bottom: 1px solid #ccc;
padding-bottom: 20px;
}
h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
h2 {
font-size: 1.8rem;
margin: 20px 0 15px;
}
h3 {
font-size: 1.3rem;
margin: 15px 0;
}
p {
margin-bottom: 15px;
color: #333;
}
.subtitle {
color: #666;
}
.back-button {
display: inline-block;
margin-bottom: 20px;
padding: 10px 15px;
background: #f0f0f0;
color: black;
text-decoration: none;
border: 1px solid #ccc;
border-radius: 4px;
}
.back-button:hover,
.action-button:hover {
background: #e0e0e0;
}
.info-box {
background: #f9f9f9;
border-left: 4px solid #333;
padding: 15px;
margin: 20px 0;
}
.canvas-container {
background: #f9f9f9;
border: 1px solid #ddd;
border-radius: 4px;
padding: 20px;
margin: 30px 0;
}
.canvas-toolbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 15px;
flex-wrap: wrap;
}
.canvas-toolbar p {
margin: 0;
color: #666;
font-size: 0.95rem;
}
.action-button {
padding: 8px 12px;
background: #f0f0f0;
color: black;
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
font: inherit;
}
.canvas-shell {
display: flex;
justify-content: center;
align-items: center;
min-height: 500px;
overflow: auto;
background: white;
border: 1px solid #ddd;
}
#canvas {
display: block;
margin: 0 auto;
max-width: 100%;
height: auto;
background: black;
outline: none;
}
.status {
margin-top: 15px;
min-height: 24px;
color: #666;
}
progress {
width: 100%;
height: 16px;
margin-top: 10px;
}
progress[hidden] {
display: none;
}
details {
margin-top: 15px;
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
textarea {
width: 100%;
min-height: 120px;
margin-top: 10px;
padding: 10px;
border: 1px solid #ddd;
font-family: "Courier New", monospace;
font-size: 0.9rem;
resize: vertical;
background: white;
color: black;
}
footer {
text-align: center;
margin-top: 40px;
padding-top: 20px;
border-top: 1px solid #ccc;
font-size: 0.85rem;
color: #666;
}
@media (max-width: 768px) {
body {
padding: 16px;
}
h1 {
font-size: 2rem;
}
.canvas-container {
padding: 15px;
}
.canvas-shell {
min-height: 360px;
}
}