-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanding_wave.html
146 lines (119 loc) · 3.04 KB
/
standing_wave.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
<!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>
var sliderA;
var sliderW;
var sliderF;
//y=A*sin((2*PI*x)/λ+ω)
let step = 0.1;
let totalWidthWave;
let frequency = 0.0;
let amplitude = 75.0;
let wavelength = 500.0;
let dx;
let yvalues;
function setup() {
createCanvas(600, 600);
sliderA = createSlider(0, 200, 75, 1);
sliderA.position(25, 30);
sliderA.style("width", "250px");
sliderW = createSlider(1, 700, 600, 1);
sliderW.position(25, 80);
sliderW.style("width", "250px");
sliderF = createSlider(0, 1, 0, 0.01);
sliderF.position(25, 130);
sliderF.style("width", "250px");
}
function draw() {
background(220);
grid();
noStroke();
textSize(16);
amplitude = sliderA.value();
text("Amplitude: " + amplitude, 30, 20);
wavelength = sliderW.value();
text("Wavelength: " + wavelength, 30, 70);
freqSlider = sliderF.value();
text("Angular frequency: " + freqSlider, 30, 120);
calcWave();
renderWave();
}
function grid() {
stroke("#000000");
fill("#000000");
strokeWeight(1);
for (let i = 0; i < width; i += 30) {
line(i, 0, i, height);
}
for (let j = 0; j < height; j += 30) {
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;
dx = (TWO_PI / wavelength) * step;
yvalues = new Array(floor(totalWidthWave / step));
frequency += freqSlider;
let x = frequency;
for (let i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x) * amplitude;
x += dx;
}
}
function renderWave() {
noFill();
stroke("red");
beginShape();
for (let x = 0; x < yvalues.length; x += 1) {
vertex(x * step, height / 2 + yvalues[x]);
}
endShape();
}
</script>
</body>
</html>