-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharmonic_oscillator.html
151 lines (122 loc) · 3.08 KB
/
harmonic_oscillator.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
input[type="range"] {
-webkit-appearance: none;
width: 100%;
margin: 4.15px 0;
}
input[type="range"]:focus {
outline: none;
}
input[type="range"]::-webkit-slider-runnable-track {
width: 100%;
height: 6.7px;
cursor: pointer;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
background: #ffffff;
border-radius: 1.3px;
border: 0.2px solid #010101;
}
input[type="range"]::-webkit-slider-thumb {
box-shadow: 0.9px 0.9px 1px #000000, 0px 0px 0.9px #0d0d0d;
border: 1.1px solid #000000;
height: 15px;
width: 40px;
border-radius: 50px;
background: red;
cursor: pointer;
-webkit-appearance: none;
margin-top: -4.35px;
}
input[type="range"]:focus::-webkit-slider-runnable-track {
background: #ffffff;
}
</style>
</head>
<body>
<script>
// x = A cos(omega0t + phi)
let x = 0.1; //displacement
let k = 0.2; //spring constant
let m = 25; //mass
let A = 200; //amplitude
let phi = 0; //phase
let omega0; //natural frequency
let step = 1;
let totalWidthWave;
var sliderK;
var sliderM;
var sliderA;
function setup() {
createCanvas(800, 600);
sliderA = createSlider(1, 150, 120, 1);
sliderA.position(25, 30);
sliderA.style("width", "250px");
sliderK = createSlider(0, 2, 0.1, 0.01);
sliderK.position(25, 80);
sliderK.style("width", "250px");
sliderM = createSlider(1, 500, 25, 1);
sliderM.position(25, 130);
sliderM.style("width", "250px");
}
function draw() {
background(220);
grid();
noStroke();
textSize(16);
A = sliderA.value();
text("Amplitude: " + A, 30, 20);
k = sliderK.value();
text("Spring constant: " + k, 30, 70);
m = sliderM.value();
text("Mass: " + m, 30, 120);
omega0 = sqrt(k / m);
calcWave();
renderWave();
}
function grid() {
stroke("#000000");
fill("#000000");
strokeWeight(1);
for (let i = 0; i < width; i += 20) {
line(i, 0, i, height);
}
for (let j = 0; j < height; j += 20) {
line(0, j, width, j);
}
strokeWeight(3);
line(width / 2, 0, width / 2, height);
line(0, height / 2, width, height / 2);
}
function calcWave() {
totalWidthWave = width + 26;
yvalues = new Array(floor(totalWidthWave / step));
for (let t = 0; t < yvalues.length; t++) {
if (omega0 == 0) {
yvalues[t] = 0;
} else {
yvalues[t] = A * cos(omega0 * t + phi);
}
}
}
function renderWave(x, y) {
noFill();
stroke("red");
strokeJoin(ROUND);
beginShape();
for (let x = 0; x < yvalues.length; x += 1) {
vertex(x * step, yvalues[x] + height / 2);
}
endShape();
}
</script>
</body>
</html>