Skip to content

Commit

Permalink
Fix FractionBounce accelerometer on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
llaske committed Oct 25, 2024
1 parent 7deebd7 commit 1515035
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
3 changes: 1 addition & 2 deletions activities/FractionBounce.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ let app = new Vue({
this.acceleration = {
x: acceleration.x,
y: acceleration.y,
z: acceleration.z,
type: acceleration.type
z: acceleration.z
};
this.accelerationUpdate();
},
Expand Down
34 changes: 25 additions & 9 deletions activities/FractionBounce.activity/js/components/SugarDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ Vue.component('sugar-device', {
template: `<div style="display: none">{{ watchId }}</div>`,
data: function () {
return {
watchId: null
watchId: null,
readyToWatch: false,
frequencyExpected: 0
}
},
created: function () {
var cordovaScript = document.createElement('script');
cordovaScript.setAttribute('type', 'text/javascript');
cordovaScript.setAttribute('src', '../../cordova.js');
document.head.appendChild(cordovaScript);
mounted: function () {
var vm = this;
requirejs(['domReady!'], function () {
document.addEventListener('deviceready', function() {
vm.readyToWatch = true;
if (vm.frequencyExpected > 0) {
vm.watchAcceleration(vm.frequencyExpected);
}
}, false);
});
},
methods: {
isMobile() {
Expand Down Expand Up @@ -85,13 +92,22 @@ Vue.component('sugar-device', {
accelerometer.start();
}
} else if (navigator.accelerometer) {
watchId = navigator.accelerometer.watchAcceleration(function () {
vm.accelerationCallback(accelerometer);
}, null, { frequency: frequency });
if (vm.readyToWatch) {
vm.watchId = navigator.accelerometer.watchAcceleration(function (accelerometer) {
vm.accelerationCallback(accelerometer);
}, null, { frequency: frequency });
} else {
vm.frequencyExpected = frequency;
}
} else {
vm.frequencyExpected = frequency;
}
},

accelerationCallback: function (acceleration) {
if (this.readyToWatch) {
this.readyToWatch = false;
}
this.$emit('acceleration-callback', acceleration);
},

Expand Down

0 comments on commit 1515035

Please sign in to comment.