Main Page

From Virtujoy Documentation
Revision as of 11:26, 13 September 2025 by Timothy (talk | contribs)

Web Sensor API

Sensor API mengekspos fungsionalitas sensor device hardware.[1] Mungkin kita bisa mengembangkan web app yang mengumpulkan data sensor dan mengirimkannya ke server game?

Catatan: Sensor API hanya didukung browser tertentu (misalnya Chrome).

<ul>
  <li id="gyroscope-x"></li>
  <li id="gyroscope-y"></li>
  <li id="gyroscope-z"></li>
</ul>
if (typeof Gyroscope === "undefined") {
  alert("Gyroscope API not supported on this device/browser.")
}
else {
  const gyroscope = new Gyroscope({ frequency: 60 });

  const gyroscopeLabelX = document.querySelector("#gyroscope-x");
  const gyroscopeLabelY = document.querySelector("#gyroscope-y");
  const gyroscopeLabelZ = document.querySelector("#gyroscope-z");

  gyroscope.addEventListener("reading", (e) => {
    gyroscopeLabelX.innerText = `Angular velocity along the X-axis ${gyroscope.x}`;
    gyroscopeLabelY.innerText = `Angular velocity along the Y-axis ${gyroscope.y}`;
    gyroscopeLabelZ.innerText = `Angular velocity along the Z-axis ${gyroscope.z}`;
  });

  gyroscope.start();
}