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

271 lines
8.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: Lens
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
form {
position: relative;
}
.slideContainer {
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="toggle">Position indicators</label>
<input type="checkbox" id="toggle" onclick="toggleClick()">
</form>
<div class="slidecontainer">
<t>X position</t>
<input type="range" min="-350" max="350" value="0" step = "1" id="xSlider">
<br>
<t>Y position</t>
<input type="range" min="-150" max="150" value="0" step = "1" id="ySlider">
<br>
<t>Focal length</t>
<input type="range" min="-50" max="100" value="50" step = "1" id="fSlider">
</div>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
var f = 50;
var cll = "#000000";
var clb = "#0000ff";
var nxb = 3;
var nyb = 3;
var dt = .03;
var a = new Array(6);
for (i = 0; i < 6; i++)
{
a[i] = new Array(6);
}
var xAdj = 0;
var x0 = xAdj - c.width / (2 * 1.5);
var y0 = 0;
var x1 = 0;
var y1 = 30;
var xs = c.width / (2 * 1.5);
var dx = 5;
var dy = 5;
var t = 0;
var m = 1;
var toggle = false;
var xPos = 0;
var yPos = 0;
var xSlider = document.getElementById("xSlider");
var ySlider = document.getElementById("ySlider");
var fSlider = document.getElementById("fSlider");
xSlider.max = c.width / (2 * 1.5) - 30;
if (xAdj > xSlider.max)
{
xAdj = xSlider.max;
}
x0 = xAdj - c.width / (2 * 1.5) - 5;
xs = c.width / (2 * 1.5);
xSlider.oninput = function()
{
xAdj = parseFloat(this.value);
x0 = xAdj - c.width / (2 * 1.5);
}
ySlider.oninput = function()
{
y0 = parseFloat(this.value);
}
fSlider.oninput = function()
{
f = parseFloat(this.value);
}
function fnys(pxs, pf, px0, py0, px1, py1)
{
return ((py1 - py0) / (px1 - px0) - py1 / pf) * (pxs - px1) + py1;
}
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
ctx.scale(1.5, -1.5);
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()
{
ys = fnys(xs, f, x0, y0, x1, y1);
ctx.strokeStyle = cll;
ctx.beginPath();
ctx.moveTo(xPos + x1, yPos + y1);
ctx.lineTo(xPos + x1, yPos - y1);
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos + x0, yPos + y0, 4, 0, Math.PI * 2);
ctx.stroke();
v = fnys(xs, f, x0, y0, x1, -y1) - fnys(xs, f, x0, y0, x1, y1) + 2 * y1;
if (f != 0)
{
console.log("f != 0");
xv = 2 * y1 * (xs - x1) / v;
yv = y1 + xv * (fnys(xs, f, x0, y0, x1, y1) - y1) / (xs - x1);
ctx.strokeStyle = clb;
ctx.beginPath();
ctx.arc(xPos + xv, yPos + yv, 4, 0, Math.PI * 2);
ctx.stroke();
}
ray(xs, f, x0, y0, x1, y1, clb, xv, yv);
ray(xs, f, x0, y0, x1, -y1, clb, xv, yv);
ray(xs, f, x0, y0, x1, 0, clb, xv, yv);
/*ray(xs, f, x0, 0, x1, y1, "#00ffff");
ray(xs, f, x0, 0, x1, -y1, "#00ffff");
ray(xs, f, x0, 0, x1, 0, "#00ffff");*/
if (toggle)
{
for (j = -150; j <= 140; j += 10)
{
ctx.strokeStyle = "#000000";
ctx.beginPath();
ctx.moveTo(xPos + xv, yPos + j);
ctx.lineTo(xPos + xv, yPos + j + 5);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(xPos + x0, yPos + j);
ctx.lineTo(xPos + x0, yPos + j + 5);
ctx.stroke();
}
}
}
function ray(pxs, pf, px0, py0, px1, py1, pclb, pxv, pyv)
{
if (px0 <= px1)
{
ctx.strokeStyle = pclb;
ctx.beginPath();
ctx.moveTo(xPos + px0, yPos + py0);
ctx.lineTo(xPos + px1, yPos + py1);
ctx.stroke();
} else
{
ctx.strokeStyle = pclb;
ctx.beginPath();
ctx.moveTo(xPos - pxs, yPos + py1 - pxs * (py1 - py0) / (px1 - px0));
ctx.lineTo(xPos + px1, yPos + py1);
ctx.stroke();
}
ctx.strokeStyle = "#00ffff";
ctx.beginPath();
ctx.moveTo(xPos + xs, yPos + fnys(pxs, pf, px0, py0, px1, py1));
ctx.lineTo(xPos + px1, yPos + py1);
ctx.stroke();
console.log(pxs, px1);
if (pxv < px1)
{
ctx.strokeStyle = "#000000";
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(xPos + px1, yPos + py1);
ctx.lineTo(xPos + pxv, yPos + pyv);
ctx.stroke();
ctx.setLineDash([]);
}
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
ctx.scale(1.5, -1.5);
xSlider.max = c.width / (2 * 1.5) - 30;
if (xAdj > xSlider.max)
{
xAdj = xSlider.max;
}
x0 = xAdj - c.width / (2 * 1.5);
xs = c.width / (2 * 1.5);
}, true);
/*document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '38') {
yplus();
}
else if (e.keyCode == '40') {
yminus();
}
else if (e.keyCode == '37') {
xminus();
}
else if (e.keyCode == '39') {
xplus();
}
else if (e.keyCode == '49') {
//togglec();
}
else if (e.keyCode == '50') {
toggled();
}
else if (e.keyCode == '51') {
//togglee();
}
}*/
function toggleClick()
{
checkBox = document.getElementById("toggle");
toggle = checkBox.checked;
Clear(ctx);
}
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>