Skip to content

Commit

Permalink
plugin working and detecting shake event, still receiving a function …
Browse files Browse the repository at this point in the history
…as a callback needed
  • Loading branch information
GerManson committed Jun 22, 2011
1 parent 6e9991b commit 0814c80
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
2 changes: 2 additions & 0 deletions js/application.js
@@ -1,3 +1,5 @@
$(document).ready(function() {

$(this).gShake();

});
73 changes: 40 additions & 33 deletions lib/gShake.js
@@ -1,33 +1,33 @@
(function($) {

var settings = {
'sensibility': 20,
'timeout': 0
'sensibility': 20,
'timeout': 0
};

var coords = {
'x': null,
'y': null,
'z': null
'x': null,
'y': null,
'z': null
};

var lastTime = new Date();

var methods = {
init: function(options) {
alert("plugin started");
//alert(methods.test());
init: function(options) {
alert("plugin started");
//alert(methods.test());

return this.each(function() {
// If options exist, lets merge them
// with our default settings
if (options) {
$.extend(settings, options);
}
return this.each(function() {
// If options exist, lets merge them
// with our default settings
if (options) {
$.extend(settings, options);
}

if (('ondevicemotion' in window)) {
window.addEventListener('devicemotion', methods.devicemotion, false);
}
if (('ondevicemotion' in window)) {
window.addEventListener('devicemotion', methods.devicemotion, false);
}
});
},

Expand All @@ -36,31 +36,38 @@
},

stop: function() {

if (('ondevicemotion' in window)) {
window.removeEventListener('devicemotion', this, false);
}

lastTime = new Date();
coords.x = null;
coords.y = null;
coords.z = null;
},

devicemotion: function(e) {
var current = e.accelerationIncludingGravity;

if((this.lastX !== null) || (this.lastY !== null) || (this.lastZ !== null)) {
if((coords.x !== null) || (coords.y !== null) || (coords.z !== null)) {

var deltaX = Math.abs(this.lastX - current.x),
deltaY = Math.abs(this.lastY - current.y),
deltaZ = Math.abs(this.lastZ - current.z);
var deltaX = Math.abs(coords.x - current.x),
deltaY = Math.abs(coords.y - current.y),
deltaZ = Math.abs(coords.z - current.z);

if(((deltaX > this.threshold) && (deltaY > this.threshold)) ||
((deltaX > this.threshold) && (deltaZ > this.threshold)) ||
((deltaY > this.threshold) && (deltaZ > this.threshold))) {
if(((deltaX > settings.sensibility) && (deltaY > settings.sensibility)) ||
((deltaX > settings.sensibility) && (deltaZ > settings.sensibility)) ||
((deltaY > settings.sensibility) && (deltaZ > settings.sensibility))) {

//calculate time in milliseconds since last shake registered
var currentTime = new Date(),
timeDifference = currentTime.getTime() - this.lastTime.getTime();
//calculate time in milliseconds since last shake registered
var currentTime = new Date(),
timeDifference = currentTime.getTime() - lastTime.getTime();

if (timeDifference > 300) {
methods.shakeEventDidOcurr();
this.lastTime = new Date();
}
}
if (timeDifference > 300) {
methods.shakeEventDidOcurr();
lastTime = new Date();
}
}
}

coords.x = current.x;
Expand Down

0 comments on commit 0814c80

Please sign in to comment.