Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/methodswithclass/accelerometer
An engine that reads the accelerometer data from the device, calculates position, and applies it to a DOM element.
https://github.com/methodswithclass/accelerometer
acceleration accelerometer calculation dynamics gaming integration math motion numerical physics velocity
Last synced: 4 months ago
JSON representation
An engine that reads the accelerometer data from the device, calculates position, and applies it to a DOM element.
- Host: GitHub
- URL: https://github.com/methodswithclass/accelerometer
- Owner: methodswithclass
- Created: 2017-10-18T18:37:33.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-08-20T15:23:45.000Z (over 1 year ago)
- Last Synced: 2024-04-25T16:02:49.328Z (9 months ago)
- Topics: acceleration, accelerometer, calculation, dynamics, gaming, integration, math, motion, numerical, physics, velocity
- Language: JavaScript
- Homepage:
- Size: 566 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Accelerometer
This package takes acceleromter data from your device, calculates position, and applies it to a DOM element.
Applications inlcude gaming, user interface controls, and more.# Change log
Version 3.3.0: request device motion permission bug fix
# Usage
It numerically integrates the velocity and position, in real time, from the accelerometer data from the device.
`npm install @methodswithclass/accelerometer`
`import Accelerometer, { ValidStatus } from @methodswithclass/accelerometer`
one of these values is returned in a promise from accel.validate()
```
ValidStatus = {
valid: 'valid',
invalid: 'invalid',
unchecked: 'unchecked',
denied: 'denied',
unresponsive: 'unresponsive',
}
```parameters for numerical integration process and general motion behavior, defaults are shown
```
const params = {
interval: 2, //acceleromter interval in milliseconds
filterSize: 3, //sampling/smoothing size
mu: 0.1, //friction
damp: 0.4, //bounce dampening
gravity: true, //whether the object responds to tilting of device (true) or only change in position of device (false)
bounce: true, //whether the object bounces off walls (true), or sticks to the wall (false)
timeout: 10, // motion timeout with no activity, in seconds
xDir: 1, //horizonal flip
yDir: 1, //vertical flip
factor: 0.008, //sensitivity factor (speed of object)
}
```Accelerometer manages several instances by id, subsequent calls return original
```
const accel = Accelerometer({
id: "accel", // name of accelerometer instance
arena: "arenaId", // element id to space to move within
object: "objectId", //element id of object to be moved
params, //inject accelerometer parameters
overrideValidate: false // not all devices have accelerometers, this flag forces the validator to return 'valid', for local development
});accel.calibrate({ xDir, yDir, factor}); // this is run by default
accel.validate(); // returns a promise with the device validation status, requests permission if necessary (must prompt with a button to request permission)
accel.start(); //start updating position of DOM element based on accelerometer data
accel.stop(); // stop updating position, reset
accel.reset(); // returns object to initial position
```