-
-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathexport.html
More file actions
116 lines (100 loc) · 5.34 KB
/
export.html
File metadata and controls
116 lines (100 loc) · 5.34 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TIC-80 tiny computer</title>
<style type="text/css">
#game-frame > div { font-size: 44px; font-family: monospace; font-weight: bold;}
/* You can set custom dimensions here */
.game { width: 100vw; height: 100vh; }
/* Uncomment to remove the frame on top of the canvas and start the game directly */
/* #game-frame { display: none; } */
</style>
</head>
<body style="margin:0; padding:0;">
<div class="game" style="margin: 0; position: relative; background: #1a1c2c;">
<div id="game-frame" style="cursor: pointer; position: absolute; margin: 0 auto; opacity: 1; background: #1a1c2c; width: 100%; height: 100%;">
<div style="text-align: center; color: white; display: flex; justify-content: center; align-items: center; width: 100%; height: 100%;">
<p style="margin: 0;">- CLICK TO PLAY -</p>
</div>
</div>
<canvas style="width: 100%; height: 100%; margin: 0 auto; display: block; image-rendering: pixelated;" id="canvas" oncontextmenu="event.preventDefault()" onmousedown="window.focus()" tabindex="0"></canvas>
</div>
<script type="text/javascript">
var Module = {canvas: document.getElementById('canvas'), arguments:['cart.tic']};
// Safari/WebKit-only: remap Arrow keys with location 3 (numpad) to location 0.
(function(){
const ua = navigator.userAgent || '';
const isWebKit = /AppleWebKit/i.test(ua);
if (!isWebKit) return;
const synthesizeArrowWithLocation0 = (orig) => {
try {
const ev = new KeyboardEvent(orig.type, {
key: orig.key,
code: orig.code,
location: 0,
repeat: orig.repeat,
ctrlKey: orig.ctrlKey,
shiftKey: orig.shiftKey,
altKey: orig.altKey,
metaKey: orig.metaKey,
bubbles: true,
cancelable: true,
});
Object.defineProperty(ev, 'which', { get: () => orig.which });
Object.defineProperty(ev, 'keyCode', { get: () => orig.keyCode });
return ev;
} catch { return null; }
};
const remapArrowLocationCapture = (e) => {
const isArrow = (e.key && e.key.startsWith('Arrow')) || (e.code && e.code.startsWith('Arrow'));
if (!isArrow) return;
if (e.location === 3) {
const syn = synthesizeArrowWithLocation0(e);
if (syn) {
e.stopImmediatePropagation();
e.preventDefault();
(e.target || document.getElementById('canvas')).dispatchEvent(syn);
}
}
};
['keydown','keyup'].forEach(t => document.addEventListener(t, remapArrowLocationCapture, true));
})();
// Prevent page scrolling while the game canvas is focused
(function(){
const canvasEl = document.getElementById('canvas');
const preventArrowDefault = (e) => {
const key = e.key || e.code;
const kc = e.keyCode || e.which;
const isArrow = (key && ['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(key)) || (kc >= 37 && kc <= 40);
if (isArrow && document.activeElement === canvasEl) e.preventDefault();
};
document.addEventListener('keydown', preventArrowDefault, { passive: false });
canvasEl.addEventListener('keydown', preventArrowDefault, { passive: false });
})();
const gameFrame = document.getElementById('game-frame')
const displayStyle = window.getComputedStyle(gameFrame).display;
if (displayStyle === 'none')
{
let scriptTag = document.createElement('script'), // create a script tag
firstScriptTag = document.getElementsByTagName('script')[0]; // find the first script tag in the document
scriptTag.src = 'tic80.js'; // set the source of the script to your script
firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag); // append the script to the DOM
// Focus canvas to ensure keyboard events are routed to the game
document.getElementById('canvas').focus()
} else {
gameFrame.addEventListener('click', function() {
let scriptTag = document.createElement('script'), // create a script tag
firstScriptTag = document.getElementsByTagName('script')[0]; // find the first script tag in the document
scriptTag.src = 'tic80.js'; // set the source of the script to your script
firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag); // append the script to the DOM
this.remove()
// Focus canvas to ensure keyboard events are routed to the game
document.getElementById('canvas').focus()
});
}
</script>
</body>
</html>