Skip to content

Commit 62b8325

Browse files
committed
Update formatting of HTML with CSS. Some problems remain if viewport is narrow.
1 parent fc54c92 commit 62b8325

File tree

2 files changed

+128
-10
lines changed

2 files changed

+128
-10
lines changed

interactive/vector-addition.html

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,110 @@
66
<title>Vector Sum Plotter</title>
77
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
88
<style>
9-
#plot {
9+
body {
10+
font-family: 'Arial', sans-serif;
11+
background-color: #f9f9f9;
12+
color: #333;
13+
margin: 0;
14+
padding: 0;
15+
display: flex;
16+
flex-direction: column;
17+
align-items: center;
18+
min-height: 100vh;
19+
}
20+
h1 {
21+
text-align: center;
22+
margin-top: 20px;
23+
color: #444;
24+
}
25+
#intro {
26+
max-width: 800px;
27+
margin: 20px auto;
28+
padding: 20px;
29+
background-color: #fff;
30+
border-radius: 10px;
31+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
32+
}
33+
#container {
34+
display: flex;
35+
flex-direction: row;
36+
align-items: flex-start;
37+
justify-content: center;
38+
max-width: 1200px;
39+
margin: 20px auto;
40+
gap: 20px;
1041
width: 100%;
42+
}
43+
#vector-input {
44+
display: flex;
45+
flex-direction: column;
46+
flex: 1;
47+
}
48+
textarea {
1149
height: 600px;
50+
padding: 10px;
51+
border-radius: 5px;
52+
border: 1px solid #ccc;
53+
font-size: 16px;
54+
resize: none;
55+
}
56+
#plot {
57+
flex: 1;
58+
height: 600px;
59+
}
60+
#instructions {
61+
max-width: 800px;
62+
margin: 20px auto;
63+
padding: 20px;
64+
background-color: #fff;
65+
border-radius: 10px;
66+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
67+
}
68+
ul {
69+
padding-left: 20px;
70+
}
71+
li {
72+
margin-bottom: 10px;
73+
}
74+
@media (max-width: 768px) {
75+
#container {
76+
flex-direction: column;
77+
align-items: center;
78+
}
79+
textarea {
80+
width: 100%;
81+
}
1282
}
1383
</style>
1484
</head>
1585
<body>
16-
<h1>Vector Plotter</h1>
17-
<p>Enter a sequence of 2D vectors in the format:</p>
18-
<p>[x1, y1]<br>[x2, y2]<br>&hellip;</p>
19-
<p>The plot will show how to find the sum of the vectors by concatenating vectors such that the tail of each vector is at the head of the previous vector. The orange, dashed vector represents the resulting sum of all entered vectors.</p>
20-
<textarea id="vector-plot" rows="10" cols="50" placeholder="Enter vectors here"></textarea>
21-
<div id="plot"></div>
86+
<h1>Vector Sum Plotter</h1>
87+
<div id="intro">
88+
<p>Use this interactive tool to visualize how to find the sum of a sequence of vectors.</p>
89+
<p>Enter a sequence of 2D vectors in the format:</p>
90+
<p>[x1, y1]<br>[x2, y2]<br>&hellip;</p>
91+
<p>The sum can be found by concatenating vectors such that the tail of each vector is at the head of the previous vector. The sum of the vectors has its head at the head of the last of these concatenated vectors, and its tail at the origin. The sum is shown as an orange, dashed vector.</p>
92+
<p> </p>
93+
<p> See below the plot for things to try.</p>
94+
</div>
95+
<div id="container">
96+
<div id="vector-input">
97+
<textarea id="vector-plot" placeholder="Enter vectors here"></textarea>
98+
</div>
99+
<div id="plot"></div>
100+
</div>
101+
<div id="instructions">
102+
<p><b>Things to try:</b></p>
103+
<ul>
104+
<li>Start with a pair of vectors like [1, 1] and [4, 1].</li>
105+
<li>Compare the sum if you enter the vectors in different orders!</li>
106+
<li>What happens if you enter vectors that are scaled versions of each other, like [1, 1] and [3, 3]?</li>
107+
<li>Try entering vectors with zero components</li>
108+
<li>Try entering vectors with negative components</li>
109+
<li>Does the sum of two long vectors always yield a long vector? What is the longest possible vector you can create from two vectors of given lengths? What is the shortest possible vector?</li>
110+
<li>Try entering more than two vectors.</li>
111+
</ul>
112+
</div>
22113

23114
<script src="vector-addition.js"></script>
24115
</body>

interactive/vector-addition.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,56 @@ document.getElementById('vector-plot').addEventListener('keydown', function(even
1212

1313
const key = event.key;
1414

15+
// Prevent any key that is not in the allowedKeys list
1516
if (!allowedKeys.includes(key)) {
1617
event.preventDefault();
1718
return;
1819
}
1920

2021
const textarea = event.target;
2122

23+
// Check for the left bracket (`[`) and ensure it's unique per line
2224
if (key === '[') {
2325
const cursorPosition = textarea.selectionStart;
26+
27+
28+
// Get the content up to and including the current line
2429
const valueUpToCursor = textarea.value.substring(0, cursorPosition);
2530
const lastLineBreak = valueUpToCursor.lastIndexOf('\n');
2631
const currentLine = valueUpToCursor.substring(lastLineBreak + 1);
32+
33+
34+
// Check if the current line already contains `[` and prevent insertion if so
2735
if (currentLine.includes('[')) {
2836
event.preventDefault();
2937
return;
3038
}
3139
}
3240

41+
// Check for the right bracket (`]`) and advance to next line if it is pressed
3342
if (key === ']') {
43+
// Get the cursor position
3444
const cursorPosition = textarea.selectionStart;
45+
// Get the content up to and including the current line
46+
const valueUpToCursor = textarea.value.substring(0, cursorPosition);
47+
// Prevent the default right bracket input behavior
3548
event.preventDefault();
36-
textarea.value = textarea.value.substring(0, cursorPosition) + ']' +
49+
50+
// Insert the right bracket followed by a newline
51+
textarea.value = textarea.value.substring(0, cursorPosition) + ']\n[' +
3752
textarea.value.substring(cursorPosition);
38-
textarea.selectionStart = textarea.selectionEnd = cursorPosition + 1;
53+
54+
// Move the cursor position to after the new line
55+
textarea.selectionStart = textarea.selectionEnd = cursorPosition + 3;
3956
plotVectors();
4057
}
58+
59+
// event.preventDefault();
60+
// textarea.value = textarea.value.substring(0, cursorPosition) + ']' +
61+
// textarea.value.substring(cursorPosition);
62+
// textarea.selectionStart = textarea.selectionEnd = cursorPosition + 1;
63+
// plotVectors();
64+
// }
4165
});
4266

4367
let x_min = -5, x_max = 5, y_min = -5, y_max = 5;
@@ -205,10 +229,13 @@ function plotVectors() {
205229
xaxis: { range: [x_min, x_max] },
206230
yaxis: { range: [y_min, y_max] },
207231
showlegend: false,
232+
// autosize: true,
208233
annotations: annotations
209234
};
210235

211-
Plotly.newPlot('plot', traces, layout);
236+
var config = {responsive: true}
237+
238+
Plotly.newPlot('plot', traces, layout, config);
212239
}
213240

214241
function parseVectors(input) {

0 commit comments

Comments
 (0)