-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
94 lines (83 loc) · 2.47 KB
/
util.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
function new_svg_elem(elem) {
return $(document.createElementNS('http://www.w3.org/2000/svg', elem));
}
// function plotFunctionGraph(ctx, plot, func) {}
function funGraph(plot, func) {
var yy, x, xx;
plot.ctx.beginPath();
var needMove = false;
for (var i=0; i<plot.width; i+=2) {
x = plot.x_pixel(i);
var y = func(x);
if (i==0 || Math.abs(y-yy)>(x-xx)*100) {
needMove = true;
} else {
if (needMove) {
plot.moveTo(xx, yy);
}
plot.lineTo(x, func(x));
}
yy = y;
xx = x;
}
plot.ctx.stroke();
}
function get_querystring_params() {
// adapted from http://stackoverflow.com/a/2880929/1221660
var urlParams = {};
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) {
return decodeURIComponent(s.replace(pl, " "));
};
var query = window.location.hash.substring(1);
if (query == "") {
// be backward compatible: previously the querystring was used
// now the hash part (which doesn't require reloading)
query = window.location.search.substring(1);
}
while (match = search.exec(query)) {
urlParams[decode(match[1])] = decode(match[2]);
}
return urlParams;
}
function setCanvasEvents() {
$("#canvas").on("mousemove",function(event) {
var coords = plot.mouse_coords(event);
$("#x").html(""+coords.x);
$("#y").html(""+coords.y);
});
// if mousewheel is moved
$("#canvas").mousewheel(function(e, delta) {
if (!plot) return;
var coords = plot.mouse_coords(e);
// determine the new scale
var factor = 1.04
if (delta < 0) factor = 1.0/factor
plot.zoom(factor, coords.x, coords.y);
update();
return false;
});
}
function newPlotFromParams(params) {
var reference = {
xCenter: 0.0,
yCenter: 0.0,
radius: Math.sqrt(320*320 + 240*240) / 80
};
if (params['r']) reference.radius = parseFloat(params['r']);
if (params['x']) reference.xCenter = parseFloat(params['x']);
if (params['y']) reference.yCenter = parseFloat(params['y']);
plot = new Plot(reference);
return plot;
}
function setLocationHash(params) {
var hash = "#";
var sep = "";
for (key in params) {
hash += sep + key + "=" + encodeURIComponent(params[key]);
sep = "&";
}
history.replaceState(undefined, undefined, hash);
}