-
Notifications
You must be signed in to change notification settings - Fork 6
/
btd.js
286 lines (263 loc) · 9.32 KB
/
btd.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
var canvas = document.getElementById("btdCanvas");
var ctx = canvas.getContext('2d');
var functionObjects = [];
var screenWidth;
var divisionCount;
var useSecantRendering;
var useJSInterpretation;
var plotDensity;
var combineZoom;
var modifierZoom;
var modifierZoomX;
var modifierZoomY;
var axisStrokeWeight;
var graphStrokeWeight;
var functionStrokeWeight;
var labelTextSize;
var funcText;
var pixelMid;
var spacePerDivision;
var baseScaleX;
var baseScaleY;
var scaleX;
var scaleY;
var secantBoundOffset;
var colorList = [
"rgb(230, 25, 75)", //red
"rgb(60, 180, 75)", //green
"rgb(0, 130, 200)", //blue
"rgb(245, 130, 48)", //orange
"rgb(145, 30, 180)", //purple
"rgb(255, 225, 25)", //yellow
"rgb(70, 240, 240)", //cyan
"rgb(240, 50, 230)", //magenta
"rgb(210, 245, 60)", //lime
"rgb(250, 190, 190)", //pink
"rgb(0, 128, 128)", //teal
"rgb(128, 128, 0)" //olive
];
var presetFormFromURL = function(){ //TODO: clean this up somehow?!
var pFuncText = (new URL(location)).searchParams.get("function");
if(pFuncText != null){
$("#functionText").val(pFuncText);
}
var pDivisionCount = (new URL(location)).searchParams.get("divisionCount");
if(pDivisionCount != null){
$("#divisionCount").val(pDivisionCount);
}
var pUseSecantRendering = (new URL(location)).searchParams.get("useSecantRendering");
if(pUseSecantRendering != null){
var pUseSecantRenderingBoolean = (pUseSecantRendering == 'true');
$("#useSecantRendering").prop("checked", pUseSecantRenderingBoolean);
$("#useDotRendering").prop("checked", !pUseSecantRenderingBoolean);
}
var pUseJSInterpretation = (new URL(location)).searchParams.get("useJSInterpretation");
if(pUseJSInterpretation != null){
var pUseJSInterpretationBoolean = (pUseJSInterpretation == 'true');
$("#useJSInterpretation").prop("checked", pUseJSInterpretationBoolean);
$("#useMathJSInterpretation").prop("checked", !pUseJSInterpretationBoolean);
$("#JSfunctionHint").toggle(pUseJSInterpretationBoolean);
$("#mathJSfunctionHint").toggle(!pUseJSInterpretationBoolean);
}
var pPlotDensity = (new URL(location)).searchParams.get("plotDensity");
if(pPlotDensity != null){
$("#plotDensity").val(pPlotDensity);
}
var pCombineZoom = (new URL(location)).searchParams.get("combineZoom");
if(pCombineZoom != null){
var pCombineZoomBoolean = (pCombineZoom == 'true');
$("#combineZoom").prop("checked", pCombineZoomBoolean);
$("#noCombineZoom").prop("checked", !pCombineZoomBoolean);
}
var pModifierZoom = (new URL(location)).searchParams.get("modifierZoom");
if(pModifierZoom != null){
$("#modifierZoom").val(pModifierZoom);
}
var pModifierZoomX = (new URL(location)).searchParams.get("modifierZoomX");
if(pModifierZoomX != null){
$("#modifierZoomX").val(pModifierZoomX);
}
var pModifierZoomY = (new URL(location)).searchParams.get("modifierZoomY");
if(pModifierZoomY != null){
$("#modifierZoomY").val(pModifierZoomY);
}
var pAxisStrokeWeight = (new URL(location)).searchParams.get("axisStrokeWeight");
if(pAxisStrokeWeight != null){
$("#axisStrokeWeight").val(pAxisStrokeWeight);
}
var pGraphStrokeWeight = (new URL(location)).searchParams.get("graphStrokeWeight");
if(pGraphStrokeWeight != null){
$("#graphStrokeWeight").val(pGraphStrokeWeight);
}
var pFunctionStrokeWeight = (new URL(location)).searchParams.get("functionStrokeWeight");
if(pFunctionStrokeWeight){
$("#functionStrokeWeight").val(pFunctionStrokeWeight);
}
var pLabelTextSize = (new URL(location)).searchParams.get("labelTextSize");
if(pLabelTextSize != null){
$("#labelTextSize").val(pLabelTextSize);
}
}
presetFormFromURL();
var updateForm = function(){
divisionCount = $("#divisionCount").val();
useSecantRendering = $("#useSecantRendering").is(":checked");
useJSInterpretation = $("#useJSInterpretation").is(":checked");
$("#JSfunctionHint").toggle(useJSInterpretation);
$("#mathJSfunctionHint").toggle(!useJSInterpretation);
plotDensity = parseFloat($("#plotDensity").val());
combineZoom = $("#combineZoom").is(":checked");
modifierZoom = parseFloat($("#modifierZoom").val());
modifierZoomX = parseFloat($("#modifierZoomX").val());
modifierZoomY = parseFloat($("#modifierZoomY").val());
axisStrokeWeight = parseFloat($("#axisStrokeWeight").val());
graphStrokeWeight = parseFloat($("#graphStrokeWeight").val());
functionStrokeWeight = parseFloat($("#functionStrokeWeight").val());
labelTextSize = parseFloat($("#labelTextSize").val());
funcText = $("#functionText").val();
changeURL();
calculateValues();
evaluateFunctions();
}
var calculateValues = function(){
pixelMid = screenWidth / 2;
spacePerDivision = screenWidth / divisionCount;
void(combineZoom && ((modifierZoomX = modifierZoom) && (modifierZoomY = modifierZoom)));
baseScaleX = 1 / (screenWidth / 2);
baseScaleY = screenWidth / 2;
scaleX = baseScaleX * modifierZoomX;
scaleY = baseScaleY * 1 / modifierZoomY;
secantBoundOffset = plotDensity * 10;
}
var evaluateFunctions = function(){
functionObjects = [];
var funcTexts = funcText.split('\n');
for (var i = 0; i < funcTexts.length; i++) {
let newFunc;
try {
if(useJSInterpretation){ //Let's just eval that shi*
newFunc = new Function(["x"], "try{return " + funcTexts[i] + "}catch(e){}");
}else{ //Let's use math.js!
const mathNode = math.parse(funcTexts[i]);
const mathCode = mathNode.compile();
newFunc = function(x){
const scope = {
x: x,
t: Date.now(),
r: Math.random()
};
try{
return mathCode.eval(scope);
}catch(e){
return NaN;
}
}
}
} catch (e) {
newFunc = function(x) {
return NaN;
};
}
functionObjects.push({
func: newFunc,
color: colorList[i%funcTexts.length]
});
}
}
var changeURL = function(){
var newURL = location.href.split("?")[0]
newURL += "?function=" + encodeURIComponent(funcText);
newURL += "&divisionCount=" + encodeURIComponent(divisionCount);
newURL += "&useSecantRendering=" + encodeURIComponent(useSecantRendering);
newURL += "&useJSInterpretation=" + encodeURIComponent(useJSInterpretation);
newURL += "&plotDensity=" + encodeURIComponent(plotDensity);
newURL += "&combineZoom=" + encodeURIComponent(combineZoom);
newURL += "&modifierZoom=" + encodeURIComponent(modifierZoom);
newURL += "&modifierZoomX=" + encodeURIComponent(modifierZoomX);
newURL += "&modifierZoomY=" + encodeURIComponent(modifierZoomY);
newURL += "&axisStrokeWeight=" + encodeURIComponent(axisStrokeWeight);
newURL += "&graphStrokeWeight=" + encodeURIComponent(graphStrokeWeight);
newURL += "&functionStrokeWeight=" + encodeURIComponent(functionStrokeWeight);
newURL += "&labelTextSize=" + encodeURIComponent(labelTextSize);
history.replaceState({}, '', newURL);
}
var resizeCanvas = function(){
screenWidth = $("#cContainer").width();
canvas.width = screenWidth;
canvas.height = screenWidth;
calculateValues();
};
$("#settingsForm").on('input change', updateForm);
updateForm();
$(window).on('resize', resizeCanvas);
resizeCanvas();
drawLoop = function(){
if(plotDensity <= 0){
plotDensity = NaN;
}
//Setup
ctx.clearRect(0, 0, canvas.width, canvas.height);
//Draw axis
ctx.strokeStyle = "rgb(0,0,0)";
ctx.lineWidth = axisStrokeWeight;
ctx.beginPath();
ctx.moveTo(screenWidth / 2, 0);
ctx.lineTo(screenWidth / 2, screenWidth);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, screenWidth / 2);
ctx.lineTo(screenWidth, screenWidth / 2);
ctx.stroke();
//Draw grid
ctx.font = labelTextSize + 'px serif';
ctx.fillStyle = "rgb(0, 0, 0)";
var textSpacing = screenWidth / divisionCount;
ctx.strokeStyle = "rgb(48,48,48)";
ctx.lineWidth = graphStrokeWeight;
for (var i = -divisionCount / 2; i <= divisionCount / 2; i++) {
ctx.fillText(((i / divisionCount * 2) * modifierZoomX).toString(), (textSpacing * i) + pixelMid, pixelMid + labelTextSize);
ctx.fillText(((-i / divisionCount * 2) * modifierZoomY).toString(), pixelMid - labelTextSize, (textSpacing * i) + pixelMid);
var lineCoords = pixelMid + (screenWidth * i / divisionCount);
ctx.beginPath();
ctx.moveTo(lineCoords, 0);
ctx.lineTo(lineCoords, screenWidth);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, lineCoords);
ctx.lineTo(screenWidth, lineCoords);
ctx.stroke();
}
//Draw functions
ctx.lineWidth = functionStrokeWeight;
for (var k = -pixelMid; k <= pixelMid + plotDensity; k = k + plotDensity) { //From left to right on x axis
for (var i = 0; i < functionObjects.length; i++) { //For each function
ctx.strokeStyle = functionObjects[i].color;
ctx.fillStyle = functionObjects[i].color;
var x1 = k + pixelMid;
var y1 = (-functionObjects[i].func(k * scaleX, i) * scaleY) + pixelMid; //Evaluate point
if (useSecantRendering) {
var x2 = (k - plotDensity) + pixelMid;
var y2 = (-functionObjects[i].func((k - plotDensity) * scaleX, i) * scaleY) + pixelMid; //Evaluate second secant point
if (
x1 > (0 - secantBoundOffset) &&
x1 <= (screenWidth + secantBoundOffset) &&
x2 > (0 - secantBoundOffset) &&
x2 <= (screenWidth + secantBoundOffset) &&
y1 > (0 - secantBoundOffset) &&
y1 <= (screenWidth + secantBoundOffset) &&
y2 > (0 - secantBoundOffset) &&
y2 <= (screenWidth + secantBoundOffset)
) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
} else {
ctx.fillRect(x1-(functionStrokeWeight/2), y1-(functionStrokeWeight/2),functionStrokeWeight,functionStrokeWeight);
}
}
}
requestAnimationFrame(drawLoop);
}
requestAnimationFrame(drawLoop);