-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from ccrma/dev
WebChucK v1.2.9
- Loading branch information
Showing
57 changed files
with
3,598 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Chuck from "./Chuck"; | ||
/** | ||
* 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: | ||
* @example | ||
* ```ts | ||
* import { Chuck, Accel } from "webchuck"; | ||
* | ||
* const theChuck = await Chuck.init([]); | ||
* const accel = await Accel.init(theChuck); // Initialize Accel | ||
* ``` | ||
*/ | ||
export default class Accel { | ||
private theChuck; | ||
private _accelActive; | ||
private boundHandleMotion; | ||
/** @internal */ | ||
constructor(theChuck: Chuck); | ||
/** | ||
* 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). | ||
* @example | ||
* ```ts | ||
* theChuck = await Chuck.init([]); | ||
* accel = await Accel.init(theChuck); // Initialize Accel | ||
*/ | ||
static init(theChuck: Chuck, enableAccel?: boolean): Promise<Accel>; | ||
/** | ||
* @internal | ||
* Check if accel is active | ||
*/ | ||
accelActive(): Promise<void>; | ||
/** | ||
* Enable Javascript event (DeviceMotionEvent) listeners for Accel | ||
* @example | ||
* ```ts | ||
* // If accel is not yet enabled | ||
* accel.enableAccel(); | ||
* ``` | ||
*/ | ||
enableAccel(): void; | ||
/** | ||
* Disable Javascript event (DeviceMotionEvent) listeners for Accel | ||
* @example | ||
* ```ts | ||
* // If accel is enabled | ||
* accel.disableAccel(); | ||
* ``` | ||
*/ | ||
disableAccel(): void; | ||
/** @internal */ | ||
private handleMotion; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Accel_ck, AccelMsg_ck } from "./accelCk"; | ||
/** | ||
* 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: | ||
* @example | ||
* ```ts | ||
* import { Chuck, Accel } from "webchuck"; | ||
* | ||
* const theChuck = await Chuck.init([]); | ||
* const accel = await Accel.init(theChuck); // Initialize Accel | ||
* ``` | ||
*/ | ||
export default class Accel { | ||
/** @internal */ | ||
constructor(theChuck) { | ||
this._accelActive = false; | ||
// Initialize members | ||
this.theChuck = theChuck; | ||
this.boundHandleMotion = this.handleMotion.bind(this); | ||
} | ||
/** | ||
* 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). | ||
* @example | ||
* ```ts | ||
* theChuck = await Chuck.init([]); | ||
* accel = await Accel.init(theChuck); // Initialize Accel | ||
*/ | ||
static async init(theChuck, enableAccel = true) { | ||
const accel = new Accel(theChuck); | ||
// Add Accel and AccelMsg classes to ChucK VM | ||
await accel.theChuck.runCode(AccelMsg_ck); | ||
await accel.theChuck.runCode(Accel_ck); | ||
// Enable mouse and keyboard | ||
/* | ||
if (enableAccel) { | ||
// If iOS, request permission | ||
if (typeof (DeviceOrientationEvent as any).requestPermission === 'function') { | ||
const permission = await (DeviceOrientationEvent as any).requestPermission(); | ||
if (permission === 'granted') { | ||
accel.enableAccel(); | ||
} else { | ||
console.log("Accelscope permission denied."); | ||
} | ||
} else { | ||
// just try to enable | ||
accel.enableAccel(); | ||
} | ||
} | ||
*/ | ||
accel.enableAccel(); | ||
return accel; | ||
} | ||
/** | ||
* @internal | ||
* Check if accel is active | ||
*/ | ||
async accelActive() { | ||
const x = await this.theChuck.getInt("_accelActive"); | ||
this._accelActive = x == 1; | ||
} | ||
/** | ||
* Enable Javascript event (DeviceMotionEvent) listeners for Accel | ||
* @example | ||
* ```ts | ||
* // If accel is not yet enabled | ||
* accel.enableAccel(); | ||
* ``` | ||
*/ | ||
enableAccel() { | ||
// consider using "deviceorientationabsolute" | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Window/deviceorientationabsolute_event | ||
window.addEventListener("devicemotion", this.boundHandleMotion); | ||
} | ||
/** | ||
* Disable Javascript event (DeviceMotionEvent) listeners for Accel | ||
* @example | ||
* ```ts | ||
* // If accel is enabled | ||
* accel.disableAccel(); | ||
* ``` | ||
*/ | ||
disableAccel() { | ||
window.removeEventListener("devicemotion", this.boundHandleMotion); | ||
} | ||
//----------------------------------------- | ||
// JAVASCRIPT HID EVENT HANDLERS | ||
//----------------------------------------- | ||
/** @internal */ | ||
handleMotion(event) { | ||
this.accelActive(); | ||
if (this._accelActive) { | ||
if (event.acceleration != null) { | ||
this.theChuck.setFloat("_accelX", event.acceleration.x ? event.acceleration.x : 0.0); | ||
this.theChuck.setFloat("_accelY", event.acceleration.y ? event.acceleration.y : 0.0); | ||
this.theChuck.setFloat("_accelZ", event.acceleration.z ? event.acceleration.z : 0.0); | ||
this.theChuck.broadcastEvent("_accelReading"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.