Files

177 lines
6.0 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>
Problem 8-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 = 0.002;
var t = 0;
var g = 98;
var y1 = 0;
var y2 = 0;
var y3 = 0;
var y4 = 0;
var v = 0;
var v2 = 0;
var r = 20;
var xDist = 100;
var theta = 0;
var a = 2 * g / 7;
var a2 = g / 3;
//Relative position of canvas
var xPos = -50;
var yPos = 70;
//Moves and scales canvas
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
ctx.scale(2, -2);
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
Draw();
t += dt;
v += a * dt;
v2 += a2 * dt;
y1 += v * dt;
y2 -= v * dt;
y3 += v2 * dt;
y4 -= v2 * dt;
theta -= v * dt;
if (Math.sqrt(((100 - y3) ** 2) + (400)) < 30)
{
Reset();
}
if (Math.sqrt(((100 - y4) ** 2) + (400)) < 30)
{
Reset();
}
setTimeout(Update, 5);
}
function Reset()
{
y1 = 0;
y2 = 0;
y3 = 0;
y4 = 0;
theta = 0;
v = 0;
v2 = 0;
}
function Draw()
{
ctx.setLineDash([]);
ctx.fillStyle = "#999999";
ctx.beginPath();
ctx.arc(xPos, yPos, r, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.moveTo(xPos, yPos);
ctx.lineTo(xPos + r * Math.cos(theta), yPos + r * Math.sin(theta));
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos, yPos, r, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(xPos + r, yPos);
ctx.lineTo(xPos + r, yPos - (100 - y2));
ctx.stroke();
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.arc(xPos + r, yPos - (100 - y2), Math.PI * Math.sqrt(2), 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.moveTo(xPos - r, yPos);
ctx.lineTo(xPos - r, yPos - (100 - y1));
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos - r, yPos - (100 - y1), Math.PI, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(xPos + xDist, yPos, r, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(xPos + xDist + r, yPos);
ctx.lineTo(xPos + xDist + r, yPos - (100 - y4));
ctx.stroke();
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.arc(xPos + xDist + r, yPos - (100 - y4), Math.PI * Math.sqrt(2), 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.moveTo(xPos + xDist - r, yPos);
ctx.lineTo(xPos + xDist - r, yPos - (100 - y3));
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos + xDist - r, yPos - (100 - y3), Math.PI, 0, Math.PI * 2);
ctx.fill();
ctx.setLineDash([2, 2]);
ctx.beginPath();
ctx.moveTo(-c.width, yPos - 100 + y1);
ctx.lineTo(c.width, yPos - 100 + y1);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-c.width, yPos - 100 + y2);
ctx.lineTo(c.width, yPos - 100 + y2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-c.width, yPos - 100 + y3);
ctx.lineTo(c.width, yPos - 100 + y3);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-c.width, yPos - 100 + y4);
ctx.lineTo(c.width, yPos - 100 + y4);
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(2, -2);
}, 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>