Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Simple Harmonic Motion #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
#front_page_body{
background:#091245;;}
#myContainer {
height: 100px;
position: relative;
background: yellow;
}

#myAnimation {
width: 50px;
height: 50px;
position: absolute;
top: 25px;
left: 50%;
background: red;
}
12 changes: 10 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
<html>
<head>
<meta charset="utf-8">
<title>First Project</title>
<title>Simple Harmonic Motion</title>
<script src='js/core.js'></script>
<link rel="stylesheet" type="text/css" href="css/style1.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

<p>
<button onclick="myMove()">Click Me</button>
</p>

<div id ="myContainer">
<div id ="myAnimation"></div>
</div>

</body>
</html>
29 changes: 28 additions & 1 deletion js/core.js
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
$(document).ready(function () {}
var id = null;
function myMove() {
var elem = document.getElementById("myAnimation");
var pos = elem.offsetLeft;
var Tmax = 0;
var k = 1.0; // spring constant
var m = 1.0; // mass in kg
var xt0 = 1.0; // initial position
var t0 = 0.0; // initial time
var vt0 = 0.0; // initial velocity
var dt = 0.1; // time interval
var xtold = xt0; // set & print out the initial conditions
var vtold = vt0;
clearInterval(id);
id = setInterval(frame, 10);
function frame() {
if (Tmax > 130) {
clearInterval(id);
} else {
var vt = vtold + dt*(-k/m)*xtold;
var xt = xtold + dt*vtold;
xtold = xt;
vtold = vt;
Tmax += dt;
elem.style.left = (pos + xt) + 'px';
}
}
}