Files
WavesPrograms/chladni.html
2021-08-18 19:24:55 -07:00

130 lines
4.4 KiB
HTML

<html>
<head>
<title>
Waves: Chladni plates
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
form {
position: relative;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;">
Your browser does not support the HTML5 canvas tag.</canvas>
<form>
<label for="x">X mode</label>
<input type="number" id="x" min="0" max="10" step="1" value="1">
<br>
<label for="y">Y mode</label>
<input type="number" id="y" min="0" max="10" step="1" value="1">
</form>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
var xMode = document.getElementById("x").value;
var yMode = document.getElementById("y").value;
var step = 0.01;
var sandThreshold = 0.01;
var plateSize = 53;
var xPos = 0;
var yPos = 0;
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
ctx.scale(4, -4);
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
setTimeout(Update, 1000/60);
if (xMode != document.getElementById("x").value)
{
xMode = parseFloat(document.getElementById("x").value);
sandThreshold = 0.065 + 0.055 * (1.02 ** (xMode + yMode));
Clear(ctx);
Draw();
}
if (yMode != document.getElementById("y").value)
{
yMode = parseFloat(document.getElementById("y").value);
sandThreshold = 0.065 + 0.055 * (1.02 ** (xMode + yMode));
Clear(ctx);
Draw();
}
}
Update();
function Draw()
{
ctx.beginPath();
ctx.moveTo(xPos - plateSize, yPos - plateSize);
ctx.lineTo(xPos + plateSize, yPos - plateSize);
ctx.lineTo(xPos + plateSize, yPos + plateSize);
ctx.lineTo(xPos - plateSize, yPos + plateSize);
ctx.lineTo(xPos - plateSize, yPos - plateSize);
ctx.stroke();
for (x = -1; x <= 0; x += step)
{
for (y = -1; y <= 0; y += step)
{
if (Math.random() >= 0)
{
phi = (Math.cos(xMode * x * Math.PI) * Math.cos(yMode * y * Math.PI)) + (Math.cos(xMode * y * Math.PI) * Math.cos(yMode * x * Math.PI));
console.log(phi);
if (phi <= sandThreshold && phi >= -sandThreshold)
{
ctx.beginPath();
ctx.arc(x * plateSize + xPos, y * plateSize + yPos, step * plateSize, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(-x * plateSize + xPos, y * plateSize + yPos, step * plateSize, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(x * plateSize + xPos, -y * plateSize + yPos, step * plateSize, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(-x * plateSize + xPos, -y * plateSize + yPos, step * plateSize, 0, Math.PI * 2);
ctx.fill();
console.log("point");
}
}
}
}
}
Draw();
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
ctx.scale(4, -4);
Clear(ctx);
Draw();
}, true);
</script>
</body>
</html>