-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
291 lines (243 loc) · 6.55 KB
/
index.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
(() => {
const inputX = document.querySelector("[name=x-fn]");
const inputY = document.querySelector("[name=y-fn]");
const submit = document.querySelector("[type=submit]");
const example = document.querySelector("[type=button]");
const canvas1 = document.querySelector("#cartesian");
const canvas2 = document.querySelector("#polar");
const cartesianGl = canvas1.getContext("webgl");
const polarGl = canvas2.getContext("webgl");
if (!cartesianGl || !polarGl) {
return alert(
"Sorry, your system doesn't seem to support WebGL"
);
}
const unitsPerAxe = 8;
const renderCartesian = initGl(cartesianGl, unitsPerAxe);
const renderPolar = initGl(polarGl, unitsPerAxe);
setAndDraw("t", "t/2");
submit.addEventListener("click", event => {
event.preventDefault();
draw(inputX.value, inputY.value);
});
const examples = [
["cos(t)", "sin(t)"],
["2 * cos(t)^3", "2 * sin(t)^3"],
["log(t) - sin(t)", "sin(t)"],
["t * 100", "t"],
["t", "1 + sin(t * 4)"],
["t", "2 + abs(sin(t * 7)) / 3"],
["t / 2", "tan(t)"],
["sin(t * 4) * t/4", "t"],
["t^2", "abs(t)"],
["100 * cos(t)", "abs(t) / 5"],
["cos(t)^3", "3 * sin(t)^3 + log(t)"],
["tan(t)^3", "5 * sin(t) + log(t)"],
];
(() => {
let exampleIndex = 1;
example.addEventListener("click", () => {
const [exX, exY] = examples[exampleIndex];
setAndDraw(exX, exY)
exampleIndex = exampleIndex < examples.length - 1
? exampleIndex + 1
: 0;
});
})();
function setAndDraw(exprX, exprY) {
inputX.value = exprX;
inputY.value = exprY;
draw(exprX, exprY);
}
function draw(exprX, exprY) {
const compiledX = window.math.compile(exprX);
const compiledY = window.math.compile(exprY);
drawCartesian(
compiledX,
compiledY,
unitsPerAxe,
renderCartesian
);
drawPolar(
compiledX,
compiledY,
unitsPerAxe,
renderPolar
);
}
})();
function initGl(gl, unitsPerAxe) {
setupResize(gl);
const vertexShaderSource = `
attribute vec2 a_position;
uniform float u_scale;
void main() {
gl_Position = vec4(a_position.xy / u_scale, 0, 1);
}
`;
const fragmentShaderSource = `
precision mediump float;
uniform vec4 u_color;
void main() {
gl_FragColor = u_color;
}
`;
const vertexShader = createShader(
gl,
gl.VERTEX_SHADER,
vertexShaderSource
);
const fragmentShader = createShader(
gl,
gl.FRAGMENT_SHADER,
fragmentShaderSource
);
const program = createProgram(
gl,
vertexShader,
fragmentShader
);
const positionAttributeLocation = gl.getAttribLocation(
program,
"a_position"
);
const scaleUniformLocation = gl.getUniformLocation(
program,
"u_scale"
);
const colorUniformLocation = gl.getUniformLocation(
program,
"u_color"
);
const positionBuffer = gl.createBuffer();
// the following calls are usually done at render time
// but since we're just doing simple fixed 2D, we can
// afford to make them only once at setup
gl.useProgram(program);
gl.uniform1f(scaleUniformLocation, unitsPerAxe / 2);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(
positionAttributeLocation,
2, // components per iteration
gl.FLOAT, // data type
false, // no data normalization
0, // stride
0, // offset
);
return makeRender(gl, colorUniformLocation);
}
function setupResize(gl) {
function resize() {
gl.canvas.style.height = gl.canvas.clientWidth;
gl.canvas.width = gl.canvas.clientWidth;
gl.canvas.height = gl.canvas.clientHeight;
}
resize();
window.addEventListener("resize", resize);
}
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
const success = gl.getShaderParameter(
shader,
gl.COMPILE_STATUS
);
if (!success) {
const log = gl.getShaderInfoLog(shader);
const kind = type === gl.VERTEX_SHADER
? "vertex"
: "fragment";
gl.deleteShader(shader);
throw new Error(`Error in ${kind} shader: ${log}`);
}
return shader;
}
function createProgram(gl, vertexShader, fragmentShader) {
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
const success = gl.getProgramParameter(
program,
gl.LINK_STATUS
);
if (!success) {
const log = gl.getProgramInfoLog(program);
gl.deleteProgram(program);
throw new Error(`Error while linking program: ${log}`);
}
return program;
}
function makeRender(gl, colorUniformLocation) {
return (objects) => {
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
objects.forEach(({ components, primitive, color }) => {
gl.uniform4f(colorUniformLocation, ...color);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(components),
gl.STATIC_DRAW
);
gl.drawArrays(
primitive,
0,
components.length / 2
);
});
};
}
function getBasis(unitsPerAxe) {
const semiAxisUnits = unitsPerAxe / 2;
const axes = {
primitive: WebGLRenderingContext.LINES,
color: [0, 0, 0.5, 1],
components: [
-semiAxisUnits, 0,
semiAxisUnits, 0,
0, semiAxisUnits,
0, -semiAxisUnits
]
};
for (let i = -semiAxisUnits; i < semiAxisUnits; ++i) {
axes.components.push(i, 0.06, i, -0.06); // x mark
axes.components.push(-0.06, i, 0.06, i); // y mark
}
return axes;
}
function drawCartesian(exprX, exprY, unitsPerAxe, render) {
const components = [];
for (let t = -1000; t < 1000; t += 0.05) {
const x = exprX.eval({ t });
const y = exprY.eval({ t });
components.push(x, y);
}
draw(components, unitsPerAxe, render);
}
function drawPolar(exprX, exprY, unitsPerAxe, render) {
const components = [];
for (let t = -1000; t < 1000; t += 0.01) {
const angle = exprX.eval({ t });
const radius = exprY.eval({ t });
if (radius < 0) {
continue;
}
const x = radius * Math.cos(angle);
const y = radius * Math.sin(angle);
components.push(x, y);
}
draw(components, unitsPerAxe, render);
}
function draw(components, unitsPerAxe, renderFunc) {
return renderFunc([
getBasis(unitsPerAxe),
{
primitive: WebGLRenderingContext.LINE_STRIP,
color: [0.3, 1, 0.3, 1],
components
}
]);
}