Repository navigation
Expand file tree
/
Copy pathplotter.js
More file actions
70 lines (60 loc) · 1.97 KB
/
Copy pathplotter.js
File metadata and controls
70 lines (60 loc) · 1.97 KB
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
plotter = {};
plotter.init = function() {
this.stage = document.getElementById('stage');
this.plotting = document.getElementById('plotting');
if (null == this.stage || !this.stage.getContext || null == this.plotting || !this.plotting.getContext) return;
this.axes = {
x0: .5 + .5 * this.stage.width, // x0 pixels from left to x=0
y0: .5 + .5 * this.stage.height, // y0 pixels from top to y=0
scale: 40 // 40 pixels from x=0 to x=1
};
this.showAxes();
};
plotter.checkCollisions = function(x, y) {
for (var i = 0; i < players.alive.length; ++i) {
if (Math.abs(x - players.alive[i].x) < players.alive[i].radius &&
Math.abs(y - players.alive[i].y) < players.alive[i].radius) {
alert("Busted!");
players.alive.splice(i, 1);
}
}
}
plotter.draw = function(funString, color, thick) {
var fun = function(x) {
return eval('Math.' + funString);
};
plotter.plotting.width = plotter.plotting.width;
var ctx = this.plotting.getContext("2d");
var x, y, dx = 3;
var iMax = Math.round((ctx.canvas.width - this.axes.x0) / dx);
var iMin = Math.round(-this.axes.x0 / dx);
ctx.beginPath();
ctx.strokeStyle = color;
var i = iMin;
this.plottingInterval = setInterval(function() {
if (i <= iMax) {
x = dx * i;
y = plotter.axes.scale * fun(x / plotter.axes.scale);
if (i == iMin) {
ctx.moveTo(plotter.axes.x0 + x, plotter.axes.y0 - y);
} else {
ctx.lineTo(plotter.axes.x0 + x, plotter.axes.y0 - y);
}
ctx.stroke();
plotter.checkCollisions(x, y);
++i;
} else {
clearInterval(plotter.plottingInterval);
}
}, 1);
};
plotter.showAxes = function() {
var ctx = this.stage.getContext("2d");
ctx.beginPath();
ctx.strokeStyle = "rgb(128,128,128)";
ctx.moveTo(0, this.axes.y0);
ctx.lineTo(ctx.canvas.width, this.axes.y0); // X axis
ctx.moveTo(this.axes.x0, 0);
ctx.lineTo(this.axes.x0, ctx.canvas.height); // Y axis
ctx.stroke();
};