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

273 lines
9.1 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 2-1
</title>
<style>
.container
{
position: relative;
}
#caseOneButton
{
left: 12.5px;
top: 12.5px;
width: 100px;
height: 50px;
position: absolute;
}
#caseTwoButton
{
left: 12.5px;
top: 75px;
width: 100px;
height: 50px;
position: absolute;
}
#caseThreeButton
{
left: 12.5px;
top: 137.5px;
width: 100px;
height: 50px;
position: absolute;
}
#caseFourButton
{
left: 12.5px;
top: 200px;
width: 100px;
height: 50px;
position: absolute;
}
canvas
{
position: absolute;
}
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;"></canvas>
<div class="container">
<button type="button" id="caseOneButton" onclick="Case1()">&#969/&#969&#8320 = 0.5</button>
<button type="button" id="caseTwoButton" onclick="Case2()">&#969/&#969&#8320 = 0.75</button>
<button type="button" id="caseThreeButton" onclick="Case3()">&#969/&#969&#8320 = 1</button>
<button type="button" id="caseFourButton" onclick="Case4()">&#969/&#969&#8320 = 2</button>
</div>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
var cl = 11;
var clc = 12;
var dt = .025;
var o = 1.5;
var g = .5;
var dom = .1;
var bottom = 0;
var side = 350;
var dh = 150;
var dr = -50;
var h = 10;
var k = 90;
var m = 10;
var v = 0;
var dis = 0;
var phi = 0;
var i = 1;
var t = 0;
var xPos = -20;
var yPos = 00;
var amp = 0;
var pause = false;
var steadyStadeReps = 400;
var o0 = Math.sqrt(k/m);
CalculateInitialValues();
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
ctx.scale(3, 3);
function Update()
{
if (!pause)
{
dis += v * dt;
var stretch = (-h*Math.cos(o*t - phi) + dis)
v += ((-stretch * k * dt / m) + (-v * g * dt));
Draw();
t = t + dt;
ctx.strokeStyle = "#000000";
}
setTimeout(Update, 1000/120);
}
Update();
function Case1()
{
o = Math.sqrt(k/m) * 0.5;
CalculateInitialValues();
}
function Case2()
{
o = Math.sqrt(k/m) * 0.75;
CalculateInitialValues();
}
function Case3()
{
o = Math.sqrt(k/m);
CalculateInitialValues();
}
function Case4()
{
o = Math.sqrt(k/m) * 2;
CalculateInitialValues();
}
function Draw()
{
Clear(ctx);
var x1 = dr + h*Math.cos(o*t);
var a = (h*(o0^2 - o^2))/((o0^2 - o^2)^2 + g^2 * o^2)
var b = (h*(g*o))/((o0^2 - o^2)^2 + g^2 * o^2)
var x2 = dh + dr + dis
if (x2 > amp)
{
amp = x2;
}
ctx.beginPath();
ctx.moveTo(dr + xPos, -10 + yPos);
ctx.lineTo(dr + xPos, yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(dh + dr + xPos, -10 + yPos);
ctx.lineTo(dh + dr + xPos, yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(h + dr + xPos, -10 + yPos);
ctx.lineTo(h + dr + xPos, yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(dh + dr + (k*h/m)/(g*o0) + xPos, -10 + yPos);
ctx.lineTo(dh + dr + (k*h/m)/(g*o0) + xPos, 0 + yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-h + dr + xPos, -10 + yPos);
ctx.lineTo(-h + dr + xPos, yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(dh + dr - (k*h/m)/(g*o0) + xPos, -10 + yPos);
ctx.lineTo(dh + dr - (k*h/m)/(g*o0) + xPos, yPos);
ctx.stroke();
var dx = (x2 - x1) / 3;
/*ctx.beginPath();
ctx.moveTo(x1 + 10 + xPos, -30 + yPos);
ctx.lineTo(x1 + dx + xPos, -20 + yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x1 + dx + xPos, -20 + yPos);
ctx.lineTo(x1 + dx + dx + xPos, -40 + yPos);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x1 + dx + dx + xPos, -40 + yPos);
ctx.lineTo(x2 - 10 + xPos, -30 + yPos);
ctx.stroke();*/
drawSpring(1, 3, (x2 - 10) - (x1 + 10), 10, 10, x1 + 10 + xPos, -30 + yPos, 0.1);
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.arc(x1 + xPos, -30 + yPos, 10, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(x2 + xPos, -30 + yPos, 10, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(x1 + xPos, -30 + yPos, 10, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.arc(x2 + xPos, -30 + yPos, 10, 0, Math.PI * 2);
ctx.stroke();
}
function Reset()
{
pause = !pause;
}
function drawSpring(a, b, xSize, ySize, numLoops, x, y, tStep)
{
var tStart = Math.PI / 2;
var tEnd = (3 * Math.PI / 2) + (2 * Math.PI * numLoops);
var xEnd = a * tEnd - b * Math.sin(tEnd);
var yEnd = a + b;
var xFactor = xSize / xEnd;
var yFactor = ySize / yEnd;
var xShift = x - a * (Math.PI / 2) + b * Math.sin(Math.PI / 2);
var yShift = y - a + b * Math.cos(Math.PI / 2);
for (p = tStart; p <= tEnd; p += tStep)
{
ctx.beginPath();
ctx.moveTo(xShift + xFactor * (a * p - b * Math.sin(p)), yShift + yFactor * (a - b * Math.cos(p)));
ctx.lineTo(xShift + xFactor * (a * (p + tStep) - b * Math.sin(p + tStep)), yShift + yFactor * (a - b * Math.cos(p + tStep)));
ctx.stroke();
}
}
function CalculateInitialValues()
{
/*var a = ((k*h/m)*(o0^2 - o^2))/((o0^2 - o^2)^2 + g^2 * o^2);
var b = ((k*h/m)*(g*o))/((o0^2 - o^2)^2 + g^2 * o^2);
phi = Math.atan(b/a);
if (a < 0)
{
phi += Math.PI;
}
dis = 0;
v = o * ((k*h/m)/(g*o0));*/
for (j = 0; j < steadyStadeReps; j++)
{
dis += v * dt;
var stretch = (-h*Math.cos(o*t - phi) + dis)
v += ((-stretch * k * dt / m) + (-v * g * dt));
t = t + dt;
}
}
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);
</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>