aboutsummaryrefslogtreecommitdiff
path: root/projects/chip8emu/index.html
blob: 212d584ee061c161834e7ebb0c0d9f6eb3b888c9 (plain)
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
---
title: CHIP8Emu
layout: project
---

<h3>CHIP8Emu</h3>

<p>An emulator for CHIP-8 written in C++</p>

<h3>Try It</h3>

<div class="emscripten" id="status">Downloading...</div>
<progress value="0" max="100" id="progress" hidden=0></progress>  
<canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex=0></canvas>

<button class="chip8-button" id="1">1</button>
<button class="chip8-button" id="2">2</button>
<button class="chip8-button" id="3">3</button>
<button class="chip8-button" id="4">4</button>
<br>
<button class="chip8-button" id="q">Q</button>
<button class="chip8-button" id="w">W</button>
<button class="chip8-button" id="e">E</button>
<button class="chip8-button" id="r">R</button>
<br>
<button class="chip8-button" id="a">A</button>
<button class="chip8-button" id="s">S</button>
<button class="chip8-button" id="d">D</button>
<button class="chip8-button" id="f">F</button>
<br>
<button class="chip8-button" id="z">Z</button>
<button class="chip8-button" id="x">X</button>
<button class="chip8-button" id="c">C</button>
<button class="chip8-button" id="v">V</button>
<br>

<script type="text/javascript" src="module.js"></script>
<script async type="text/javascript" src="chip8emu.js"></script>

<script>
  function sendKey(keyCode, eventType) {
    var c = document.getElementById("canvas");
    c.focus();
    c.dispatchEvent(new KeyboardEvent(eventType, {'keyCode': keyCode}));
  }

  const KEYS = {
    '1': 49,
    '2': 50,
    '3': 51,
    '4': 52,
    'q': 81,
    'w': 87,
    'e': 69,
    'r': 82,
    'a': 65,
    's': 83,
    'd': 68,
    'f': 70,
    'z': 90,
    'x': 88,
    'c': 67,
    'v': 86
  }

  async function tap(e) {
    sendKey(81, 'keydown');

    await new Promise(r => setTimeout(r, 10));

    sendKey(81, 'keyup');
  }

  Object.keys(KEYS).forEach(id => {
    console.log(id);
    const element = document.getElementById(id);
    element.addEventListener('click', tap);
  });
</script>