Skip to content

Commit 15fb26d

Browse files
author
Lauren McCarthy
committed
adding support for different device motion events across diff browsers
1 parent 1cd4202 commit 15fb26d

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

lib/p5.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.js v0.4.2 March 23, 2015 */
1+
/*! p5.js v0.4.2 March 28, 2015 */
22
(function (root, factory) {
33
if (typeof define === 'function' && define.amd)
44
define('p5', [], function () { return (root.returnExportsGlobal = factory());});
@@ -134,9 +134,15 @@ amdclean['core'] = function (require, shim, constants) {
134134
'touchmove': null,
135135
'touchend': null,
136136
'resize': null,
137-
'blur': null,
138-
'devicemotion': null
137+
'blur': null
139138
};
139+
if (window.DeviceOrientationEvent) {
140+
this._events.deviceorientation = null;
141+
} else if (window.DeviceMotionEvent) {
142+
this._events.devicemotion = null;
143+
} else {
144+
this._events.MozOrientation = null;
145+
}
140146
this._loadingScreenId = 'p5_loading';
141147
this._start = function () {
142148
if (this._userNode) {
@@ -3432,10 +3438,25 @@ amdclean['inputacceleration'] = function (require, core) {
34323438
};
34333439
var old_max_axis = '';
34343440
var new_max_axis = '';
3441+
p5.prototype.ondeviceorientation = function (e) {
3442+
this._setProperty('accelerationX', e.beta);
3443+
this._setProperty('accelerationY', e.gamma);
3444+
this._setProperty('accelerationZ', e.alpha);
3445+
this._handleMotion();
3446+
};
34353447
p5.prototype.ondevicemotion = function (e) {
3436-
this._setProperty('accelerationX', e.accelerationIncludingGravity.x);
3437-
this._setProperty('accelerationY', e.accelerationIncludingGravity.y);
3438-
this._setProperty('accelerationZ', e.accelerationIncludingGravity.z);
3448+
this._setProperty('accelerationX', e.acceleration.x * 2);
3449+
this._setProperty('accelerationY', e.acceleration.y * 2);
3450+
this._setProperty('accelerationZ', e.acceleration.z * 2);
3451+
this._handleMotion();
3452+
};
3453+
p5.prototype.onMozOrientation = function (e) {
3454+
this._setProperty('accelerationX', e.x);
3455+
this._setProperty('accelerationY', e.y);
3456+
this._setProperty('accelerationZ', e.z);
3457+
this._handleMotion();
3458+
};
3459+
p5.prototype._handleMotion = function () {
34393460
if (window.orientation === 90 || window.orientation === -90) {
34403461
this._setProperty('deviceOrientation', 'landscape');
34413462
} else if (window.orientation === 0) {

lib/p5.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/core.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,17 @@ define(function (require) {
174174
'touchmove': null,
175175
'touchend': null,
176176
'resize': null,
177-
'blur': null,
178-
'devicemotion': null
177+
'blur': null
179178
};
179+
180+
if (window.DeviceOrientationEvent) {
181+
this._events.deviceorientation = null;
182+
} else if (window.DeviceMotionEvent) {
183+
this._events.devicemotion = null;
184+
} else {
185+
this._events.MozOrientation = null;
186+
}
187+
180188
this._loadingScreenId = 'p5_loading';
181189

182190
this._start = function () {

src/input/acceleration.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150,53 +150,57 @@ define(function (require){
150150
* </code>
151151
* </div>
152152
*/
153+
p5.prototype.ondeviceorientation = function (e) {
154+
this._setProperty('accelerationX', e.beta);
155+
this._setProperty('accelerationY', e.gamma);
156+
this._setProperty('accelerationZ', e.alpha);
157+
this._handleMotion();
158+
};
153159
p5.prototype.ondevicemotion = function (e) {
154-
this._setProperty('accelerationX', e.accelerationIncludingGravity.x);
155-
this._setProperty('accelerationY', e.accelerationIncludingGravity.y);
156-
this._setProperty('accelerationZ', e.accelerationIncludingGravity.z);
157-
158-
//sets orientation property
159-
//device is horizontal
160-
if(window.orientation === 90 || window.orientation === -90){
160+
this._setProperty('accelerationX', e.acceleration.x * 2);
161+
this._setProperty('accelerationY', e.acceleration.y * 2);
162+
this._setProperty('accelerationZ', e.acceleration.z * 2);
163+
this._handleMotion();
164+
};
165+
p5.prototype.onMozOrientation = function (e) {
166+
this._setProperty('accelerationX', e.x);
167+
this._setProperty('accelerationY', e.y);
168+
this._setProperty('accelerationZ', e.z);
169+
this._handleMotion();
170+
};
171+
p5.prototype._handleMotion = function() {
172+
if (window.orientation === 90 || window.orientation === -90) {
161173
this._setProperty('deviceOrientation', 'landscape');
162-
}
163-
else if (window.orientation === 0){ //device is vertical
174+
} else if (window.orientation === 0) {
164175
this._setProperty('deviceOrientation', 'portrait');
165-
}
166-
else if (window.orientation === undefined){
167-
//In case the device doesn't support this function
176+
} else if (window.orientation === undefined) {
168177
this._setProperty('deviceOrientation', 'undefined');
169178
}
170-
171179
var onDeviceMove = this.onDeviceMove || window.onDeviceMove;
172180
if (typeof onDeviceMove === 'function') {
173-
if(Math.abs(this.accelerationX - this.pAccelerationX) > move_threshold ||
174-
Math.abs(this.accelerationY - this.pAccelerationY) > move_threshold ||
175-
Math.abs(this.accelerationZ - this.pAccelerationZ) > move_threshold ){
181+
if (Math.abs(this.accelerationX - this.pAccelerationX) > move_threshold ||
182+
Math.abs(this.accelerationY - this.pAccelerationY) > move_threshold ||
183+
Math.abs(this.accelerationZ - this.pAccelerationZ) > move_threshold) {
176184
onDeviceMove();
177185
}
178186
}
179-
180187
var onDeviceTurn = this.onDeviceTurn || window.onDeviceTurn;
181188
if (typeof onDeviceTurn === 'function') {
182-
//set current_max_axis
183189
var max_val = 0;
184-
if(Math.abs(this.accelerationX) > max_val){
190+
if (Math.abs(this.accelerationX) > max_val) {
185191
max_val = this.accelerationX;
186192
new_max_axis = 'x';
187193
}
188-
if(Math.abs(this.accelerationY) > max_val){
194+
if (Math.abs(this.accelerationY) > max_val) {
189195
max_val = this.accelerationY;
190196
new_max_axis = 'y';
191197
}
192-
if(Math.abs(this.accelerationZ) > max_val){
193-
new_max_axis = 'z'; //max_val is now irrelevant
198+
if (Math.abs(this.accelerationZ) > max_val) {
199+
new_max_axis = 'z';
194200
}
195-
196-
if(old_max_axis !== '' && old_max_axis !== new_max_axis){
201+
if (old_max_axis !== '' && old_max_axis !== new_max_axis) {
197202
onDeviceTurn(new_max_axis);
198203
}
199-
200204
old_max_axis = new_max_axis;
201205
}
202206
};

0 commit comments

Comments
 (0)