artworld/examples/gallery.html
2025-03-12 23:51:24 -05:00

114 lines
2.7 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JS Module Loader</title>
<style>
body {
display: flex;
font-family: Arial, sans-serif;
height: 100vh;
margin: 0;
}
#sidebar {
width: 200px;
background: #f4f4f4;
padding: 10px;
overflow-y: auto;
}
#file-list button {
display: block;
width: 100%;
margin: 5px 0;
padding: 10px;
text-align: left;
border: none;
background: #ddd;
cursor: pointer;
}
#file-list button:hover {
background: #bbb;
}
#main {
flex-grow: 1;
display: flex;
flex-direction: column;
padding: 10px;
}
#mainCanvas {
width: 500px;
height: 500px;
background: #eee;
flex: 1;
}
#output {
white-space: pre-wrap;
background: #222;
color: #ffb300;
padding: 10px;
overflow: auto;
font-family: monospace;
flex: 1;
max-height: 50%;
}
</style>
</head>
<body>
<div id="sidebar">
<h3>Files</h3>
<div id="file-list"></div>
</div>
<div id="main">
<canvas id="mainCanvas"></canvas>
<div id="output">Script output will appear here...</div>
</div>
<script>
const jsFiles = [
"balls.js",
"circles.js",
"copies.js",
"dataclock.js",
"lines.js",
"liskov.js",
"polygons.js",
"rect.js",
"spiral.js",
];
async function loadAndRunModule(fileName) {
const canvas = document.getElementById("mainCanvas");
const outputDiv = document.getElementById("output");
// Clear the canvas
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
outputDiv.textContent = `Loading ${fileName}...\n`;
try {
// import the module dynamically with cache busting
const module = await import(`./${fileName}?t=${Date.now()}`);
// TODO: get source code
} catch (err) {
outputDiv.textContent += `Error loading ${fileName}:\n${err}`;
}
}
function renderFileList() {
const fileListDiv = document.getElementById("file-list");
jsFiles.forEach((file) => {
const btn = document.createElement("button");
btn.textContent = file;
btn.onclick = () => loadAndRunModule(file);
fileListDiv.appendChild(btn);
});
}
renderFileList();
</script>
</body>
</html>