-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (66 loc) · 1.76 KB
/
index.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
import TSK_expert_system from "./src/expert";
import Robot from "./src/robot";
import {
set_color,
find_xy,
reset_paint
} from "./src/painter";
import {
SIZE_W,
SIZE_H,
START_RADIUS,
FPS
} from "./src/constants";
let map;
let bw = true;
const robot = new Robot(START_RADIUS, SIZE_H / 2, 0);
const $paint = document.querySelector("#paint");
const $robot = document.querySelector("#robot");
const $bw = document.querySelector("#bw");
const $launch = document.querySelector("#launch");
const p = $paint.getContext("2d");
const r = $robot.getContext("2d");
$paint.width = SIZE_W;
$paint.height = SIZE_H;
$robot.width = SIZE_W;
$robot.height = SIZE_H;
let fps, fpsInterval, startTime, now, then, elapsed;
function animate() {
requestAnimationFrame(animate);
now = Date.now();
elapsed = now - then;
if (elapsed > fpsInterval) {
then = now - (elapsed % fpsInterval);
r.clearRect(0, 0, SIZE_W, SIZE_H);
robot.move(TSK_expert_system(robot.sensors));
robot.draw_trace(p);
robot.set_sensors(map);
robot.draw(r);
}
}
reset_paint(p);
robot.draw(r);
$launch.addEventListener("click", e => {
map = p.getImageData(0, 0, SIZE_W, SIZE_H);
fpsInterval = 1000 / FPS;
then = Date.now();
startTime = then;
animate();
});
$robot.addEventListener("mousemove", e => {
find_xy("move", e, $paint);
}, false);
$robot.addEventListener("mousedown", e => {
find_xy("down", e, $paint);
}, false);
$robot.addEventListener("mouseup", e => {
find_xy("up", e, $paint);
}, false);
$robot.addEventListener("mouseout", e => {
find_xy("out", e, $paint);
}, false);
$bw.addEventListener("click", () => {
bw = !bw;
set_color(bw);
$bw.value = `change to ${bw ? "black" : "white"}`;
});