Code Monkey home page Code Monkey logo

accelerometer's Introduction

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

accelerometer's People

Contributors

methodswithclass avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.