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

212 lines
7.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 4-2
</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;
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">Mode 1</button>
<div class="dropdown-content">
<button onclick="Cases(1)">Mode 1</button>
<button onclick="Cases(2)">Mode 2</button>
<button onclick="Cases(3)">Mode 3</button>
<button onclick="Cases(4)">Mode 4</button>
<button onclick="Cases(5)">Mode 5</button>
<button onclick="Cases(6)">Mode 6</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 xPos = 0;
var yPos = 0;
var h = [1/2, 1, 1/2, -1/2, -1, -1/2];
var d = [0, 0, 0, 0, 0, 0];
var amp = Math.PI / 24;
var l = 20;
var a = 30;
var omega = .5;
var t = 0;
var dt = .03;
var j = 1;
var em = 20;
var bm = 9;
var cm = -1;
var dm = 1;
var o = Math.sqrt(em - 2 * bm * Math.cos(j * Math.PI/3) - 2 * cm * Math.cos(2 * j * Math.PI/3) - dm * Math.cos(j * Math.PI));
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 Cases(num)
{
btn.textContent = "Mode " + num;
if (num == 1)
{
Case1();
} else if (num == 2)
{
Case2();
} else if (num == 3)
{
Case3();
} else if (num == 4)
{
Case4();
} else if (num == 5)
{
Case5();
} else if (num == 6)
{
Case6();
}
}
function Case1()
{
h = [1, 1/2, -1/2, -1, -1/2, 1/2];
t = 0;
j = 1;
}
function Case2()
{
h = [-1, 1/2, 1/2, -1, 1/2, 1/2];
t = 0;
j = 3;
}
function Case3()
{
h = [1, -1, 1, -1, 1, -1];
t = 0;
j = 3;
}
function Case4()
{
h = [0, 1, 1, 0, -1, -1];
t = 0;
j = 1;
}
function Case5()
{
h = [0, -1, 1, 0, -1, 1];
t = 0;
j = 3;
}
function Case6()
{
h = [1, 1, 1, 1, 1, 1];
t = 0;
j = 0;
}
function Update()
{
o = Math.sqrt(em - 2 * bm * Math.cos(j * Math.PI/3) - 2 * cm * Math.cos(2 * j * Math.PI/3) - dm * Math.cos(j * Math.PI));
Clear(ctx);
theta = 0;
for (i = 0; i < 6; i++)
{
d[i] = h[i] * Math.sin(omega * o * t) * amp;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(l * Math.cos(theta), l * Math.sin(theta));
ctx.lineTo(l * Math.cos(theta) + l * Math.cos(theta + (d[i]/2)), l * Math.sin(theta) + l * Math.sin(theta + (d[i]/2)));
ctx.lineTo(l * Math.cos(theta) + l * Math.cos(theta + (d[i]/2)) + l * Math.cos(theta + d[i]), l * Math.sin(theta) + l * Math.sin(theta + (d[i]/2)) + l * Math.sin(theta + d[i]));
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.arc(l * Math.cos(theta) + l * Math.cos(theta + (d[i]/2)) + l * Math.cos(theta + d[i]), l * Math.sin(theta) + l * Math.sin(theta + (d[i]/2)) + l * Math.sin(theta + d[i]), 5, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(l * Math.cos(theta) + l * Math.cos(theta + (d[i]/2)) + l * Math.cos(theta + d[i]), l * Math.sin(theta) + l * Math.sin(theta + (d[i]/2)) + l * Math.sin(theta + d[i]), 5, 0, Math.PI * 2);
ctx.stroke();
theta += Math.PI/3;
}
t += dt;
setTimeout(Update, 1000/60)
}
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>