Class Accel

Introducing Accel (accelerometer, on mobile) support for WebChucK. Accel wraps JavaScript DeviceMotionEvent listeners easing access to mobile device accelerometers in WebChucK code.

To get started with Accel:

import { Chuck, Accel } from "webchuck";

const theChuck = await Chuck.init([]);
const accel = await Accel.init(theChuck); // Initialize Accel

The devicemotion event gives the acceleration of the device on the three axes: x, y, and z. Acceleration is expressed in m/s². More on the devicemotion 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 accelerometer permission
if (typeof DeviceMotionEvent.requestPermission === 'function') {
await DeviceMotionEvent.requestPermission();
}

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

Hierarchy

  • Accel

Properties

theChuck: Chuck
_accelActive: boolean = false
boundHandleMotion: {}

Type declaration

    Methods

    • Initialize Accel functionality in your WebChucK instance. This adds a Accel and AccelMsg class to the ChucK Virtual Machine (VM). Accelerometer event (DeviceMotionEvent) listeners are added if enableAccel is true (default).

      Parameters

      • theChuck: Chuck
      • enableAccel: boolean = true

      Returns Promise<Accel>

      Example

      theChuck = await Chuck.init([]);
      accel = await Accel.init(theChuck); // Initialize Accel
    • Enable Javascript event (DeviceMotionEvent) listeners for Accel

      Returns void

      Example

      // If accel is not yet enabled
      accel.enableAccel();
    • Disable Javascript event (DeviceMotionEvent) listeners for Accel

      Returns void

      Example

      // If accel is enabled
      accel.disableAccel();