Skip to content

Commit a1b66b2

Browse files
Merge devel to master. (#56)
Merge devel to master.
2 parents 7454a5e + 318cc51 commit a1b66b2

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

example/index.html

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<style>
88
@import url("https://fontlibrary.org/face/cmu-serif");
99

10+
html, body {
11+
font-size: 16px;
12+
scroll-behavior: smooth;
13+
}
14+
1015
body {
1116
margin: 0;
1217
padding: 0;
@@ -25,10 +30,10 @@
2530
/* border: 1px solid rgba(0, 0, 0, 0.2); */
2631
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
2732

28-
/* Don't use drop shadow. Every frame redraw
33+
/* Don't use drop shadow. Every frame redraws
2934
* the shadow, 60 times a second, if you use
3035
* `drop-shadow`, causing like 23ms of time to be
31-
* wasted on what the dev-tool call 'composite layers'.
36+
* wasted on what the dev-tools call 'composite layers'.
3237
* `box-shadow` above works without re-drawing itself,
3338
* use that instead.
3439
*/
@@ -45,6 +50,16 @@
4550
top: 15px;
4651
border: 1px solid rgba(0,0,0,0.4);
4752
}
53+
54+
hr {
55+
height: 6px;
56+
background: rgba(25,25,25,0.07);
57+
border: none;
58+
}
59+
60+
button {
61+
cursor: pointer;
62+
}
4863
</style>
4964
<script type="text/x-mathjax-config">
5065
MathJax.Hub.Queue(
@@ -149,6 +164,10 @@
149164
file = document.getElementById('picks').value;
150165
localStorage.setItem('last_script', file);
151166
});
167+
168+
window.onload = () => {
169+
window.scrollTo(0, document.body.scrollHeight);
170+
};
152171
</script>
153172
</body>
154173
</html>

example/vector_plot.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,56 @@ document.body.html`
1010
v(x, y) = f(x, y) &times (g(x, y)&#xee + h(x, y)&#x135)<br />
1111
v(x, y) = (<span id="factor"></span>) &times;
1212
((<span id="i"></span>)&#xee; + (<span id="j"></span>)&#x135;)
13-
<br />
13+
<br /><br />
1414
where <i>f, g</i> and <i>h</i> are scalars,<br />
1515
and <i>r</i> represents the distance of the given point in space from
16-
the origin.
16+
the origin.<br />
17+
<br /><hr /><br />
18+
<button onclick="select_plot(this)">Default</button>
19+
<button onclick="select_plot(this)">Divergent Monopole</button>
20+
<button onclick="select_plot(this)">Convergent Monopole</button>
21+
<button onclick="select_plot(this)">Dipole</button>
22+
<button onclick="select_plot(this)">Magnetic Curl around Current</button>
1723
</div>
1824
`;
1925

26+
const vec_funcs = {
27+
default: {
28+
factor: '1',
29+
i: 'y^3 - 9y',
30+
j: 'x^3 - 9x'
31+
},
32+
magnetic_curl_around_current: {
33+
factor: '1/r^2',
34+
i: '-y',
35+
j: 'x'
36+
},
37+
divergent_monopole: {
38+
factor: '1/r^3',
39+
i: 'x',
40+
j: 'y'
41+
},
42+
convergent_monopole: {
43+
factor: '-1/r^3',
44+
i: 'x',
45+
j: 'y'
46+
},
47+
dipole: {
48+
factor: '1',
49+
i: '(x+3)/sqrt((x+3)^2+y^2)^3 - (x-3)/sqrt((x-3)^2+y^2)^3',
50+
j: '(1/sqrt((x+3)^2+y^2)^3 - 1/sqrt((x-3)^2+y^2)^3)y'
51+
},
52+
};
53+
54+
window.select_plot = self => {
55+
[factor_input.value, i_input.value, j_input.value] = Object.values(
56+
vec_funcs[self.innerHTML.toLowerCase().split(' ').join('_')]
57+
);
58+
i_input.update();
59+
for (const input of [factor_input, i_input, j_input])
60+
input.fit();
61+
};
62+
2063
const factor_input = text('#factor', '1');
2164
const i_input = text('#i', 'y^3 - 9y');
2265
const j_input = text('#j', 'x^3 - 9x');
@@ -79,12 +122,11 @@ factor_input.change(draw, 500).fit();
79122
css`
80123
#controls {
81124
position: absolute;
82-
top: 50%;
125+
top: calc(50% + ${sketch.height}px/2 + 40px);
83126
left: 50%;
84127
width: 80vw;
85-
transform: translate(-50%, -50%);
128+
transform: translate(-50%, 0);
86129
padding-bottom: 40px;
87-
margin-top: 340px;
88130
font-family: 'CMUSerifRoman', serif;
89131
}
90132
#controls input {
@@ -95,6 +137,15 @@ css`
95137
font-family: monospace;
96138
background: #eee;
97139
border-bottom: 1px dashed #aaa;
140+
font-size: 14px;
141+
}
142+
.hidden {
143+
font: inherit;
144+
font-family: monospace;
145+
font-size: 14px;
146+
}
147+
#controls #factor input {
148+
min-width: 50px;
98149
}
99150
.hidden {
100151
font: inherit;

lib/BasicDOM.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ class Input {
2727
this.elem = document.createElement('input');
2828
this.id = id(this.elem);
2929
this.hidden = null;
30+
this.lambda = null;
3031

3132
this.elem.type = this.type;
32-
this.elem.className = `BasicDOM-input DOM${this.id}`;
33+
this.elem.className = 'BasicDOM-input';
34+
this.elem.id = `BD-DOM-${this.id}`;
3335
document.querySelector(this.parent).appendChild(this.elem);
3436
}
3537

@@ -61,13 +63,20 @@ class Input {
6163
return this;
6264
}
6365

66+
update() {
67+
if (this.lambda !== null) this.lambda();
68+
this.fit();
69+
}
70+
6471
change(lambda, timeout = this.timeout) {
72+
this.lambda = lambda;
73+
6574
if (timeout === 0) {
6675
return this.elem.addEventListener('input', lambda);
6776
}
6877

6978
let timed = null;
70-
this.elem.addEventListener('keyup', () => {
79+
this.elem.addEventListener('input', () => {
7180
clearTimeout(timed);
7281
timed = setTimeout(lambda, timeout);
7382
});

0 commit comments

Comments
 (0)