|
30 | 30 | .game[width] + .loading {
|
31 | 31 | display: none;
|
32 | 32 | }
|
| 33 | + span { |
| 34 | + --outline: #ccc; |
| 35 | + font-family: sans-serif; |
| 36 | + font-size: 18px; |
| 37 | + display: none; |
| 38 | + justify-content: center; |
| 39 | + align-items: center; |
| 40 | + position: relative; |
| 41 | + padding-bottom: 3px; |
| 42 | + width: 48px; |
| 43 | + height: 45px; |
| 44 | + border-radius: 5px; |
| 45 | + background-color: #fff; |
| 46 | + color: #444; |
| 47 | + border-top: 1px solid var(--outline); |
| 48 | + box-shadow: 0px 3px 0px 2px var(--outline); |
| 49 | + -webkit-user-select: none; |
| 50 | + user-select: none; |
| 51 | + } |
| 52 | + section { |
| 53 | + --cell-length: 56px; |
| 54 | + display: grid; |
| 55 | + grid-template-columns: repeat(3, 1fr); |
| 56 | + grid-template-rows: repeat(2, 1fr); |
| 57 | + place-items: center; |
| 58 | + grid-gap: 1px; |
| 59 | + width: calc(var(--cell-length) * 3); |
| 60 | + height: calc(var(--cell-length) * 2); |
| 61 | + } |
| 62 | + section ~ section { |
| 63 | + margin-top: 50px; |
| 64 | + } |
| 65 | + section > span:nth-child(1) { |
| 66 | + grid-column: 2; |
| 67 | + grid-row: 1; |
| 68 | + } |
| 69 | + section > span:nth-child(2) { |
| 70 | + grid-column: 1; |
| 71 | + grid-row: 2; |
| 72 | + } |
| 73 | + section#a > span:nth-child(1), |
| 74 | + section > span:nth-child(3) { |
| 75 | + grid-column: 2; |
| 76 | + grid-row: 2; |
| 77 | + } |
| 78 | + section > span:nth-child(4) { |
| 79 | + grid-column: 3; |
| 80 | + grid-row: 2; |
| 81 | + } |
| 82 | + #a, |
| 83 | + #b { |
| 84 | + position: fixed; |
| 85 | + bottom: 0; |
| 86 | + } |
| 87 | + #a { |
| 88 | + left: 0; |
| 89 | + } |
| 90 | + #b { |
| 91 | + right: 0; |
| 92 | + } |
| 93 | + @media (pointer: coarse) { |
| 94 | + span { |
| 95 | + display: flex; |
| 96 | + } |
| 97 | + } |
33 | 98 | </style>
|
34 | 99 | </head>
|
35 | 100 | <body>
|
36 | 101 | <div class="emscripten">
|
37 |
| - <canvas class="game portrait" oncontextmenu="event.preventDefault()"></canvas> |
| 102 | + <canvas class="game portrait" oncontextmenu="event.preventDefault()" data-key="Enter" data-which="13"></canvas> |
| 103 | + <section id="a"> |
| 104 | + <span data-key="KeyZ" data-which="90">z</span> |
| 105 | + <span data-key="KeyX" data-which="88">x</span> |
| 106 | + </section> |
| 107 | + <section id="b"> |
| 108 | + <span data-key="ArrowUp" data-which="38">↑</span> |
| 109 | + <span data-key="ArrowLeft" data-which="37">←</span> |
| 110 | + <span data-key="ArrowDown" data-which="40">↓</span> |
| 111 | + <span data-key="ArrowRight" data-which="39">→</span> |
| 112 | + </section> |
38 | 113 | <div class="loading">Loading...</div>
|
39 | 114 | </div>
|
40 | 115 | <script type="text/javascript">
|
|
45 | 120 | $game.classList.toggle('portrait');
|
46 | 121 | }
|
47 | 122 | }
|
| 123 | + const create_and_fire_event = function (event, type) { |
| 124 | + const control_event = new Event(type, { bubbles: true }); |
| 125 | + |
| 126 | + control_event.code = event.target.dataset.key; |
| 127 | + control_event.key = event.target.dataset.key; |
| 128 | + |
| 129 | + control_event.keyCode = event.target.dataset.which; |
| 130 | + control_event.which = event.target.dataset.which; |
| 131 | + |
| 132 | + Module.canvas.dispatchEvent(control_event); |
| 133 | + }; |
48 | 134 | var Module = {
|
| 135 | + preRun: [], |
| 136 | + postRun: [ |
| 137 | + function () { |
| 138 | + if (window.matchMedia("(pointer: coarse)").matches) { |
| 139 | + // touchscreen |
| 140 | + Array.from(document.querySelectorAll('span, canvas')).forEach(function (control) { |
| 141 | + control.addEventListener('touchstart', function (event) { |
| 142 | + event.preventDefault(); |
| 143 | + create_and_fire_event(event, 'keydown'); |
| 144 | + }); |
| 145 | + control.addEventListener('touchend', function (event) { |
| 146 | + event.preventDefault(); |
| 147 | + create_and_fire_event(event, 'keyup'); |
| 148 | + }); |
| 149 | + control.addEventListener('touchcancel', function (event) { |
| 150 | + event.preventDefault(); |
| 151 | + create_and_fire_event(event, 'keyup'); |
| 152 | + }); |
| 153 | + }); |
| 154 | + } |
| 155 | + } |
| 156 | + ], |
49 | 157 | print(text) {
|
50 | 158 | console.log(Array.prototype.slice.call(arguments).join(' '));
|
51 | 159 | },
|
|
0 commit comments