-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.js
64 lines (62 loc) · 1.49 KB
/
field.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
var Field = Field || function(canvas) {
var that = {};
if(!canvas) {
canvas = document.querySelector("canvas");
}
if(!canvas) {
canvas = document.createElement("canvas");
canvas.height = 1000;
canvas.width = 1000;
var body = document.querySelector("body");
body.appendChild(canvas);
}
var context = canvas.getContext("2d");
var robots = [];
var render = function() {
context.save();
context.setTransform(1, 0, 0, 1, 0, 0);
context.clearRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "white";
context.lineWidth = 2;
context.beginPath();
context.moveTo(0, 500);
context.lineTo(1000, 500);
context.moveTo(500, 0);
context.lineTo(500, 1000);
var i;
for(i=0; i<1000; i+=100) {
context.moveTo(i, 490);
context.lineTo(i, 510);
context.moveTo(490, i);
context.lineTo(510, i);
}
context.stroke();
context.restore();
};
var last;
var stopped = 0;
var animate = function(time) {
if(last === time) {
return;
}
last = last || time;
var diff = time-last;
last = time;
if(diff>300) {
stopped += diff;
}
render();
robots.forEach(function(robot) {
robot.calculate(time-stopped);
context.save();
robot.render(context);
context.restore();
});
window.requestAnimationFrame(animate);
};
that.add = function(robot) {
robots.push(robot);
};
window.requestAnimationFrame(animate);
return that;
};