-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent-view.html
228 lines (173 loc) · 6.45 KB
/
student-view.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D Graph - Students' view</title>
<script src="js/mathbox-bundle.js"></script>
<script src="js/dat.gui.js"></script>
<!-- http://silentmatt.com/javascript-expression-evaluator/ -->
<script src="js/parser.js"></script>
<link rel="stylesheet" href="css/mathbox.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#bgcolor {
height: 97vh;
background: rgb(22,73,125);
background: linear-gradient(90deg, rgba(22,73,125,1) 0%, rgba(11,11,38,1) 50%, rgba(22,73,125,1) 100%);
align: center;
}
</style>
</head>
<body>
<div id="bgcolor">
<script>
var element = document.querySelector('#bgcolor');
var mathbox = mathBox({ element: element,
plugins: ['core', 'controls', 'cursor', 'mathbox'],
controls: {klass: THREE.OrbitControls}
});
if (mathbox.fallback) throw "WebGL not supported"
//var three = mathbox.three;
//three.renderer.setClearColor(new THREE.Color(0xFFFFFF), 0.0);
var graphData, view;
var functionText = "a*x^2 + b*y^2"; //default equation when nothing is pushed from teacher's page
function getText () {
functionText = localStorage.getItem("confirmedFunction");
}
//var pointText = "(1,1)";
var a = 1, b = 1;
var xMin = -3, xMax = 3, yMin = -3, yMax = 3, zMin = -3, zMax = 3;
var zAutofit = true;
// start of displayGraph function ==============================================================
var displayGraphFunc = function()
{
var zFunc = Parser.parse( functionText ).toJSFunction( ['x','y'] );
graphData.set("expr",
function (emit, x, y, i, j, t)
{
emit( x, zFunc(x,y), y );
}
);
if (zAutofit)
{
var xStep = (xMax - xMin) / 256;
var yStep = (yMax - yMin) / 256;
var zSmallest = zFunc(xMin, yMin);
var zBiggest = zFunc(xMin, yMin);
for (var x = xMin; x <= xMax; x += xStep)
{
for (var y = yMin; y <= yMax; y += yStep)
{
var z = zFunc(x,y);
if (z < zSmallest) zSmallest = z;
if (z > zBiggest) zBiggest = z;
}
}
zMin = zSmallest;
zMax = zBiggest;
}
view.set("range", [[xMin, xMax], [zMin,zMax], [yMin, yMax]]);
//set the graph color to rainbow that dynamically changes with value
graphColors.set("expr",
function (emit, x, y, i, j, t)
{
var z = zFunc(x,y);
var percent = (z - 1.2 * zMin) / (zMax - 1.2 * zMin);
var color = new THREE.Color( 0xffffff );
color.setHSL( 1-percent, 1, 0.5 );
emit( color.r, color.g, color.b, 1.0 );
}
);
}
// end of displayGraph function ==============================================================
var displayGraph = function() { displayGraphFunc(); };
// setting proxy:true allows interactive controls to override base position
var camera = mathbox.camera( { proxy: true, position: [4,2,4] } );
// save as variable to adjust later
view = mathbox.cartesian(
{
range: [[xMin, xMax], [yMin, yMax], [zMin,zMax]],
scale: [2,1,2],
}
);
// axes
var xAxis = view.axis( {axis: 1, width: 8, detail: 40, color:"red"} );
var xScale = view.scale( {axis: 1, divide: 10, nice:true, zero:true} );
var xTicks = view.ticks( {width: 5, size: 15, color: "red", zBias:2} );
var xFormat = view.format( {digits: 2, font:"Arial", weight: "bold", style: "normal", source: xScale} );
var xTicksLabel = view.label( {color: "red", zIndex: 0, offset:[0,-20], points: xScale, text: xFormat} );
var yAxis = view.axis( {axis: 3, width: 8, detail: 40, color:"green"} );
var yScale = view.scale( {axis: 3, divide: 5, nice:true, zero:false} );
var yTicks = view.ticks( {width: 5, size: 15, color: "green", zBias:2} );
var yFormat = view.format( {digits: 2, font:"Arial", weight: "bold", style: "normal", source: yScale} );
var yTicksLabel = view.label( {color: "green", zIndex: 0, offset:[0,0], points: yScale, text: yFormat} );
var zAxis = view.axis( {axis: 2, width: 8, detail: 40, color:"blue"} );
var zScale = view.scale( {axis: 2, divide: 5, nice:true, zero:false} );
var zTicks = view.ticks( {width: 5, size: 15, color: "blue", zBias:2} );
var zFormat = view.format( {digits: 2, font:"Arial", weight: "bold", style: "normal", source: zScale} );
var zTicksLabel = view.label( {color: "blue", zIndex: 0, offset:[0,0], points: zScale, text: zFormat} );
view.grid( {axes:[1,3], width: 2, divideX: 20, divideY: 20, opacity:0.25} );
var graphData = view.area({
axes: [1,3], channels: 3, width: 64, height: 64,
expr: function (emit, x, y, i, j, t)
{
var z = x*y;
emit( x, z, y );
},
});
// actuall emitter set later.
var graphColors = view.area({
expr: function (emit, x,y, i,j, t)
{
if (x < 0)
emit(1.0, 0.0, 0.0, 1.0);
else
emit(0.0, 1.0, 0.0, 1.0);
},
axes: [1,3],
width: 64, height: 64,
channels: 4, // RGBA
});
// create graph in two parts, because want solid and wireframe to be different colors
// shaded:false for a solid color (curve appearance provided by mesh)
// width: width of line mesh
// note: colors will mult. against color value, so set color to white (#FFFFFF) to let colors have complete control.
var graphViewSolid = view.surface({
points:graphData,
color:"#FFFFFF", shaded:false, fill:true, lineX:false, lineY:false, colors:graphColors, visible:true, width:0
});
var graphWireVisible = true;
var graphViewWire = view.surface({
points: graphData,
color:"#000000", shaded:false, fill:false, lineX:true, lineY:true, visible:graphWireVisible, width:2
});
// display the current function as pop-up alert
var functionAlert =
{
message: function() { alert( 'z = f(x,y) = ' + functionText )}
};
// GUI controls
var gui = new dat.GUI();
//gui.add( this, 'functionText' ).name('z = f(x,y) = ');
//gui.add( this, 'displayGraph' ).name("Display Graph");
gui.add(functionAlert, 'message').name("Show Function");
var graphWireVisibleGUI = gui.add( this, "graphWireVisible" ).name("View wireframe").onChange(
function()
{
graphViewWire.set("visible", graphWireVisible);
}
);
var folder0 = gui.addFolder('Parameters');
var aGUI = folder0.add( this, 'a' ).min(0).max(5).step(0.01).name('a = ');
var bGUI = folder0.add( this, 'b' ).min(0).max(5).step(0.01).name('b = ');
folder0.open();
// onChange or onFinishChange
aGUI.onChange( displayGraphFunc );
bGUI.onChange( displayGraphFunc );
gui.open();
getText();
displayGraphFunc();
</script>
</div>
</body>
</html>