DEV Community

Cover image for How to Make an Online Test for Gamepad Controller Using HTML5 Gamepad API
David Moffitt
David Moffitt

Posted on

How to Make an Online Test for Gamepad Controller Using HTML5 Gamepad API

In this tutorial, we will learn how to make an online test for Gamepad Controllers with HTML5 Gamepad API. Let’s get started!
We will use the following code to create a simple gamepad controller input test:


HTML

<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="gamepadtest.js"></script>
<style>
.axes {
  padding: 1em;
}

.buttons {
  margin-left: 1em;
}

.axis {
  min-width: 200px;
  margin: 1em;
}

.button {
  display: inline-block;
  width: 1em;
  text-align: center;
  padding: 1em;
  border-radius: 20px;
  border: 1px #47347C;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAAxJREFUCNdjYPjPAAACAgEAqiqeJwAAAABJRU5ErkJggg==);
  background-size: 0% 0%;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

.pressed {
  border: 1px #ED1F6A;
}

.touched::after {
  content: "touch";
  display: block;
  position: absolute;
  margin-top: -0.2em;
  margin-left: -0.5em;
  font-size: 0.8em;
  opacity: 0.7;
}
</style>
</head>
<body>  
  <h2 id="start">Press a button on your controller to start</h2>
<a href="https://github.com/davidcmoffitt/gamepad"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" alt="Find me on GitHub"></a>  
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS

var haveEvents = 'GamepadEvent' in window;
var haveWebkitEvents = 'WebKitGamepadEvent' in window;
var controllers = {};
var rAF = window.mozRequestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.requestAnimationFrame;

function connecthandler(e) {
  addgamepad(e.gamepad);
}
function addgamepad(gamepad) {
  controllers[gamepad.index] = gamepad; var d = document.createElement("div");
  d.setAttribute("id", "controller" + gamepad.index);
  var t = document.createElement("h1");
  t.appendChild(document.createTextNode("gamepad: " + gamepad.id));
  d.appendChild(t);
  var b = document.createElement("div");
  b.className = "buttons";
  for (var i=0; i<gamepad.buttons.length; i++) {
    var e = document.createElement("span");
    e.className = "button";
    e.innerHTML = i;
    b.appendChild(e);
  }
  d.appendChild(b);
  var a = document.createElement("div");
  a.className = "axes";
  for (i=0; i<gamepad.axes.length; i++) {
    e = document.createElement("meter");
    e.className = "axis";
    e.setAttribute("min", "-1");
    e.setAttribute("max", "1");
    e.setAttribute("value", "0");
    e.innerHTML = i;
    a.appendChild(e);
  }
  d.appendChild(a);
  document.getElementById("start").style.display = "none";
  document.body.appendChild(d);
  rAF(updateStatus);
}

function disconnecthandler(e) {
  removegamepad(e.gamepad);
}

function removegamepad(gamepad) {
  var d = document.getElementById("controller" + gamepad.index);
  document.body.removeChild(d);
  delete controllers[gamepad.index];
}

function updateStatus() {
  scangamepads();
  for (j in controllers) {
    var controller = controllers[j];
    var d = document.getElementById("controller" + j);
    var buttons = d.getElementsByClassName("button");
    for (var i=0; i<controller.buttons.length; i++) {
      var b = buttons[i];
      var val = controller.buttons[i];
      var pressed = val == 1.0;
      var touched = false;
      if (typeof(val) == "object") {
        pressed = val.pressed;
        if ('touched' in val) {
          touched = val.touched;
        }
        val = val.value;
      }
      var pct = Math.round(val * 100) + "%";
      b.style.backgroundSize = pct + " " + pct;
      b.className = "button";
      if (pressed) {
        b.className += " pressed";
      }
      if (touched) {
        b.className += " touched";
      }
    }

    var axes = d.getElementsByClassName("axis");
    for (var i=0; i<controller.axes.length; i++) {
      var a = axes[i];
      a.innerHTML = i + ": " + controller.axes[i].toFixed(4);
      a.setAttribute("value", controller.axes[i]);
    }
  }
  rAF(updateStatus);
}

function scangamepads() {
  var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
  for (var i = 0; i < gamepads.length; i++) {
    if (gamepads[i] && (gamepads[i].index in controllers)) {
      controllers[gamepads[i].index] = gamepads[i];
    }
  }
}

if (haveEvents) {
  window.addEventListener("gamepadconnected", connecthandler);
  window.addEventListener("gamepaddisconnected", disconnecthandler);
} else if (haveWebkitEvents) {
  window.addEventListener("webkitgamepadconnected", connecthandler);
  window.addEventListener("webkitgamepaddisconnected", disconnecthandler);
} else {
  setInterval(scangamepads, 500);
}
Enter fullscreen mode Exit fullscreen mode

I have always been fascinated by technology and the way it works, even when I was a little boy. I remember playing with my first computer as a kid, a green screen monitor/keyboard/mouse set-up that would be considered ancient nowadays, but it was amazing to me at the time.

Toys and games weren't trendy for me; they were just fun! I collected all sorts of them: Legos, Hot Wheels, and so on. But one day I got the greatest gift ever: the Nintendo 64. It changed my life forever! From then on out, all I wanted to play were video games.
My parents weren't too happy about this obsession of mine, but they eventually caved when they saw how much money I was saving by not going to the movies with friends or buying expensive new toys (remember how expensive action figures used to be?).

Video Games are fun, but they can also teach you a lot of skills that you don't realize you're learning. For instance, playing fast-paced games like Call of Duty or Halo will help your reaction time and reflexes. Patience is another great skill that can be learned from playing video games; waiting for your food bar to refill in World of Warcraft will make you much more patient.

A Breakdown of the Different Types of Game Controllers

There are many different types of game controllers available in the market. But not all of them are suitable for every gamer. In order to get the best controller for your needs, you need to know which type of controller you want and how it can be used.

There are three different types of game controllers:

a) traditional console controllers,
b) advanced controllers,
c) gamepad for mac.
you can test all of these exciting gamepad controllers on this site: https://gamepadtest.com

A traditional console controller is a wireless device that connects to the console via USB or Bluetooth. They work with most games and consoles but they require an extra power outlet. They also don’t have any kind of internal memory so they can’t save your progress or settings in-game like other modern controllers do.

An advanced controller is a wireless device that connects to the console and allows for enhanced control to be given on the game. This new controller has been created with the newest technology in mind, which allows for an easier gaming experience than ever before.

Gaming Styles

The competitive play style is a type of play style that is focused on winning. It is the most common play style in games and competitive sports. The cooperative play style is a type of play style that focuses on helping others win. It is used in cooperative games like the video game Halo. The free-for-all play style is a type of playstyle that allows players to do whatever they want without any restrictions or rules.

If you are into competitive gaming then you should consider investing in good game controller to gain a competitive edge on your rival gamers, well which controllers to consider precisely is a talk of another day.

How to Start Playing Esports without Staking Your Money

Esports is a competitive gaming phenomenon that has been gaining momentum in the past few years.

One of the most important things to consider when starting out with esports is how much you are willing to invest. It’s important to know what your goals are and how much you can afford before investing any money.

There are three main ways to start playing esports without staking your money:

  • Playing casually,

  • Playing in a team,

  • Playing in tournaments

Conclusion: Become a Professional Gamer and Discover the Fun You're Missing Out On

The popularity of gaming has grown immensely in the last few years. And with the introduction of innovations like virtual reality (VR), gaming is set to grow even more.

The idea of playing games for a living is not as far-fetched as it sounds. There are a lot of people who have turned their passion for gaming into a career and have been able to make a living from it.

There are many different ways that you can make money from playing games professionally, including streaming, creating and selling content, being an esports athlete, or even becoming part of game development teams.

Top comments (0)