forked from mahonnaise/cyclotron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcyclotron.js
More file actions
77 lines (66 loc) · 1.57 KB
/
cyclotron.js
File metadata and controls
77 lines (66 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var offset=10,gyro=false;
cyclotron = function (options) {
var touch=false;
var settings = $.extend({
dampingFactor: 1,
historySize: 5
}, options);
var container, sx, dx = 10, armed, tick, prev, h = [];
container = $('.cycle');
container.bind('mousedown touchstart', function (e) {
touch=true;
sx = e.pageX>0?e.pageX:e.originalEvent.touches[0].pageX - offset;
armed = true;
e.preventDefault();
// alert(e.originalEvent.touches[0].pageX);
});
container.bind('mousemove touchmove', function (e) {
var px;
if (armed) {
px = e.pageX>0?e.pageX:e.originalEvent.touches[0].pageX;
if (prev === undefined) {
prev = px;
}
offset = px - sx;
if (h.length > settings.historySize) {
h.shift();
}
h.push(prev - px);
container.css('background-position', offset);
prev = px;
}
});
container.bind('mouseleave mouseup touchend', function () {
touch=false;
if (armed) {
var i, len = h.length, v = h[len - 1];
for (i = 0; i < len; i++) {
v = (v * len + (h[i])) / (len + 1);
}
dx = v;
}
armed = false;
});
var alpha,beta,gamma;
window.ondeviceorientation = function(event) {
$('#consol').html(touch.toString());
if(!touch){
alpha = Math.round(event.alpha);
beta = Math.round(event.beta);
gamma = Math.round(event.gamma);
offset = event.gamma*30;
$('.cycle').css('background-position', offset);
gyro=true;
}
}
}
function tick() {
if(gyro)
return;
offset+=1;
$('.cycle').css('background-position', offset);
};
$(document).ready(function() {
cyclotron();
setInterval(tick, 24);
});