-
Notifications
You must be signed in to change notification settings - Fork 71
/
compass.html
81 lines (56 loc) · 2.72 KB
/
compass.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>FULLTILT compass demo</title>
<!-- DEVICEORIENTATION EMULATOR DETECTION + BOOTSTRAP -->
<script src="https://richtr.github.io/doe/doe.js"></script>
<script src="../dist/fulltilt.min.js" type="text/javascript"></script>
</head>
<body>
<h1>FULLTILT compass demo</h1>
<canvas id="arrow"></canvas>
<script>
var arrowWidth = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight;
var halfArrowWidth = arrowWidth / 2;
var arrow = document.getElementById('arrow');
var ctx = arrow.getContext('2d');
// Set initial canvas width/height
arrow.width = arrowWidth;
arrow.height = arrowWidth;
// Start the FULLTILT DeviceOrientation listeners and update true north
// bearing indicator whenever a new deviceorientation event is fired.
var promise = FULLTILT.getDeviceOrientation({'type': 'world'});
promise.then(function(orientationControl) {
orientationControl.listen(function() {
// Get latest screen-adjusted deviceorientation data
var screenAdjustedEvent = orientationControl.getScreenAdjustedEuler();
ctx.clearRect(0,0,arrowWidth,arrowWidth);
// Convert true north heading to radians
var heading = screenAdjustedEvent.alpha * Math.PI / 180;
var x1 = halfArrowWidth + Math.round(halfArrowWidth * Math.sin(heading));
var y1 = halfArrowWidth - Math.round(halfArrowWidth * Math.cos(heading));
var x2 = halfArrowWidth + Math.round(10.0 * Math.sin(heading - Math.PI/2));
var y2 = halfArrowWidth - Math.round(10.0 * Math.cos(heading - Math.PI/2));
var x3 = halfArrowWidth + Math.round(10.0 * Math.sin(heading + Math.PI/2));
var y3 = halfArrowWidth - Math.round(10.0 * Math.cos(heading + Math.PI/2));
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.lineTo(x3,y3);
ctx.closePath();
ctx.fill();
});
});
window.addEventListener('resize', function() {
arrowWidth = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight
halfArrowWidth = arrowWidth / 2;
arrow.width = arrowWidth;
arrow.height = arrowWidth;
}, false);
</script>
<!-- Github Banner -->
<a href="https://github.com/richtr/Full-Tilt"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
</body>
</html>