summaryrefslogtreecommitdiff
path: root/site/public/assets/javascripts/mx/extensions/mx.movementsMobile.js
blob: 95b61d1b480560775fb3600e3c43480305ff92d2 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
MX.MobileMovements = function (cam) {

	var touching = true,
			moving = false,
			startTime = null,
	    v = 12,
			vr = Math.PI * 0.012,
			vx = vy = vz = 0;

	var directionLocked = false,
	    directionLockThreshold = 5

	var pos = { x: 0, y: viewHeight, z: 0, rotationX: 0, rotationY: 0 }

	var pointX, pointY, deltaX, deltaY, distX = 0, distY = 0, absDistX = 0, absDistY = 0, startTime

  var rotationX = 0, rotationY = 0, destRotationX = 0, destRotationY = 0
  var rotationSum = 0
  var rotationMedian = 0
  var orientationMax = 0
  var samples = 0
  var sampleThreshold = 120
  var lastAlpha
  
  var is_portrait

	var exports = {

		init: function () {
		  exports.orientationchange()
		  
			document.addEventListener("touchstart", exports.touchstart)
			document.addEventListener("touchmove", exports.touchmove)
			document.addEventListener("touchend", exports.touchend)
      window.addEventListener('orientationchange', exports.orientationchange)
      window.addEventListener("devicemotion", exports.devicemotion)
      window.addEventListener("deviceorientation", exports.deviceorientation)
    },
    touchstart: function(e){
      if (e.touches.length == 1) {
        touching = true
        
        startTime = Date.now()
        
        var point = event.touches[0]
        pointX = point.pageX
        pointY = point.pageY
        distX = distY = 0
        pos.x = cam.x
        pos.z = cam.z
        pos.rotationY = cam.rotationY
      }
    },
		touchmove: function(e){
      e.preventDefault()
      if (e.touches.length == 1) {

        var timestamp = Date.now()
        var point = event.touches[0]
        deltaX = point.pageX - pointX
        deltaY = point.pageY - pointY
        
        pointX = point.pageX
        pointY = point.pageY

        distX += deltaX
        distY += deltaY
        absDistX = abs(distX)
        absDistY = abs(distY)
      }
    },
	  touchend: function(e){
      e.preventDefault()
      if (e.touches.length == 0) {
        touching = directionLocked = false
        var timestamp = Date.now()
        var duration = startTime - timestamp
        if (duration < 300) {
        }
      }
    },
    orientationchange: function(e){
      is_portrait = window.innerWidth < window.innerHeight
      if (is_portrait) {
        lastAlpha = 0
      }
    },
    devicemotion: function(e) {
      if (! is_portrait) return;          
      var rotationBeta = e.rotationRate.alpha; // weird!
      rotationSum += rotationBeta;
      samples += 1;
    },
    deviceorientation: function (e) {
      if (! lastAlpha) { lastAlpha = e.alpha }
      is_portrait ? exports.portraitorientation(e) : exports.landscapeorientation(e)
    },
    portraitorientation: function(e) {
      // compass gives most accurate orientation in portrait mode
      var alpha, dx = 0, dy = 0

      if (e.webkitCompassHeading) {
        alpha = 180 - e.webkitCompassHeading;
      }
      else {
        alpha = 180 - e.alpha;
      }
      
      // android rotates in reverse
      if (is_android) {
        alpha = 360 - alpha
      }
      
      // use rotationRate to gauge if we've tilted the screen past vertical
      // for looking at ceiling
      if (e.beta > orientationMax) {
        orientationMax = e.beta
        rotationMedian = rotationSum
      }
      
      // this number was only going to 83 max.. not 90.. weird
      var beta = e.beta + 7;
      
      // if we've got enough motion data, we should be able to determine
      // if we've tilted backwards. otherwise, lock to the horizon.
      if (! is_android && samples > sampleThreshold) {
        dx = rotationSum > rotationMedian ? e.beta - 90 : 90 - e.beta
      }
      else {
        dx = 0
      }
      
      // avoid jumping around in a circle
      if (Math.abs(alpha - lastAlpha) < 100 || Math.abs(alpha - lastAlpha) > 300) {
        dy = alpha - lastAlpha
        lastAlpha = alpha
      }

      // avoid jumping around in a circle #2
      if (dy > 300) {
        dy -= 360
      } else if (dy < -300) {
        dy += 360
      }

      destRotationX = MX.toRad(dx)
      destRotationY += MX.toRad(dy)
    },
    
    landscapeorientation: function (e) {
      var dx, dy
      
      dx = e.gamma > 0 ? 90 - e.gamma : 90 + e.gamma
      dy = e.alpha - lastAlpha
      lastAlpha = e.alpha

      // avoid the sudden jump from 0 to -360
      if (dy > 300) {
        dy -= 360
      }
      else if (dy < -300) {
        dy += 360
      }

      destRotationX = dx > 45 ? 0 : MX.toRad(dx)
      destRotationY += MX.toRad(dy)
		},
		
		update: function () {
		  var drx, dry
		  
      dry = (destRotationY - rotationY) / 6
      drx = (destRotationX - rotationX) / 6
      rotationY += dry
      rotationX += drx
      cam.rotationY = pos.rotationY += dry
      cam.rotationX = pos.rotationX += drx
      
			if (distX || distY) {
				var oldDistY = absDistY, oldDistX = absDistX
				absDistY = avg(absDistY, 0, 5)
				var dy = (oldDistY - absDistY) * sign(distY) * 2

				absDistX = avg(absDistX, 0, 5)
				var dx = (oldDistX - absDistX) * sign(distX) * 2

				distY = sign(distY) * absDistY
				distX = sign(distX) * absDistX

				pos.x -= dy * Math.cos(pos.rotationY + Math.PI / 2)
				pos.z -= dy * Math.sin(pos.rotationY + Math.PI / 2)
				cam.rotationY = pos.rotationY += dx / (window.innerWidth) * Math.PI / 2

				app.tube("move", pos)
			}
		},
		
		lock: function(){ locked = true },
		unlock: function(){ locked = false },
		scale: function(n){ if (n) scale = n; return scale },
		resetScale: function(n){ scale = DEFAULT_SCALE },
		gravity: function(g){ return typeof g == "boolean" ? gravity = g : gravity },
		velocity: function(n){ v = clamp(n, 1, 50) },
		jumpVelocity: function(n){ jumpV = clamp(n, 1, 50) },
	}

  return exports
}


// function momentum (current, start, time, lowerMargin, wrapperSize, deceleration) {
// 	var distance = current - start,
// 		speed = Math.abs(distance) / time,
// 		destination,
// 		duration;
// 
// 	deceleration = deceleration === undefined ? 0.0006 : deceleration;
// 
// 	destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
// 	duration = speed / deceleration;
// 
// 	if ( destination < lowerMargin ) {
// 		destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
// 		distance = Math.abs(destination - current);
// 		duration = distance / speed;
// 	} else if ( destination > 0 ) {
// 		destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
// 		distance = Math.abs(current) + destination;
// 		duration = distance / speed;
// 	}
// 
// 	return {
// 		destination: Math.round(destination),
// 		duration: duration
// 	};
// }