Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

Radial vs axial deadzones in JavaScript: a small tested function

A deadzone is a small circle (or square) around the stick's center where input is ignored. Radial and axial deadzones look almost the same on paper but treat a slightly sideways push very differently. Here is a short fun

A deadzone is a small circle (or square) around the stick's center where input is ignored. Radial and axial deadzones look almost the same on paper but treat a slightly sideways push very differently. Here is a short function that does both, with test cases.

Disclosure: I built ControllerTest (https://controllertest.com), a free browser gamepad tester that includes a deadzone visualizer. This post comes from writing that tool.

The two modes

  • Radial. Take the distance of the stick from the center, hypot(x, y). If it is smaller than the deadzone, output zero. Otherwise rescale the distance and keep the direction.
  • Axial. Apply the deadzone to each axis on its own. A small push on one axis is removed even while the other axis is large.

The difference shows up when you hold the stick mostly in one direction. Radial keeps the small sideways part. Axial snaps it to zero, which can feel like the stick "sticks" to a straight line.

The function

function applyDeadzone(x, y, { inner = 0.1, outer = 1.0, mode = "radial" } = {}) {
  const scale = (v) => (Math.min(v, outer) - inner) / (outer - inner);

  if (mode === "axial") {
    const axis = (v) => (Math.abs(v) < inner ? 0 : Math.sign(v) * scale(Math.abs(v)));
    return { x: axis(x), y: axis(y) };
  }

  // radial: judge the distance from center, keep the direction
  const m = Math.hypot(x, y);
  if (m < inner) return { x: 0, y: 0 };
  const s = scale(m) / m;
  return { x: x * s, y: y * s };
}

inner is the deadzone. outer is where the output reaches its maximum, so values past it count as full tilt. The rescaling matters: without it, the output would jump from 0 to the inner value the moment you leave the deadzone.

Feed it the axes from navigator.getGamepads()[0].axes (indexes 0 and 1 for the left stick with the standard mapping).

Same input, different result

Push the stick to (0.5, 0.08) with inner = 0.1:

  • Radial returns about { x: 0.446, y: 0.071 }. The small sideways part survives.
  • Axial returns about { x: 0.444, y: 0 }. The small part is gone.

Tests

I checked the function with a few assertions in Node: a push inside the deadzone returns zero in radial mode; two small axes return zero in axial mode; axial keeps the large axis and zeroes the small one; radial keeps the direction (same angle before and after); a full push returns 1; a diagonal push never exceeds a magnitude of 1; a value exactly at the deadzone edge returns 0; negative values mirror positive ones; and a value past outer clamps to 1. All of these pass. This is a logic test with plain numbers. I have not tested it on a physical controller.

What it does not fix

A deadzone hides drift, it does not repair a worn stick, and a bigger deadzone makes small movements less precise. If a stick rests well away from center, measure it first and compare with a known-good controller. I wrote a guide on deadzone settings here: https://controllertest.com/guides/controller-deadzone-explained/. You can also move a stick and watch the raw and output dots change in the visualizer: https://controllertest.com/tools/deadzone-visualizer/.

If you see a mistake in the logic, tell me in the comments.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.