Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- JS13K, 2025, By AlexDeltaDev twitter.com/Alex_ADEdge -->
- <!-- Based on this mapping by XEM: https://xem.github.io/articles/jsgamesinputs.html -->
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JS13K-Warmup-Template</title>
- <style>
- html, body {
- width: 100%;
- height: 100%;
- touch-action: none;
- overflow: hidden;
- display: flex;
- justify-content: center;
- align-items: center;
- background-color: #272744;
- }
- #title {
- position: absolute;
- bottom: 3px;
- left: 25px;
- color: aliceblue;
- font-family: "Lucida Console", "Courier New", monospace;
- }
- #title2 {
- position: absolute;
- bottom: 24px;
- left: 25px;
- color: aliceblue;
- font-family: "Lucida Console", "Courier New", monospace;
- }
- canvas {
- display: block;
- margin: auto;
- /* cursor: none; */
- background-color: #111111;
- }
- @media only screen and (min-device-width: 320px) and (max-device-width: 768px) and (orientation: portrait) {
- canvas {
- transform: rotate(90deg);
- transform-origin: center center;
- }
- }
- </style>
- <!-- <link rel="stylesheet" href="style.css"> -->
- <!-- <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script> -->
- <!-- <script type="module" src="./src/main.js"></script> -->
- <script>
- var up = false, right = false, down = false, left = false; // keyboard values
- var c_up = false, c_right = false, c_down = false, c_left = false; // controller values
- // Baseline variables
- var mobile, app, cvs, cx, w, h, asp, asp2, rect, rng, seed, mouseX, mouseY;
- // Scalings
- var w2 = 960; var h2 = 540;
- // Toggles
- var debug = true;
- var webGL = true;
- //GamePad
- var gp = navigator.getGamepads()[0];
- // App Setup
- window.onload = function()
- {
- initSetup();
- }
- function initSetup()
- {
- console.log("Initilizing...");
- cvs = document.getElementById('cvs');
- cx = cvs.getContext("2d");
- w = cvs.clientWidth;
- h = cvs.clientHeight;
- asp = w/h; // Aspect ratio of window
- asp2 = w2/h2; // Aspect ratio of inner cvs
- cx.imageSmoothingEnabled = false; // gritty
- // setup listeners, check for game controller
- setupEventListeners(cvs);
- if (!gp) { console.log("Gamepad not found (press a button on the controller to connect)"); }
- tick();
- }
- function tick(timestamp)
- {
- cx.clearRect(0, 0, w, h);
- // Update gamepad state every loop
- gp = navigator.getGamepads()[0];
- if (gp) {
- checkGamePad();
- }
- // Draw debug outputs to cavans
- cx.font = '16px monospace';
- cx.fillStyle = '#fff';
- if(up || c_up) { cx.fillStyle = '#3f3'; }
- cx.fillText('Up: ' + (up || c_up), 10, 30);
- cx.fillStyle = '#fff';
- if(down || c_down) { cx.fillStyle = '#3f3'; }
- cx.fillText('Down: ' + (down || c_down), 10, 50);
- cx.fillStyle = '#fff';
- if(left || c_left) { cx.fillStyle = '#3f3'; }
- cx.fillText('Left: ' + (left || c_left), 10, 70);
- cx.fillStyle = '#fff';
- if(right || c_right) { cx.fillStyle = '#3f3'; }
- cx.fillText('Right: ' + (right || c_right), 10, 90);
- // Request next frame, ie r loop
- requestAnimationFrame(tick);
- }
- // Keydown listener
- onkeydown = (e) => {
- const k = e.key.toLowerCase();
- // Up (up / W / Z)
- if (k === "arrowup" || k === "w" || k === "z") up = true;
- // Right (right / D)
- if (k === "arrowright" || k === "d") right = true;
- // Down (down / S)
- if (k === "arrowdown" || k === "s") down = true;
- // Left (left / A / Q)
- if (k === "arrowleft" || k === "a" ||k === "q") left = true;
- // console.log("E: " + e.key);
- }
- // Keyup listener
- onkeyup = (e) => {
- const k = e.key.toLowerCase();
- // Up
- if (k === "arrowup" || k === "w" || k === "z") up = false;
- // Right
- if (k === "arrowright" || k === "d") right = false;
- // Down
- if (k === "arrowdown" || k === "s") down = false;
- // Left
- if(k === "arrowleft" || k === "a" || k === "q") left = false;
- // console.log("UP: " + up);
- }
- function checkGamePad() {
- const b = gp.buttons;
- //debug all buttons
- // console.log(JSON.stringify(gp.buttons.map(b => b.value), null, 2));
- // b.forEach((btn, index) => {
- // if (btn.pressed) console.log("Button pressed:", index);
- // });
- // D-Pad buttons
- c_up = b[12]?.pressed;
- c_down = b[13]?.pressed;
- c_left = b[14]?.pressed;
- c_right = b[15]?.pressed;
- // Left stick test
- const lx = gp.axes[0], ly = gp.axes[1];
- if (lx < -0.5) c_left = true;
- if (lx > 0.5) c_right = true;
- if (ly < -0.5) c_up = true;
- if (ly > 0.5) c_down = true;
- }
- function setupEventListeners(c) {
- // Keyboard (WASD/ZQSD/Arrows) Events
- c.addEventListener('keydown', onkeydown);
- c.addEventListener('keyup', onkeyup);
- // Gamepad/Controller Events
- window.addEventListener("gamepadconnected", (e) => {
- // console.log("Gamepad connected:", e.gamepad);
- console.log(
- "Gamepad connected at index %d: %s. %d buttons, %d axes.",
- e.gamepad.index,
- e.gamepad.id,
- e.gamepad.buttons.length,
- e.gamepad.axes.length,
- );
- gp = navigator.getGamepads()[0];
- });
- window.addEventListener("gamepaddisconnected", e => {
- console.log("Gamepad disconnected:", e.gamepad.id);
- });
- // Mouse/Touch Events
- c.addEventListener('pointermove', (e) => {
- // console.log("pointermove");
- });
- c.addEventListener('pointerdown', (e) => {
- // console.log("pointerdown");
- });
- c.addEventListener('pointerup', (e) => {
- // console.log("pointerup");
- });
- c.addEventListener('pointercancel', (e) => {
- // The same as pointer up, but for mobile specific cases
- // console.log("pointercancel");
- });
- }
- </script>
- </head>
- <body>
- <h3 id="title">JS13K 2025 (Warmup/Template)</h3>
- <p id="title2">v.0.0-</p>
- <canvas id="cvs" width="960" height="540"></canvas>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement