-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathpspicture.js
67 lines (58 loc) · 1.95 KB
/
pspicture.js
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
import { psgraph } from 'latex2js-pstricks';
import * as d3 from 'd3';
export default function render(that) {
const size = psgraph.getSize.call(that);
const style = `width: ${size.width}px; height: ${size.height}px;`;
const width = `${size.width}px`;
const height = `${size.height}px`;
const div = document.createElement('div');
div.className = 'pspicture';
div.style.width = width;
div.style.height = height;
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', width);
svg.setAttribute('height', height);
var d3svg = d3.select(svg);
that.$el = div;
psgraph.pspicture.call(that, d3svg);
div.appendChild(svg);
// sliders
const { env, plot } = that;
const { sliders } = env;
if (sliders && sliders.length) {
sliders.forEach((slider) => {
const { latex, scalar, variable, value, min, max } = slider;
const onChange = (event) => {
// update value
var val = event.target.value / scalar;
env.variables[variable] = val;
// update svg
d3svg.selectAll('.psplot').remove();
Object.entries(plot).forEach(([k, plot]) => {
if (k.match(/psplot/)) {
plot.forEach((data) => {
const d = data.fn.call(data.env, data.match);
if (psgraph[k] && d && d3svg) {
psgraph[k].call(d, d3svg);
}
});
}
});
};
const label = document.createElement('label');
const text = document.createTextNode(latex);
const input = document.createElement('input');
input.setAttribute('min', min * scalar);
input.setAttribute('max', max * scalar);
input.setAttribute('type', 'range');
input.setAttribute('value', value);
label.appendChild(text);
label.appendChild(input);
div.appendChild(label);
input.addEventListener('input', (event) => {
onChange(event);
});
});
}
return div;
}