-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
170 lines (144 loc) · 5.81 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
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SPACE INVADERS</title>
<link rel="icon"
href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAChklEQVR4Xu2aLW8VQRSGn9sS0pCGL0EQBIMpGBpEDSgEAgO48gcIhkAdAomoo00N4Q8UByQEgUCBqSBgWkxNU0EQfAlCSNs0J+GGuadzd2d2Z7t3mTNyds7Hvuc9Z2fmbI/MRy/z98cAMAZkjoClQOYEsCJoKWApkDkClgKZE8C+AilSYAqYVUyaB34nZtcEcF/pfAp8qmMnBQDXgWfKiWPA9zqOeWSPAt/U/A3geR07BkAd9P7KZs+AK8ATBeQ0IJTtj6/Az0iwDwPHHRlJqQ9Kxy3gdaTegeUpUsBnX5w94jyYAxYiHb0HPHJkfihQI9X5lxsASWDcq8QYkFMKXAIeKhLI3Lgztw5sRrLtFHDGkdkG3iodDzxzUWZS1ADfZzDKiRqLR3YjVOOdokQNgDa2wpeB006cLgB3VNyWgT/O3AxwNiq2sAasODIHgZtKxxLw3pnbAN7E2KlSA+Twca3EiD4MySboboxjwCIgm6H+8B2GtMoXgNSk4GEABEP1b6ExIKcUCMm7CiTaV5HCy5myGmAAeK6h9jV8CYwZA4ruJ6ukgN58+DZCCQIXpCLEl+QM0PvvUToMRd9PVmGAAaAOIMYAT2PkHbAVlMXhiw4AF9XyEDa2UgPa6gyNTA0wAFrqDRoDYhu1ZZ/BMXX7IzXoC/DLKUaHgBOqOL0EJgvq2ypwVT1/BZwrkJFeg77sCPFFbol2huktAyC8Rg+u1I0RrecjIP1Dd0jf73yBwU63xgwAhcB/zwD5lUV+aRk2PgOP1cPbwMkCGfnlRn69STqaqgFJnWxSmQHQJLpd0G0M6EKUmvTRGNAkul3QbQzoQpSa9NEY0CS6XdBtDOhClJr0MXsG7ALeGrRB5mAHsAAAAABJRU5ErkJggg==" />
<style>
@font-face {
font-family: 'Space Invaders';
src: url('font/Space-Invaders.woff2') format('woff2'),
url('font/Space-Invaders.woff') format('woff');
font-weight: normal;
font-style: normal;
}
canvas {
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
}
.page {
width: 448px;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.screen {
width: 448px;
height: 512px;
padding: 5px;
}
body {
background-image: url('artwork/backdrop_moon_midway.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
background-position: center;
font-family: 'Space Invaders', monospace;
font-size: 0.5rem;
line-height: 12px;
color: white;
background-color: black;
}
.bottom-left {
position: absolute;
bottom: 5px;
}
@media screen and (max-width: 767px) {
.bottom-left {
display: none;
}
}
.bottom-right {
position: absolute;
bottom: 5px;
right: 5px;
}
a {
color: #00ff00;
}
a:hover {
color: #ff0000;
}
</style>
</head>
<body>
<div class="bottom-left">
<p>PRESS 'C' TO INSERT A COIN</p>
<p>BACKSPACE: 2PLAYER BUTTON</p>
<p>ENTER: 1PLAYER BUTTON</p>
<p>ARROWS MOVE THE SHIP</p>
<p>Z: FIRE BUTTOn</p>
</div>
<div class="bottom-right">
By <a href="https://github.com/Rodrigodd">Rodrigodd</a></p>
</div>
<div class='page'>
<div class='screen'>
<canvas id='canvas' width='224' height='256' style='transform: scale(2); transform-origin: top left'></canvas>
</div>
</div>
<script type="module">
import init, { run_frame, key_down, key_up } from './pkg/space_invaders_wasm.js';
function resize() {
var w = document.documentElement.clientWidth;
var h = document.documentElement.clientHeight;
const scale = Math.min(Math.floor(w/224), Math.floor(h/256));
console.log(scale);
const page = document.getElementsByClassName("page")[0];
page.style.width = 224*scale;
const screen = document.getElementsByClassName("screen")[0];
screen.style.width = 224*scale;
screen.style.height = 256*scale;
const canvas = document.getElementById("canvas");
canvas.style.transform = `scale(${scale})`;
// canvas.innerHTML = `<canvas id='canvas' width='224' height='256' style='transform: scale(${scale}); transform-origin: top left'></canvas>`;
}
window.addEventListener("resize", resize);
resize();
async function run() {
await init();
const WIDTH = 224;
const HEIGHT = 256;
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
function updateDisplay(offset) {
const imageData = ctx.createImageData(WIDTH, HEIGHT);
let screen = run_frame();
for (let i = 0; i < WIDTH * HEIGHT * 4; i++) {
imageData.data[i] = screen[i];
}
ctx.putImageData(imageData, 0, 0);
};
let count = 0;
function runLoop() {
updateDisplay(count);
count += 1;
window.requestAnimationFrame(runLoop);
}
window.requestAnimationFrame(runLoop);
const translateKeys = {
37: 1, // P1 LEFT
39: 2, // P1 RIGHT
90: 3, // P1 SHOOT
67: 4, // COIN
13: 5, // P1 START
8: 6, // P2 START
};
document.addEventListener("keydown", event => {
key_down(translateKeys[event.keyCode]);
});
document.addEventListener("keyup", event => {
key_up(translateKeys[event.keyCode]);
});
}
run();
</script>
</body>
</html>