-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
114 lines (87 loc) · 2.92 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function main() {
var map = new Map(35, 25);
setupSlider();
setupCanvasControl(map);
}
function setupSlider() {
var dragging = false;
var mapPane = document.getElementById("mapPane");
var textPane = document.getElementById("textPane");
var slider = document.getElementById("slider");
slider.addEventListener('mousedown', function(e) {
e.preventDefault();
dragging = true;
document.onmousemove = function(e) {
if (dragging) {
mapPane.style.width = e.pageX + "px";
textPane.style.left = e.pageX + "px";
}
};
});
document.addEventListener('mouseup', function(e) {
if (dragging) {
dragging = false;
}
});
}
function setupCanvasControl(map) {
var canvas = document.getElementById("mainCanvas");
var isDown = false;
var transform = new Transform();
var prevPoint = [];
setInterval(function() {
render(canvas, transform, map);
}, 500);
canvas.addEventListener('mousedown', function(e) {
isDown = true;
var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
prevPoint = [x, y];
});
document.addEventListener('mouseup', function(e) {
isDown = false;
});
document.addEventListener('mousemove', function(e) {
if (!isDown) return;
var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
var mousePoint = transform.transformPointInverse(x, y);
var lastPoint = transform.transformPointInverse(prevPoint[0], prevPoint[1]);
var translateX = mousePoint[0] - lastPoint[0];
var translateY = mousePoint[1] - lastPoint[1];
transform.translate(translateX, translateY);
transform.clamp(map.pixelWidth, map.pixelHeight, canvas);
prevPoint = [x, y];
render(canvas, transform, map);
});
canvas.addEventListener('wheel', function(e) {
var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
var mousePoint = transform.transformPointInverse(x, y);
var factor = Math.pow(1.1, (e.deltaY / -40.0));
transform.translate(mousePoint[0], mousePoint[1]);
transform.scale(factor, factor);
transform.translate(-mousePoint[0], -mousePoint[1]);
transform.clamp(map.pixelWidth, map.pixelHeight, canvas);
canvas.parentElement.style.transform("scale(" + transform.m[0] + ")");
render(canvas, transform, map);
});
}
function render(canvas, transform, map) {
canvas.width = canvas.parentElement.clientWidth;
canvas.height = canvas.parentElement.clientHeight;
var context = canvas.getContext("2d");
context.setTransform(
transform.m[0],
transform.m[1],
transform.m[2],
transform.m[3],
transform.m[4],
transform.m[5]
);
context.save();
context.setTransform(1, 0, 0, 1, 0, 0);
context.clearRect(0, 0, canvas.width, canvas.height);
context.restore();
map.render(context);
}