Gamepad API - DOM Element Demo
How do I make an gamepad api - dom element demo?
What is a gamepad api - dom element demo? How do you make a gamepad api - dom element demo? This script and codes were developed by Charlie Walter on 11 January 2023, Wednesday.
Gamepad API - DOM Element Demo - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Gamepad API - DOM Element Demo</title> <link href='https://fonts.googleapis.com/css?family=Press+Start+2P' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="player"> <div class="speech-bubble"> Control me with your Gamepad or Keyboard! </div>
</div>
<div id="floor"></div> <script src="js/index.js"></script>
</body>
</html>
Gamepad API - DOM Element Demo - Script Codes CSS Codes
#player{ background: #c55d22; width: 30px; height: 60px; position: absolute; left: 50%; top: 50%;
}
.speech-bubble{ width: 100px; color: #c55d22; position: relative; top: -100%; left: -40px; font-size: 14px;
}
#floor{ background: #c55d22; width: 100%; height: 50%; position: absolute; top: 50%; left: 0; margin-top: 60px;
}
body{ overflow: hidden; font-family: Helvetica Neue, Open Sans; text-align: center;
}
body:before{ content: "Gamepad API - DOM Element Demo"; color: #c55d22;
}
Gamepad API - DOM Element Demo - Script Codes JS Codes
var keys = {}, speed = 4, player = document.getElementById('player'), applyDeadzone = function(number, threshold){ percentage = (Math.abs(number) - threshold) / (1 - threshold); if(percentage < 0){ percentage = 0; } return percentage * (number > 0 ? 1 : -1); }, gamepad = null, joystickX = 0, horizontalMovement = 0, getHorizontalMovementFromKeys = function(){ movement = 0; if(keys[37]){ movement = -speed; } if(keys[39]){ movement += speed; } return movement; }, gameloop = function(){ gamepad = navigator.getGamepads()[0]; if(gamepad){ joystickX = applyDeadzone(gamepad.axes[0], 0.25) * speed; if(Math.abs(joystickX) > 0){ horizontalMovement = joystickX; }else{ horizontalMovement = getHorizontalMovementFromKeys(); } }else{ horizontalMovement = getHorizontalMovementFromKeys(); } if(Math.abs(horizontalMovement) > 0){ playerLeft = parseFloat(window.getComputedStyle(player,null).getPropertyValue("left")); if(playerLeft){ player.style.left = (playerLeft + horizontalMovement) + "px"; } } window.requestAnimationFrame(gameloop); };
window.onkeydown = function(e){ var e = e || window.event; keys[e.keyCode] = true;
}
window.onkeyup = function(e){ var e = e || window.event; delete keys[e.keyCode];
}
gameloop();
Developer | Charlie Walter |
Username | cjonasw |
Uploaded | January 11, 2023 |
Rating | 3 |
Size | 2,414 Kb |
Views | 8,096 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Collision with walls | 3,242 Kb |
Login form | 3,129 Kb |
Ninja Flash CC Animation Canvas | 10,914 Kb |
Slanty goodness | 2,707 Kb |
Angry Birds Catapult with Divs | 4,366 Kb |
Growing Root - Scroll control - CANVAS | 2,342 Kb |
Spotify Discover Panel | 1,920 Kb |
Flash CC HTML5 Canvas | 14,083 Kb |
Animating Scrolling | 3,221 Kb |
JQuery countdown | 4,386 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Console fun | Dviate | 1,500 Kb |
Single element checkbox | Ivijaygupta | 1,996 Kb |
Preloader | Rikki_Sixx | 2,815 Kb |
Velocity.js custom stagger | Tommiehansen | 4,805 Kb |
Portfolio Landing Page | FDfranklin | 3,585 Kb |
HTML5 Video Autoplay | Zivcos | 1,352 Kb |
Birthday Party Starter | Aussieyang | 1,629 Kb |
SVG hamburger menu button | Elifitch | 2,602 Kb |
A Pen by Ben Babics | Benbabics | 2,957 Kb |
My Starter Kit For Codepen | Dkdesign | 2,012 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!