Class Gyro

Introducing Gyro (gyroscope, on mobile) support for WebChucK. Gyro wraps JavaScript DeviceOrientationEvent listeners easing access to mobile device gyroscope in WebChucK code.

To get started with Gyro:

import { Chuck, Gyro } from "webchuck";

const theChuck = await Chuck.init([]);
const gyro = await Gyro.init(theChuck); // Initialize Gyro

The deviceorientation event gives motion of the device around the three axes (x, y, and z) represented in degrees from 0 to 360. More on the deviceorientation event can be found online here.

iOS devices require that the web developer ask permission from the user to access sensors after a button push. This looks like:

let runButton = document.getElementById("run");

runButton.addEventListener("click", async () => {
// Request iOS gyroscope permission
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
await DeviceOrientationEvent.requestPermission();
}

await theChuck.loadFile("./yourChuckCode.ck");
theChuck.runFile("yourChuckCode.ck");
});

Hierarchy

  • Gyro

Properties

theChuck: Chuck
_gyroActive: boolean = false
boundHandleOrientation: {}

Type declaration

    Methods

    • Initialize Gyro functionality in your WebChucK instance. This adds a Gyro and GyroMsg class to the ChucK Virtual Machine (VM). Gyrscope event (DeviceOrientationEvent) listeners are added if enableGyro is true (default).

      Parameters

      • theChuck: Chuck
      • enableGyro: boolean = true

      Returns Promise<Gyro>

      Example

      theChuck = await Chuck.init([]);
      gyro = await Gyro.init(theChuck); // Initialize Gyro
    • Enable Javascript event (DeviceOrientationEvent) listeners for Gyro

      Returns void

      Example

      // If gyro is not yet enabled
      gyro.enableGyro();
    • Disable Javascript event (DeviceOrientationEvent) listeners for Gyro

      Returns void

      Example

      // If gyro is enabled
      gyro.disableGyro();