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

215 lines
7.4 KiB
HTML

<!--
This work is licensed under CC BY-NC-ND 4.0
Link to license: http://creativecommons.org/licenses/by-nc-nd/4.0/
Attribute to Russell Georgi
-->
<html>
<head>
<title>
Waves 5-3
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: absolute;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
background-color: #f1f1f1;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content button {
color: black;
padding: 12px 16px;
width: 100px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
</style>
</head>
<body>
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;"></canvas>
<div class="dropdown">
<button class="dropbtn" id="btn">1 Mass</button>
<div class="dropdown-content">
<button onclick="Blocks(2)">1 Mass</button>
<button onclick="Blocks(3)">2 Masses</button>
<button onclick="Blocks(4)">3 Masses</button>
<button onclick="Blocks(5)">4 Masses</button>
<button onclick="Blocks(6)">5 Masses</button>
<button onclick="Blocks(7)">6 Masses</button>
</div>
</div>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var btn = document.getElementById("btn");
c.width = window.innerWidth;
c.height = window.innerHeight;
var nnn = 1;
var nn = nnn + 1;
var mm = new Array(nn);
var a = new Array (nn);
var mmax = 350;
nmax = nn;
var yy = new Array(nn + 1);
var xPos = -50;
var yPos = -90;
var m = 1;
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
ctx.scale(3, -3);
for (k = 1; k <= nn - 1; k++)
{
mm[k] = 80 / Math.sqrt(2 - 2 * Math.cos((k - 1/2) * Math.PI / (2*nn-1)));
a[k] = 3 / nn;
}
var y = new Array(nn);
for (i = 0; i <= nn - 1; i++)
{
y[i] = new Array(nn + 1);
for (j = 0; j <= nn; j++)
{
y[i][j] = new Array(Math.round(mm[1] + 1));
for (k = 0; k <= mm[1]; k++)
{
y[i][j][k] = a[i]*8*Math.sin(i* Math.PI * j / (2*nn-1)) * Math.sin(2 * Math.PI * (k - 1) / mm[i]);
}
}
}
function Blocks(num)
{
nn = num;
console.log(num);
btn.textContent = (num - 1) + " Masses";
nmax = nn;
for (k = 1; k <= nn - 1; k++)
{
mm[k] = 80 / Math.sqrt(3 - 2 * Math.cos(k * Math.PI / nn));
a[k] = 2 / nn;
}
y = new Array(nn);
for (i = 0; i <= nn - 1; i++)
{
y[i] = new Array(nn);
for (j = 0; j <= nn; j++)
{
y[i][j] = new Array(Math.round(mm[1]));
for (k = 0; k <= mm[1]; k++)
{
y[i][j][k] = a[i]*8*Math.sin((2*i-1)* Math.PI * (j) / (2*nn-1)) * Math.sin(2 * Math.PI * (k - 1) / mm[i]);
}
}
}
}
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
Draw();
m += 1;
setTimeout(Update, 1000/60)
}
function Draw()
{
for (n = 0; n < nn + 1; n++)
{
yy[n] = 160;
for (k = 1; k < nn; k++)
{
yy[n] += y[k][n][m % Math.round(mm[k])];
}
}
for (n = 0; n < nmax; n++)
{
ctx.beginPath();
ctx.moveTo(10 + 80 * n / nmax + xPos, yy[n] + yPos);
ctx.lineTo(10 + 80*(n+1)/nmax + xPos, yy[n+1] + yPos);
ctx.stroke();
for (k = 1; k < nn; k++)
{
ctx.beginPath();
ctx.moveTo(10 + 80 * n/nmax + xPos, yPos + k*160/nn + y[k][n][m % Math.round(mm[k])]);
ctx.lineTo(10 + 80*(n+1)/nmax + xPos,yPos+ k*160/nn+ y[k][n+1][m % Math.round(mm[k])]);
ctx.stroke();
}
}
for (n = 1; n < nmax; n++)
{
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.arc(10 + 80 * n/nmax + xPos, yPos + yy[n], 2, 0, Math.PI * 2);
ctx.fill();
for (k = 1; k < nn; k++)
{
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.arc(10 + 80 * n/nmax + xPos, yPos + k * 160/nn + y[k][n][m % Math.round(mm[k])], 2, 0, Math.PI * 2);
ctx.fill();
}
}
ctx.beginPath();
ctx.moveTo(90 + xPos, c.height);
ctx.lineTo(90 + xPos, -c.height);
ctx.stroke();
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
ctx.scale(3, -3);
}, true);
Update();
</script>
</body>
<p xmlns:cc="http://creativecommons.org/ns#" style="font-size: 1vw; bottom: 0px; position: absolute;">
This work is licensed under
<a href="http://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC BY-NC-ND 4.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nd.svg?ref=chooser-v1"></a></p>
</html>