-
Notifications
You must be signed in to change notification settings - Fork 5
/
vtvt_demo_8-1.html
269 lines (232 loc) · 10.4 KB
/
vtvt_demo_8-1.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<html lang="en-CA">
<head>
<title>vtvt demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="vtvt_demo_styles.css">
<script type="text/javascript" src="vtvt.js"></script>
<style>
.slidercontainer {
display: block;
position: relative;
margin-top: 0rem;
margin-bottom: 0rem;
text-align: center;
}
@media screen and (max-width: 42em) {
.slider {
width: 80vmin; } }
@media screen and (min-width: 42em) and (max-width: 64em) {
.slider {
width: 60vmin; } }
@media screen and (min-width: 64em) {
.slider {
width: 40vmin; } }
</style>
</head>
<body>
<h3>Demo canvas #8</h3>
<p>This demo visualizes numerical solutions of the pendulum equation by 3blue1brown: https://www.youtube.com/watch?v=p_di4Zn4wz4 (14:28).</p>
<p>Instructions:</p>
<p>Position the starting point in the phase space (angle θ, angular velocity θ′) by dragging it. The starting pendulum position (L sin θ, -L cos θ) will be adjusted automatically based on the position in the phase space. Click "animate" to see the solution (2,000 iterations). Please note the current version of vtvt doesn't disable the animation control button while an animation sequence is being played, so it's best not to press the button again until the current animation sequence is over.</p>
<p>The iteration step is 0.005 by default, and can be adjusted in your browser's console by changing variable `step`. The length of the pendulum and the dampening coefficient can be changed with the sliders underneath the canvas. </p>
<p>Please note this demo may run a bit "heavy" on older mobile devices.</p>
<p><button id='animation_trigger'>Press to animate </button></p>
<div class="canvas-wrapper">
<canvas id='vector_canvas' class="canvas-wrapped"></canvas>
</div>
<div class="slidercontainer"><div style=“display:inline-block;”>
<label for="muSlider"">μ: </label>
<input type="range" id="muSlider" min="0" max="2" value="1" step="0.1" class="slider">
<label for="muSlider" id="muValue" ">1.0</label>
</div></div>
<div class="slidercontainer"><div style=“display:inline-block;”></div>
<label for="lSlider"">L: </label>
<input type="range" id="lSlider" min="0.5" max="9.9" value="4" step="0.1" class="slider">
<label for="lSlider" id="lValue" ">4.0</label>
</div></div>
<script>
//*************************************************************************************************
// Demo canvas #8
// Pendulum equations by 3blue1brown https://www.youtube.com/watch?v=p_di4Zn4wz4 (14:28)
var g = 10;
var step = 0.005; // numerical step length
// create sliders and variables for mu and l
var muSlider = document.getElementById("muSlider");
var muValue = document.getElementById("muValue");
var mu = muSlider.value;
var lSlider = document.getElementById("lSlider");
var lValue = document.getElementById("lValue");
var l = lSlider.value;
// Process slider changes
muSlider.oninput = function() {
mu = this.value;
muValue.innerHTML=Number.parseFloat(mu).toFixed(1);
scene.render();
}
lSlider.oninput = function() {
l = this.value;
lValue.innerHTML=Number.parseFloat(l).toFixed(1);
scene.render();
}
// initialize the scene
var scene = new vtvt({canvas_id: "vector_canvas", grid_res: 16, circle_rad: 0.8, point_rad: 0.07, show_matrix: false, show_eig: false, frame_duration: 0, anim_trigger_id: "animation_trigger"});
// number of starting points
var numPoints = 3;
var colours=['60,50,230','40,170,40','200,50,50'];
// add points
for (let i = 0; i < numPoints; i+=1) {
// add point
scene.addVector({
coords: [Math.random()*16-8,Math.random()*16-8],
c: colours[i],
draggable: true,
kind: 'point',
visible: true,
label: ['(θ',String.fromCharCode(i+48+8272),', ', 'θ\′', String.fromCharCode(i+48+8272), ')'].join('')
});
}
for (let i = 0; i < numPoints; i+=1) {
scene.addVector({
c: colours[i],
draggable: false,
kind: 'custom',
draw_arrow: false,
draw_point: true,
draw_stem: true,
draw_line: false,
visible: true,
label: ['L(sin θ', String.fromCharCode(i+48+8272), ', ', '-cos θ', String.fromCharCode(i+48+8272), ')'].join(''),
map_coords: function () {
return {
mapX: l*Math.sin(scene.vectors[i].coord_x),
mapY: -l*Math.cos(scene.vectors[i].coord_x)
};
}
});
}
// add axis labels using invisible vectors (a hack)
scene.addVector({
coords:[7.5,0.01],
c:[80,80,80],
draggable: false,
kind: 'custom',
draw_arrow: false,
draw_point: false,
draw_stem: false,
draw_line: false,
visible: true,
label: 'θ'
});
scene.addVector({
coords:[0.01,7.5],
c:[80,80,80],
draggable: false,
kind: 'custom',
draw_arrow: false,
draw_point: false,
draw_stem: false,
draw_line: false,
visible: true,
label: 'θ\′'
});
//scene.addVector( {coords: [-5, 5], c: '0,30,255', draggable: true, kind: 'point', label: 'START' } );
// create vector field vectors at [j,k] whose coords and colour update based on mu and l
for (let j = -8; j < 9; j+=0.5) {
for (let k = -8; k < 9; k+=0.5) {
// coordinate map_coords function
let vec_map = function() {
let x = k;
let y = -mu*k - g/l*Math.sin(j);
let norm = Math.sqrt(x*x+y*y);
x = x/norm/2;
y = y/norm/2;
return {mapX: x, mapY: y} ;
}
// determine colour based on norm (won't update itself based on mu and l!)
let colour = function() {
let max_norm = Math.sqrt(scene.grid_res*scene.grid_res/4 + Math.pow(mu*scene.grid_res/2 + g/l,2));
let x = k;
let y = -mu*k - g/l*Math.sin(j);
let norm = Math.sqrt(x*x+y*y);
let red = 155+100*norm/max_norm;
let green = 255-120*Math.abs(0.5-norm/max_norm);
let blue = 255-100*norm/max_norm;
return `${Math.round(red)}, ${Math.round(green)}, ${Math.round(blue)}`;
}
scene.addVector({origin: [j,k], c:'220,220,220', kind: 'vector', map_coords: vec_map, map_col: colour });
}
}
// add animation vectors
let tempArr=[];
for (let i = 0; i < numPoints; i+=1) {
tempArr.push( {
c: colours[i],
kind: 'point',
map_coords:function() {
return {mapX: scene.vectors[i].coord_x,
mapY: scene.vectors[i].coord_y };
}
}
);
}
for (let i = 0; i < numPoints; i+=1) {
tempArr.push({
c: colours[i],
draggable: false,
kind: 'custom',
draw_arrow: false,
draw_point: true,
draw_stem: true,
draw_line: false,
visible: true,
label: ['L(sin θ', String.fromCharCode(i+48+8272), ', ', '-cos θ', String.fromCharCode(i+48+8272), ')'].join(''),
map_coords: function () {
return {mapX: l*Math.sin(scene.vectors[i].coord_x),
mapY: -l*Math.cos(scene.vectors[i].coord_x) };
}
}
);
}
scene.addAnimationFrame(tempArr);
for (let i = 1; i<2000; i++) {
let tempArr=[];
for (let j = 0; j < numPoints; j+=1) {
tempArr.push( {
c: colours[j],
kind: 'point',
map_coords: function() {
let x = scene.vectors_animated[i-1][j].coord_x;
let y = scene.vectors_animated[i-1][j].coord_y;
return {mapX: x + y*step,
mapY: y - (mu*y + g/l*Math.sin(x))*step }
}
} );
}
for (let j = 0; j < numPoints; j+=1) {
tempArr.push({
c: colours[j],
draggable: false,
kind: 'custom',
draw_arrow: false,
draw_point: true,
draw_stem: true,
draw_line: false,
visible: true,
label: ['L(sin θ', String.fromCharCode(j+48+8272), ', ', 'cos θ', String.fromCharCode(j+48+8272), ')'].join(''),
map_coords: function () {
let x = scene.vectors_animated[i-1][j].coord_x;
let y = scene.vectors_animated[i-1][j].coord_y;
return {mapX: l*Math.sin(x + y*step),
mapY: -l*Math.cos(x + y*step) };
}
} );
}
scene.addAnimationFrame(tempArr);
}
// render
scene.render();
</script>
</body>
</html>