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

122 lines
4.7 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 1-1
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
</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>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
c.width = window.innerWidth;
c.height = window.innerHeight;
//Time step
var dt = .05;
//Time
var m = 1;
//Parameter interval for spring
var tStep = 0.1;
//Relative position of canvas
var xPos = 0;
var yPos = 0;
//Moves and scales canvas
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
ctx.scale(3, 3);
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
ctx.strokeStyle = "#000000";
/*
var i;
for(i=-360; i<360; i+= 20/3){
ctx.moveTo(0,i + (5/3));
ctx.lineTo(0,i);
}
ctx.stroke();
*/
//Spinning line
ctx.beginPath();
ctx.moveTo(xPos, yPos);
ctx.lineTo(xPos + 25 * Math.cos(dt * m), yPos + -25 * Math.sin(dt * m));
ctx.stroke();
//One-dimensional SHM
ctx.strokeStyle = "#FF0000";
ctx.beginPath();
ctx.moveTo(xPos, yPos - 25);
ctx.lineTo(xPos + 25 * Math.cos(dt * m), yPos - 25);
ctx.stroke();
ctx.strokeStyle = "#0000FF";
//Mass on spring
drawSpring(1, 3, 25 * Math.cos(dt * m) + 48, 5, 7, xPos - 50, yPos + 25, tStep);
ctx.beginPath();
ctx.arc(xPos + 25 * Math.cos(dt * m), yPos + 25, 2, 0, 2 * Math.PI);
ctx.fill();
m += 1;
console.log(m);
setTimeout(Update, 1000/60)
}
function drawSpring(a, b, xSize, ySize, numLoops, x, y, tStep)
{
//Draws coiled spring using prolate cycloid
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 (t = tStart; t <= tEnd; t += tStep)
{
ctx.beginPath();
ctx.moveTo(xShift + xFactor * (a * t - b * Math.sin(t)), yShift + yFactor * (a - b * Math.cos(t)));
ctx.lineTo(xShift + xFactor * (a * (t + tStep) - b * Math.sin(t + tStep)), yShift + yFactor * (a - b * Math.cos(t + tStep)));
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>