-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogic.js
319 lines (244 loc) · 6.88 KB
/
logic.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
let params = new URLSearchParams(document.location.search);
let maps;
let map_current;
var labels = {};
var show_labels = true;
var position = { x: 0, y: 0};
var start = { x: 0, y: 0 };
var zoom = 1;
var zoom_last = zoom;
var zoom_limit = { min: 0.3, max: 15 };
var initial_pinch_distance = null;
var is_panning;
var target;
var image = new Image();
image.src = 'icon.png';
var canvas;
var ctx;
window.onload = () =>
{
target = document.getElementById("map_zoomable");
canvas = document.getElementById("canvas");
ctx = canvas.getContext('2d');
canvas.image = document.createElement("canvas");
target.addEventListener('mousedown', on_mousedown);
target.addEventListener('touchstart', (e) => handle_touch(e, on_mousedown))
target.addEventListener('mouseup', on_mouseup);
target.addEventListener('mouseleave', on_mouseup);
target.addEventListener('touchend', (e) => handle_touch(e, on_mouseup))
target.addEventListener('mousemove', on_mousemove);
target.addEventListener('touchmove', (e) => handle_touch(e, on_mousemove))
target.addEventListener('wheel', on_mousescroll);
target.addEventListener('dblclick', on_doubleclick);
load_json("maps.json");
}
// Loads map.json
async function load_json(url)
{
var response = await fetch(url);
maps = await response.json();
if (!map_current)
map_current = maps.main;
for (const [key, value] of Object.entries(maps.maps))
{
var new_button = document.createElement("button");
new_button.innerText = value.name;
new_button.onclick = function () {
params.set('map', key);
params.delete('pos');
update_params();
load_map();
};
maplist.appendChild(new_button);
}
load_map();
}
// Updates map image and labels.
function load_map()
{
if (params.has('map'))
map_current = params.get('map');
image.src = maps.maps[map_current].url;
if ("labels" in maps.maps[map_current])
labels = maps.maps[map_current].labels;
else
labels = []
toggle_hidden('labels_button', !labels.length)
console.log(`Loaded map ${map_current} ${image.naturalWidth}:${image.naturalHeight} with ${Object.keys(labels).length} labels.`);
update_transform();
image.onload = function() {
canvas.image.width = image.naturalWidth;
canvas.image.height = image.naturalHeight;
canvas.image.getContext("2d").drawImage(image, 0, 0)
draw();
get_param_position();
}
}
// Updates url
function update_params()
{
history.pushState('', '', '?' + params.toString());
}
// Gets shared position from params and then updates map position.
function get_param_position()
{
if (!params.has('pos'))
return;
// Get position from query
var new_position = {x: parseInt(params.get('pos').split(',')[0]),
y: parseInt(params.get('pos').split(',')[1])}
// Convert tile position into real position.
var delta = canvas.width / canvas.clientWidth;
update_transform();
}
// Draws map and labels.
function draw()
{
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
ctx.imageSmoothingEnabled = false;
ctx.drawImage(canvas.image, 0, 0);
if ( !show_labels )
return;
for (const iter in labels)
{
value = labels[iter];
draw_text(ctx, value.name, value.x, value.y, value.size);
}
}
function draw_text(context, text, x, y, font_size=12, font="Sans-serif")
{
context.font = `${font_size}em ${font}`;
context.strokeStyle = "black";
context.lineWidth = 12;
context.strokeText(text, x, y);
context.shadowColor = "black";
context.shadowOffsetX = 5;
context.shadowOffsetY = 5;
context.shadowBlur = 5;
context.fillStyle = "white";
context.fillText(text, x, y);
}
// Updates map position and zoom.
function update_transform()
{
if (!target)
return;
target.style.transform = `translate(${position.x}px, ${position.y}px) scale(${zoom})`;
}
// Returns clicked/touched position.
function get_event_location(e)
{
if (e.touches)
return { x:e.touches[0].clientX,
y: e.touches[0].clientY }
else if (e.clientX && e.clientY)
return { x: e.clientX,
y: e.clientY }
}
// Handles touches.
function handle_touch(e, touch_handler)
{
if (e.touches.length == 1)
{
touch_handler(e);
}
else if (e.type == "touchmove" && e.touches.length == 2)
{
is_panning = false;
handle_pinch(e);
}
}
// Zooms in or out on phones.
function handle_pinch(e)
{
e.preventDefault();
let touch1 = { x: e.touches[0].clientX,
y: e.touches[0].clientY }
let touch2 = { x: e.touches[1].clientX,
y: e.touches[1].clientY }
let currentDistance = (touch1.x - touch2.x)**2 + (touch1.y - touch2.y)**2
if (initial_pinch_distance == null)
initial_pinch_distance = currentDistance
else
on_mousescroll( e, currentDistance/initial_pinch_distance )
}
// Starts panning.
function on_mousedown(e)
{
e.preventDefault();
event_location = get_event_location(e);
start = { x: event_location.x - position.x,
y: event_location.y - position.y};
is_panning = true;
}
// Stops panning.
function on_mouseup(e)
{
is_panning = false;
initialPinchDistance = null;
zoom_last = zoom;
}
// Panning.
function on_mousemove(e)
{
e.preventDefault();
if (!is_panning)
return;
event_location = get_event_location(e);
position = { x: event_location.x - start.x,
y: event_location.y - start.y};
update_transform();
}
// Zooms in or out.
function on_mousescroll(e, pinch)
{
e.preventDefault();
event_location = get_event_location(e);
var zoom_position = { x: Math.round((event_location.x - position.x) / zoom),
y: Math.round((event_location.y - position.y) / zoom)};
if (pinch)
{
zoom = pinch * zoom_last;
}
else
{
var delta = (e.wheelDelta ? e.wheelDelta : -e.deltaY);
(delta > 0) ? (zoom *= 1.2) : (zoom /= 1.2);
}
zoom = zoom.toFixed(2);
if (zoom > zoom_limit.max)
zoom = zoom_limit.max;
if (zoom < zoom_limit.min)
zoom = zoom_limit.min;
position = { x: Math.round(event_location.x - zoom_position.x * zoom),
y: Math.round(event_location.y - zoom_position.y * zoom)};
update_transform();
}
// Updates Query params with clicked location.
function on_doubleclick(e)
{
event_location = get_event_location(e);
// Get tile position.
var delta = canvas.width / canvas.clientWidth;
var share_position = {x: Math.floor((event_location.x - position.x) / zoom * delta / 32),
y: Math.floor((event_location.y - position.y) / zoom * delta / 32)};
console.log(share_position)
//params.set("pos", `${share_position.x},${share_position.y}`)
//update_params();
}
// Button logic
function toggle_labels()
{
show_labels = !show_labels;
draw();
}
function toggle_hidden(eid, bool)
{
button = document.getElementById(eid);
if (bool != null)
button.hidden = bool;
else
button.hidden = ! button.hidden;
}