-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
83 lines (78 loc) · 2.46 KB
/
map.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
let img = {"w": 750, "h": 625};
let canvas = {"w": 1600, "h": 900};
let limits = {"min": 0.5, "max": 100};
let map = [];
let posx = 0;
let posy = 0;
let startDragX = 0;
let startDragY = 0;
let dragging = false;
let debug = false;
let zoomFactor = 1;
let zoomLevel = 1;
let ctx;
function initMap() {
for(let x = 0;x<Math.pow(2, zoomLevel-1);x++) {
map[x] = [];
for(let y = 0;y<Math.pow(2, zoomLevel-1);y++) {
map[x][y] = new Image();
map[x][y].onload = function(){ if(debug){console.log("loaded !");} drawMap(); }
map[x][y].src = "img/"+(zoomLevel-1)+"/"+x+"-"+y+".png";
//map[x][y].src = "http://lorempixel.com/"+img.w+"/"+img.h;
}
}
}
function drawMap() {
if(debug){ console.log("ZoomFactor: "+zoomFactor+" at level "+zoomLevel); }
ctx.clearRect(0, 0, canvas.w, canvas.h);
for(let x = 0;x<Math.pow(2, zoomLevel-1);x++) {
for(let y = 0;y<Math.pow(2, zoomLevel-1);y++) {
if(map[x][y].complete) { ctx.drawImage(map[x][y],
x*img.w*zoomFactor/Math.pow(2, zoomLevel-1) - posx +x * (debug ? 1 : -1),
y*img.h*zoomFactor/Math.pow(2, zoomLevel-1) - posy +y * (debug ? 1 : -1),
img.w*zoomFactor/Math.pow(2, zoomLevel-1),
img.h*zoomFactor/Math.pow(2, zoomLevel-1)); }
}
}
}
$("body").append("<canvas id=\"scene\" width=\""+canvas.w+"\" height=\""+canvas.h+"\"></canvas>");
let cvs = document.getElementById("scene");
if (cvs.getContext) {
ctx = cvs.getContext("2d");
initMap();
drawMap(ctx);
$("#scene").mousedown((e) => { dragging = true; startDragX = e.clientX + posx; startDragY = e.clientY + posy; })
.mouseup(() => { dragging = false; })
.mousemove((e) => {
if(dragging) {
posx = startDragX - e.clientX;
posy = startDragY - e.clientY;
drawMap(ctx);
}
})
$(window).on("DOMMouseScroll", (e) => {
e.preventDefault();
if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
// zoom in
if(zoomFactor < limits.max) {
zoomFactor *= 1.1;
if(zoomFactor/Math.pow(2, zoomLevel-1) > 1 && zoomLevel < 5) { zoomLevel++; initMap(); }
posx += (e.originalEvent.clientX + posx) * 0.1;
posy += (e.originalEvent.clientY + posy) * 0.1;
}
}
else {
// zoom out
if(zoomFactor > limits.min) {
zoomFactor *= 0.9;
if(zoomFactor/Math.pow(2, zoomLevel-2) < 1 && zoomLevel > 1) { zoomLevel--; initMap(); }
posx -= (e.originalEvent.clientX + posx) * 0.1;
posy -= (e.originalEvent.clientY + posy) * 0.1;
}
}
drawMap();
})
.on("keypress", (e) => {
if(e.which == 100) { debug = !debug; drawMap(); }
});
}