OSDN Git Service

5b8dc9e73e2f38f5b2a9bd861fb1ba884f36f9b1
[automap/automap.git] / Automap / MapSRC / src / index.js
1 const vf = new ViewFrame();
2 vf.reloadChunkList();
3
4 // the event handlers are in iifes so they dont make unneeded globals.
5 // resize, delay re-render to reduce lag.
6 (function () {
7         var id;
8         window.addEventListener('resize', () => {
9                 clearTimeout(id);
10                 id = setTimeout(() => {
11                         vf.render();
12                 }, 500);
13         });
14 }());
15
16 // panning
17 (function () {
18         var id;
19         vf.map.canvas.addEventListener('mousedown', event => {
20                 clearTimeout(id);
21                 vf.moveCenter(event.pageX, event.pageY);
22                 id = setTimeout(() => {
23                         vf.render();
24                 }, 250);
25         });
26 }());
27
28
29 // #### CONTROLS ####
30 // hovering
31 (function () {
32         var lastX = 0;
33         var lastY = 0;
34         vf.map.canvas.addEventListener('mousemove', event => {
35                 // only count if the mouse moved more than a chunk
36                 let x = Math.floor(vf.x +
37                         (event.clientX - vf.width / 2) / vf.zoom);
38                 let y = Math.floor(vf.y +
39                         (event.clientY - vf.height / 2) / vf.zoom);
40                 if (x == lastX && y == lastY) return;
41                 lastX = x;
42                 lastY = y;
43                 vf.updateInfobox(x + '_' + y);
44         });
45 }());
46
47 // scroll/zoom
48 (function () {
49         var id;
50         vf.map.canvas.addEventListener('wheel', event => {
51                 clearTimeout(id);
52                 vf.zoom += -Math.sign(event.deltaY)*2;
53                 id = setTimeout(() => {
54                         vf.render();
55                 }, 250);
56         });
57 }());
58
59 // reload the chunk list every six seconds
60 var devBlockReload = false; // disable via command line
61 (function () {
62         setInterval(() => {
63                 if (devBlockReload) return;
64                 vf.reloadChunkList();
65         }, 6000);
66 }());