-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.html
435 lines (393 loc) · 13.5 KB
/
index.html
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<html>
<head>
<style>
.dragme{
position:absolute;
cursor: move;
/*
* Prevent javascript warning because of returning false from drag handler
* (equivalent to calling preventDefault)
*/
touch-action: none;
z-index: 1;
/* border: 1px solid #000; */
}
.token {
position : absolute;
height: auto;
z-index: 1;
}
.token-label {
text-align: center;
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 4pt;
padding:0; margin:0; border:0;
background-color: rgba(255, 255, 255, 0.50);
/* border: 1px solid #000; */
}
.token-image {
/* border: 1px solid #000; */
text-align: center;
padding:0; margin:0; border:0;
}
#draggable {
background-color: #ccc;
border: 1px solid #000;
}
body {
margin: 0; /* Remove body margins */
}
/* Fullscreen Button
https://henryegloff.com/fullscreen-button/
------------------------------*/
#fullscreen-button {
position: absolute;
top: 15px;
right: 15px;
background: rgba(128,128,128,25);
border: 0;
width: 40px;
height: 40px;
border-radius: 50%;
box-sizing: border-box;
transition: transform .3s;
font-size: 0;
opacity: 1;
pointer-events: auto;
cursor: pointer;
}
#fullscreen-button:hover {
transform: scale(1.125);
}
#fullscreen-button span {
width: 4px;
height: 4px;
border-top: 2.5px solid #111; /* color */
border-left: 2.5px solid #111; /* color */
position: absolute;
outline: 1px solid transparent;
-webkit-backface-visibility: hidden;
transform: translateZ(0);
will-change: transform;
-webkit-perspective: 1000;
transition: .3s;
transition-delay: .75s;
}
#fullscreen-button span:nth-child(1) {
top: 11px;
left: 11px;
}
#fullscreen-button span:nth-child(2) {
top: 11px;
left: 22px;
transform: rotate(90deg);
}
#fullscreen-button span:nth-child(3) {
top: 22px;
left: 11px;
transform: rotate(-90deg);
}
#fullscreen-button span:nth-child(4) {
top: 22px;
left: 22px;
transform: rotate(-180deg);
}
/* Fullscreen True
------------------------------*/
[fullscreen] #fullscreen-button span:nth-child(1) {
top: 22px;
left: 22px;
}
[fullscreen] #fullscreen-button span:nth-child(2) {
top: 22px;
left: 11px;
}
[fullscreen] #fullscreen-button span:nth-child(3) {
top: 11px;
left: 22px;
}
[fullscreen] #fullscreen-button span:nth-child(4) {
top: 11px;
left: 11px;
}
/* Dark Style
------------------------------*/
[light-mode=dark] {
background: #111;
color: #fff;
}
[light-mode=dark] #fullscreen-button {
background: rgba(255,255,255,.05);
}
[light-mode=dark] #fullscreen-button span {
border-top: 2.5px solid #fff;
border-left: 2.5px solid #fff;
}
/* Center and scale image on viewport */
.imgbox2 {
display: grid;
height: 100%;
/* Use fogColor and don't leak map dimensions in non-fullscreen */
background-color: rgb(0, 0, 0);
}
.imgbox2::backdrop {
/* Clear to fog color to prevent leaking the size in fullscreen */
background-color: rgb(255,255,255);
}
.ccenter-fit {
max-width: 100%;
max-height: 100vh;
margin: auto;
}
.imgbox {
display:grid;
height: 100%;
}
.center-fill {
max-width:100%;
max-height:100%;
/* Position is necessary so when downscaling it's also centered */
/* Position and margin are necessary so it aligns to the center */
top:0; left:0; right:0; bottom:0;
margin:auto;
z-index: 0;
}
.center-fit {
max-width:100%;
max-height:100%;
/* Position is necessary so when downscaling it's also centered */
position:absolute;
/* Position and margin are necessary so it aligns to the center */
top:0; left:0; right:0; bottom:0;
margin:auto;
z-index: 0;
}
</style>
<script>
var divBeingDragged = undefined;
function startDrag(e) {
// https://stackoverflow.com/questions/17992543/how-do-i-drag-an-image-smoothly-around-the-screen-using-pure-javascript
// determine event object
if (!e) {
var e = window.event;
}
// IE uses srcElement, others use target
var target = e.target ? e.target : e.srcElement;
target = target.closest('.dragme');
if (!target) {
console.log("No dragme class, ignoring");
return
};
// calculate event X, Y coordinates
offsetX = e.clientX || e.targetTouches[0].pageX; //the same syntax for the x value
offsetY = e.clientY || e.targetTouches[0].pageY; //the same syntax for the x value
// assign default values for top and left properties
if (!target.style.left) { target.style.left='0px'};
if (!target.style.top) { target.style.top='0px'};
// calculate integer values for top and left
// properties
coordX = parseInt(target.style.left);
coordY = parseInt(target.style.top);
target.style.zIndex = 2;
divBeingDragged = target;
// move div element
document.onmousemove = dragDiv;
window.ontouchmove = dragDiv;
}
function dragDiv(e) {
if (!divBeingDragged) {
return
}
if (!e) {
var e = window.event;
}
let target = divBeingDragged;
// move div element
target.style.left = coordX + (e.clientX || e.targetTouches[0].pageX) - offsetX + 'px';
target.style.top = coordY + (e.clientY || e.targetTouches[0].pageY) - offsetY + 'px';
return false;
}
function stopDrag() {
if (divBeingDragged) {
divBeingDragged.style.zIndex = 1;
divBeingDragged = undefined;
}
}
function create_fullscreen_button() {
let fullscreen_button = document.createElement("button");
fullscreen_button.setAttribute('id', 'fullscreen-button');
fullscreen_button.addEventListener("click", toggle_fullscreen);
fullscreen_button.innerHTML = `
<span></span>
<span></span>
<span></span>
<span></span>
`;
// This needs to be on the fullscreen element since otherwise it will get
// occluded by it no matter the z-index
document.getElementById("imgbox").appendChild(fullscreen_button);
setInterval(function(){ check_fullscreen();}, 2000);
}
function toggle_fullscreen() {
console.log("windows size", window.outerWidth, window.outerHeight, " devicepixelratio", window.devicePixelRatio);
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
let elementToFullscreen = document.getElementById("imgbox");
if (elementToFullscreen.requestFullscreen) {
elementToFullscreen.requestFullscreen()
} else if (elementToFullscreen.mozRequestFullScreen) {
elementToFullscreen.mozRequestFullScreen()
} else if (elementToFullscreen.webkitRequestFullscreen) {
elementToFullscreen.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT)
}
document.body.setAttribute("fullscreen", 1)
} else {
if (document.cancelFullScreen) {
document.cancelFullScreen()
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen()
}
document.body.removeAttribute("fullscreen")
}
}
var ratio = 1.0;
var tokenWidthInPixels = 40.0;
var canvasOffsetLeft = 0.0;
var canvasOffsetTop = 0.0;
function initTokenScaling() {
let canvas = document.getElementById("canvas");
let drags = document.querySelectorAll(".dragMe");
console.log("canvas dims", canvas.offsetLeft, canvas.offsetTop, canvas.width, canvas.height);
ratio = canvas.width / canvas.naturalWidth;
canvasOffsetLeft = canvas.offsetLeft;
canvasOffsetTop = canvas.offsetTop;
// Place the tokens in the middle of the canvas to start with
for (var i = 0; i < drags.length; ++i) {
let image = document.getElementById("image"+i);
image.width = (ratio * tokenWidthInPixels);
drags[i].style.left = canvasOffsetLeft + canvas.width/2.0 + "px";
drags[i].style.top = canvas.offsetTop + canvas.height/2.0 + "px";
}
}
function updateTokenScaling() {
/**
* Need to support:
* - Draggable tokens
* - Tokens on top of the canvas both fullscreen and windowed.
* - Canvas with the map centered and maximized in both fullscreen and
* windowed. This means that the tokens need to be aware of this resizing
* since then a canvas pixel no longer corresponds to a token pixel and
* there doesn't seem to be an automatic way of making the tokens belong
* to the same resizing. Both token position and size need to be aware of
* this.
* - In addition, switching to fullscreen causes a similar resizing of the
* canvas, which the tokens affected by. This only seems to happen on
* fullscreen mobile and it doesn't happen on windowed mobile or
* fullscreen Edge desktop .
*
* The following code keeps the token size and position constant across
* resizing and fullscreen.
*
* XXX Hook this on togglefullscreen and onResize instead of the timer?
* (but the timer is there initially because fullscreen can be ended
* by non detectable means (but maybe nowadays there's an event to
* hook on?)
* )
*/
let canvas = document.getElementById("canvas");
let newRatio = canvas.width / canvas.naturalWidth;
let newCanvasOffsetLeft = canvas.offsetLeft;
let newCanvasOffsetTop = canvas.offsetTop;
if (
// Sometimes newRatio becomes infinity in one iteration but it's
// okay the previous and the next, not clear why (maybe because the
// dynamic image is being loaded?) ignore those
((ratio != newRatio) && Number.isFinite(newRatio)) ||
// Also needs to udpate the positions when the canvas is recentered
// because the window was resized even if the ratio stayed constant
((newCanvasOffsetLeft != canvasOffsetLeft) && Number.isFinite(newCanvasOffsetLeft)) ||
((newCanvasOffsetTop != canvasOffsetTop) && Number.isFinite(newCanvasOffsetTop))
) {
console.log("ratio", ratio, "newRatio", newRatio);
for (let i = 0; i < 2; ++i) {
let token = document.getElementById("token"+i);
let image = document.getElementById("image"+i);
image.width = (newRatio * tokenWidthInPixels);
let pixLeft = (parseInt(token.style.left) - canvasOffsetLeft) / ratio;
token.style.left = (newCanvasOffsetLeft + pixLeft * newRatio) + "px";
let pixTop = (parseInt(token.style.top) - canvasOffsetTop) / ratio;
token.style.top = (newCanvasOffsetTop + pixTop * newRatio) + "px";
}
ratio = newRatio;
canvasOffsetLeft = canvas.offsetLeft;
canvasOffsetTop = canvas.offsetTop;
}
console.log("canvas", canvas.offsetLeft, canvas.offsetTop);
}
function check_fullscreen() {
console.log("check_fullscreen");
// Because users can exit & enter fullscreen by other methods
if (document.fullscreenElement || document.webkitIsFullScreen || document.mozFullScreen) {
if (!document.body.getAttribute("fullscreen")) {
console.log("Entering fullscreen after the fact");
document.body.setAttribute("fullscreen", 1);
}
} else {
if (document.body.getAttribute("fullscreen")) {
console.log("Exiting fullscreen after the fact");
document.body.removeAttribute("fullscreen");
}
}
updateTokenScaling();
}
var imageToTokenRatio = undefined;
function onLoad() {
console.log("Installing drag")
document.onmousedown = startDrag;
document.onmouseup = stopDrag;
window.ontouchstart = startDrag;
window.ontouchend = stopDrag;
console.log("Positioning drags");
initTokenScaling();
console.log("Installing fullscreen");
let elem = document.getElementById("canvas");
// Fullscreen button
if (document.fullscreenEnabled || document.webkitFullscreenEnabled ||
document.msFullscreenEnabled ) {
create_fullscreen_button();
}
elem.addEventListener("dblclick", toggle_fullscreen);
console.log("Installing reload")
setInterval( function() {
const img = document.getElementById("canvas");
//img.src = "image.png" + new Date().getTime();
img.src = "image.png?" + new Date().getTime();
}, 1000)
}
</script>
</head>
<body onload="onLoad()">
<div class="imgbox" id ="imgbox">
<!---
<div class = "token dragme" id="token0">
<table>
<tr><td class="token-image">
<img src="token.png?id=0" id="image0">
</td></tr>
<tr><td class="token-label">Wizardia</td></tr>
</table>
</div>
<div class = "token dragme" id="token1">
<table>
<tr><td class="token-image">
<img src="token.png?id=1" id="image1">
</td></tr>
<tr><td class="token-label">Warrardio</td></tr>
</table>
</div>
-->
<img src="image.png" id="canvas" class="center-fill" />
</div>
</body>
</html>