mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
Device motion
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
import {IonicView, DeviceMotion} from 'ionic/ionic';
|
||||
|
||||
|
||||
@IonicView({
|
||||
@ -10,9 +10,15 @@ import {IonicView} from 'ionic/ionic';
|
||||
<ion-title>Device Motion</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<div *ng-if="accel">{{accel.x}} {{accel.y}} {{accel.z}}</div>
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class DeviceMotionPage {
|
||||
constructor() {
|
||||
DeviceMotion.watchAcceleration().source.subscribe((accel) => {
|
||||
this.accel = accel.acceleration;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,21 +13,20 @@ import {NativePlugin} from '../plugin';
|
||||
export class DeviceMotion {
|
||||
static _wrap(result) {
|
||||
// Mimic the DeviceMotionEvent
|
||||
return {
|
||||
acceleration: result,
|
||||
accelerationIncludingGravity: result,
|
||||
return util.extend({
|
||||
acceleration: result, // result will be x/y/z accel
|
||||
accelerationIncludingGravity: result, //TODO: I know this isn't correct but not sure how to normalize from native plugin
|
||||
rotationRate: 0,
|
||||
interval: 0,
|
||||
native: true
|
||||
}
|
||||
}, result);
|
||||
}
|
||||
|
||||
static getCurrentAcceleration() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
|
||||
var fnCb = function fnCb(eventData) {
|
||||
console.log('Event', eventData);
|
||||
resolve(eventData);
|
||||
resolve(DeviceMotion._wrap(eventData));
|
||||
window.removeEventListener('devicemotion', fnCb);
|
||||
}
|
||||
window.addEventListener('devicemotion', fnCb);
|
||||
@ -52,8 +51,7 @@ export class DeviceMotion {
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
var fnCb = function fnCb(eventData) {
|
||||
console.log(eventData);
|
||||
observer.onNext(eventData);
|
||||
observer.onNext(DeviceMotion._wrap(eventData));
|
||||
};
|
||||
|
||||
window.addEventListener('devicemotion', fnCb);
|
||||
@ -73,7 +71,7 @@ export class DeviceMotion {
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
watchID = navigator.accelerometer.watchAcceleration(function (result) {
|
||||
observer.onNext(result);
|
||||
observer.onNext(DeviceMotion._wrap(result));
|
||||
}, function (err) {
|
||||
observer.onError(err, observer);
|
||||
}, options);
|
||||
|
Reference in New Issue
Block a user