compassjs 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +2 -0
- data/LICENSE +165 -0
- data/README.md +147 -0
- data/lib/assets/javascripts/compass.js +367 -0
- data/lib/compassjs.rb +8 -0
- metadata +70 -0
data/ChangeLog
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
# Compass.js
|
2
|
+
|
3
|
+
Compass.js allows you to get compass heading in JavaScript.
|
4
|
+
Today we haven’t any standard way to get compass data,
|
5
|
+
but there are two proprietary APIs and one hack:
|
6
|
+
|
7
|
+
* [PhoneGap has] `navigator.compass` API.
|
8
|
+
* iOS [Safari adds] `webkitCompassHeading` property to `deviceorientation` event.
|
9
|
+
* We can enable GPS and ask user to go forward. GPS will send current heading,
|
10
|
+
so we can calculate difference between real North and zero in
|
11
|
+
`deviceorientation` event. Next we use this difference to get compass heading
|
12
|
+
only by device orientation.
|
13
|
+
|
14
|
+
This library hides all this magic and APIs from you, autodetects available
|
15
|
+
way and provides clean and simple API for your geolocation web app.
|
16
|
+
|
17
|
+
Sponsored by [Evil Martians].
|
18
|
+
|
19
|
+
[PhoneGap has]: http://docs.phonegap.com/phonegap_compass_compass.md.html
|
20
|
+
[Safari adds]: http://developer.apple.com/library/safari/#documentation/SafariDOMAdditions/Reference/DeviceOrientationEventClassRef/DeviceOrientationEvent/DeviceOrientationEvent.html
|
21
|
+
[Evil Martians]: http://evilmartians.com/
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
|
26
|
+
Hide compass for desktop users (without compass, GPS and accelerometers):
|
27
|
+
|
28
|
+
```js
|
29
|
+
Compass.noSupport(function () {
|
30
|
+
$('.compass').hide();
|
31
|
+
});
|
32
|
+
```
|
33
|
+
|
34
|
+
Show instructions for Android users:
|
35
|
+
|
36
|
+
```js
|
37
|
+
Compass.needGPS(function () {
|
38
|
+
$('.go-outside-message').show(); // Step 1: we need GPS signal
|
39
|
+
}).needMove(function () {
|
40
|
+
$('.go-outside-message').hide()
|
41
|
+
$('.move-and-hold-ahead-message').show(); // Step 2: user must go forward
|
42
|
+
}).init(function () {
|
43
|
+
$('.move-and-hold-ahead-message').hide(); // GPS hack is enabled
|
44
|
+
});
|
45
|
+
```
|
46
|
+
|
47
|
+
Add compass heading listener:
|
48
|
+
|
49
|
+
```js
|
50
|
+
Compass.watch(function (heading) {
|
51
|
+
$('.degrees').text(heading);
|
52
|
+
$('.compass').css({ transform: 'rotate(' + (-heading) + 'deg)' });
|
53
|
+
});
|
54
|
+
```
|
55
|
+
|
56
|
+
### Method Name
|
57
|
+
|
58
|
+
Library will detect method asynchronously, so you can’t just read
|
59
|
+
`Compass.method`, because it can be empty yet. It will be better to
|
60
|
+
use `Compass.init` method:
|
61
|
+
|
62
|
+
```js
|
63
|
+
Compass.init(function (method) {
|
64
|
+
console.log('Compass heading by ' + method);
|
65
|
+
});
|
66
|
+
```
|
67
|
+
|
68
|
+
If library is already initialized, callback will be executed instantly,
|
69
|
+
without reinitialization.
|
70
|
+
|
71
|
+
### Unwatch
|
72
|
+
|
73
|
+
You can remove compass listener by `Compass.unwatch` method:
|
74
|
+
|
75
|
+
```js
|
76
|
+
var watchID = Compass.watch(function (heading) {
|
77
|
+
$('.degrees').text(heading);
|
78
|
+
});
|
79
|
+
|
80
|
+
Compass.unwatch(watchID);
|
81
|
+
```
|
82
|
+
|
83
|
+
## Installing
|
84
|
+
|
85
|
+
### Ruby on Rails
|
86
|
+
|
87
|
+
For Ruby on Rails you can use gem for Assets Pipeline.
|
88
|
+
|
89
|
+
1. Add `compassjs` gem to `Gemfile`:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
gem "compassjs"
|
93
|
+
```
|
94
|
+
|
95
|
+
2. Install gems:
|
96
|
+
|
97
|
+
```sh
|
98
|
+
bundle install
|
99
|
+
```
|
100
|
+
|
101
|
+
3. Include Pages.js to your `application.js.coffee`:
|
102
|
+
|
103
|
+
```coffee
|
104
|
+
#= require compass
|
105
|
+
```
|
106
|
+
|
107
|
+
### Others
|
108
|
+
|
109
|
+
If you don’t use any assets packaging manager (it’s very bad idea), you can use
|
110
|
+
already minified version of the library.
|
111
|
+
|
112
|
+
Take it from: [github.com/ai/compass.js/downloads].
|
113
|
+
|
114
|
+
[github.com/ai/compass.js/downloads]: https://github.com/ai/compass.js/downloads
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
1. To run tests you need node.js and npm. For example, in Ubuntu run:
|
119
|
+
|
120
|
+
```sh
|
121
|
+
sudo apt-get install nodejs npm
|
122
|
+
```
|
123
|
+
|
124
|
+
2. Next install npm dependencies:
|
125
|
+
|
126
|
+
```sh
|
127
|
+
npm install
|
128
|
+
```
|
129
|
+
|
130
|
+
3. Run all tests:
|
131
|
+
|
132
|
+
```sh
|
133
|
+
./node_modules/.bin/cake test
|
134
|
+
```
|
135
|
+
|
136
|
+
4. Run test server:
|
137
|
+
|
138
|
+
```sh
|
139
|
+
./node_modules/.bin/cake server
|
140
|
+
```
|
141
|
+
|
142
|
+
5. Open tests in browser: [localhost:8000].
|
143
|
+
6. Also you can see real usage example in integration test:
|
144
|
+
[localhost:8000/integration].
|
145
|
+
|
146
|
+
[localhost:8000]: http://localhost:8000
|
147
|
+
[localhost:8000/integration]: http://localhost:8000/integration
|
@@ -0,0 +1,367 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2012 Andrey “A.I.” Sitnik <andrey@sitnik.ru>,
|
3
|
+
* sponsored by Evil Martians.
|
4
|
+
*
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
6
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
* the Free Software Foundation, either version 3 of the License, or
|
8
|
+
* (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU Lesser General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU Lesser General Public License
|
16
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
*/
|
18
|
+
|
19
|
+
;(function(undefined) {
|
20
|
+
"use strict";
|
21
|
+
|
22
|
+
// Shortcut to check, that `variable` is not `undefined` or `null`.
|
23
|
+
var defined = function (variable) {
|
24
|
+
return (variable != null || variable != undefined);
|
25
|
+
};
|
26
|
+
|
27
|
+
// Fire `type` callbacks with `args`.
|
28
|
+
var fire = function (type, args) {
|
29
|
+
var callbacks = self._callbacks[type];
|
30
|
+
for (var i = 0; i < callbacks.length; i++) {
|
31
|
+
callbacks[i].apply(window, args);
|
32
|
+
}
|
33
|
+
};
|
34
|
+
|
35
|
+
// Calculate average value for last 5 `array` items;
|
36
|
+
var average5 = function (array) {
|
37
|
+
var sum = 0;
|
38
|
+
for (var i = array.length - 1; i > array.length - 6; i--) {
|
39
|
+
sum += array[i];
|
40
|
+
}
|
41
|
+
return sum / 5;
|
42
|
+
};
|
43
|
+
|
44
|
+
// Compass.js allow you to get compass heading in JavaScript.
|
45
|
+
// We can get compass data by two proprietary APIs and one hack:
|
46
|
+
// * PhoneGap have `navigator.compass` API.
|
47
|
+
// * iOS Safari add `webkitCompassHeading` to `deviceorientation` event.
|
48
|
+
// * We can enable GPS and ask user to go forward. GPS will send current heading,
|
49
|
+
// so we can calculate difference between real North and zero in
|
50
|
+
// `deviceorientation` event. Next we use this difference to get compass heading
|
51
|
+
// only by device orientation.
|
52
|
+
//
|
53
|
+
// Hide compass, when there isn’t any method:
|
54
|
+
//
|
55
|
+
// Compass.noSupport(function () {
|
56
|
+
// $('.compass').hide();
|
57
|
+
// });
|
58
|
+
//
|
59
|
+
// Show instructions for GPS hack:
|
60
|
+
//
|
61
|
+
// Compass.needGPS(function () {
|
62
|
+
// $('.go-outside-message').show();
|
63
|
+
// }).needMove(function () {
|
64
|
+
// $('.go-outside-message').hide()
|
65
|
+
// $('.move-and-hold-ahead-message').show();
|
66
|
+
// }).init(function () {
|
67
|
+
// $('.move-and-hold-ahead-message').hide();
|
68
|
+
// });
|
69
|
+
var self = window.Compass = {
|
70
|
+
|
71
|
+
// Name of method to get compass heading. It will have value only after
|
72
|
+
// library initialization from `init` method. So better way to get
|
73
|
+
// method name is to use `init`:
|
74
|
+
//
|
75
|
+
// Compass.init(function (method) {
|
76
|
+
// console.log('Compass by ' + method);
|
77
|
+
// });
|
78
|
+
//
|
79
|
+
// Available methods:
|
80
|
+
// * `phonegap` take from PhoneGap’s `navigator.compass`.
|
81
|
+
// * `webkitOrientation` take from iPhone’s proprietary
|
82
|
+
// `webkitCompassHeading` proprerty in `DeviceOrientationEvent`.
|
83
|
+
// * `orientationAndGPS` take from device orientation with GPS hack.
|
84
|
+
//
|
85
|
+
// If browser hasn’t access to compass, `method` will be `false`.
|
86
|
+
method: undefined,
|
87
|
+
|
88
|
+
// Watch for compass heading changes and execute `callback` with degrees
|
89
|
+
// relative to magnetic north (from 0 to 360).
|
90
|
+
//
|
91
|
+
// Method return watcher ID to use it in `unwatch`.
|
92
|
+
//
|
93
|
+
// var watchID = Compass.watch(function (heading) {
|
94
|
+
// $('.degrees').text(heading);
|
95
|
+
// // Don’t forget to change degree sign, when rotate compass.
|
96
|
+
// $('.compass').css({ transform: 'rotate(' + (-heading) + 'deg)' });
|
97
|
+
// });
|
98
|
+
//
|
99
|
+
// someApp.close(function () {
|
100
|
+
// Compass.unwatch(watchID);
|
101
|
+
// });
|
102
|
+
watch: function (callback) {
|
103
|
+
var id = ++self._lastId;
|
104
|
+
|
105
|
+
self.init(function (method) {
|
106
|
+
|
107
|
+
if ( method == 'phonegap' ) {
|
108
|
+
self._watchers[id] = self._nav.compass.watchHeading(callback);
|
109
|
+
|
110
|
+
} else if ( method == 'webkitOrientation' ) {
|
111
|
+
var watcher = function (e) {
|
112
|
+
callback(e.webkitCompassHeading);
|
113
|
+
};
|
114
|
+
self._win.addEventListener('deviceorientation', watcher);
|
115
|
+
self._watchers[id] = watcher;
|
116
|
+
|
117
|
+
} else if ( method == 'orientationAndGPS' ) {
|
118
|
+
var degrees;
|
119
|
+
var watcher = function (e) {
|
120
|
+
degrees = -e.alpha + self._gpsDiff;
|
121
|
+
if ( degrees < 0 ) {
|
122
|
+
degrees += 360;
|
123
|
+
} else if ( degrees > 360 ) {
|
124
|
+
degrees -= 360;
|
125
|
+
}
|
126
|
+
callback(degrees);
|
127
|
+
};
|
128
|
+
self._win.addEventListener('deviceorientation', watcher);
|
129
|
+
self._watchers[id] = watcher;
|
130
|
+
|
131
|
+
}
|
132
|
+
});
|
133
|
+
|
134
|
+
return id;
|
135
|
+
},
|
136
|
+
|
137
|
+
// Remove watcher by watcher ID from `watch`.
|
138
|
+
//
|
139
|
+
// Compass.unwatch(watchID)
|
140
|
+
unwatch: function (id) {
|
141
|
+
self.init(function (m) {
|
142
|
+
|
143
|
+
if ( m == 'phonegap' ) {
|
144
|
+
self._nav.compass.clearWatch(self._watchers[id]);
|
145
|
+
|
146
|
+
} else if ( m == 'webkitOrientation' || m == 'orientationAndGPS' ) {
|
147
|
+
self._win.removeEventListener('deviceorientation', self._watchers[id]);
|
148
|
+
|
149
|
+
}
|
150
|
+
delete self._watchers[id];
|
151
|
+
});
|
152
|
+
return self;
|
153
|
+
},
|
154
|
+
|
155
|
+
// Execute `callback`, when GPS hack activated to detect difference between
|
156
|
+
// device orientation and real North from GPS.
|
157
|
+
//
|
158
|
+
// You need to show to user some message, that he must go outside to be able
|
159
|
+
// to receive GPS signal.
|
160
|
+
//
|
161
|
+
// Callback must be set before `init` or `watch` executing.
|
162
|
+
//
|
163
|
+
// Compass.needGPS(function () {
|
164
|
+
// $('.go-outside-message').show();
|
165
|
+
// });
|
166
|
+
//
|
167
|
+
// Don’t forget to hide message by `needMove` callback in second step.
|
168
|
+
needGPS: function (callback) {
|
169
|
+
self._callbacks.needGPS.push(callback);
|
170
|
+
return self;
|
171
|
+
},
|
172
|
+
|
173
|
+
// Execute `callback` on second GPS hack step, when library has GPS signal,
|
174
|
+
// but user must move and hold the device straight ahead. Library will use
|
175
|
+
// `heading` from GPS movement tracking to detect difference between
|
176
|
+
// device orientation and real North.
|
177
|
+
//
|
178
|
+
// Callback must be set before `init` or `watch` executing.
|
179
|
+
//
|
180
|
+
// Compass.needMove(function () {
|
181
|
+
// $('.go-outside-message').hide()
|
182
|
+
// $('.move-and-hold-ahead-message').show();
|
183
|
+
// });
|
184
|
+
//
|
185
|
+
// Don’t forget to hide message in `init` callback:
|
186
|
+
//
|
187
|
+
// Compass.init(function () {
|
188
|
+
// $('.move-and-hold-ahead-message').hide();
|
189
|
+
// });
|
190
|
+
needMove: function (callback) {
|
191
|
+
self._callbacks.needMove.push(callback);
|
192
|
+
return self;
|
193
|
+
},
|
194
|
+
|
195
|
+
// Execute `callback` if browser hasn’t any way to get compass heading.
|
196
|
+
//
|
197
|
+
// Compass.noSupport(function () {
|
198
|
+
// $('.compass').hide();
|
199
|
+
// });
|
200
|
+
//
|
201
|
+
// On Firefox detecting can take about 0.5 second. So, it will be better
|
202
|
+
// to show compass in `init`, than to hide it in `noSupport`.
|
203
|
+
noSupport: function (callback) {
|
204
|
+
if ( self.method === false ) {
|
205
|
+
callback();
|
206
|
+
} else if ( !defined(self.method) ) {
|
207
|
+
self._callbacks.noSupport.push(callback);
|
208
|
+
}
|
209
|
+
return self;
|
210
|
+
},
|
211
|
+
|
212
|
+
// Detect compass method and execute `callback`, when library will be
|
213
|
+
// initialized. Callback will get method name (or `false` if library can’t
|
214
|
+
// detect compass) in first argument.
|
215
|
+
//
|
216
|
+
// It is best way to check `method` property.
|
217
|
+
//
|
218
|
+
// Compass.init(function (method) {
|
219
|
+
// console.log('Compass by ' + method);
|
220
|
+
// });
|
221
|
+
init: function (callback) {
|
222
|
+
if ( defined(self.method) ) {
|
223
|
+
callback(self.method);
|
224
|
+
return;
|
225
|
+
}
|
226
|
+
self._callbacks.init.push(callback);
|
227
|
+
|
228
|
+
if ( self._initing ) {
|
229
|
+
return;
|
230
|
+
}
|
231
|
+
self._initing = true;
|
232
|
+
|
233
|
+
if ( self._nav.compass ) {
|
234
|
+
self._start('phonegap');
|
235
|
+
|
236
|
+
} else if ( self._win.DeviceOrientationEvent ) {
|
237
|
+
self._win.addEventListener('deviceorientation', self._checkEvent);
|
238
|
+
setTimeout(function () {
|
239
|
+
if ( !self._checked ) {
|
240
|
+
self._start(false)
|
241
|
+
}
|
242
|
+
}, 500);
|
243
|
+
|
244
|
+
} else {
|
245
|
+
self._start(false);
|
246
|
+
}
|
247
|
+
return self;
|
248
|
+
},
|
249
|
+
|
250
|
+
// Last watch ID.
|
251
|
+
_lastId: 0,
|
252
|
+
|
253
|
+
// Hash of internal ID to watcher to use it in `unwatch`.
|
254
|
+
_watchers: { },
|
255
|
+
|
256
|
+
// Window object for testing.
|
257
|
+
_win: window,
|
258
|
+
|
259
|
+
// Navigator object for testing.
|
260
|
+
_nav: navigator,
|
261
|
+
|
262
|
+
// List of callbacks.
|
263
|
+
_callbacks: {
|
264
|
+
|
265
|
+
// Callbacks from `init` method.
|
266
|
+
init: [],
|
267
|
+
|
268
|
+
// Callbacks from `noSupport` method.
|
269
|
+
noSupport: [],
|
270
|
+
|
271
|
+
// Callbacks from `needGPS` method.
|
272
|
+
needGPS: [],
|
273
|
+
|
274
|
+
// Callbacks from `needMove` method.
|
275
|
+
needMove: []
|
276
|
+
|
277
|
+
},
|
278
|
+
|
279
|
+
// Is library now try to detect compass method.
|
280
|
+
_initing: false,
|
281
|
+
|
282
|
+
// Difference between `alpha` orientation and real North from GPS.
|
283
|
+
_gpsDiff: undefined,
|
284
|
+
|
285
|
+
// Finish library initialization and use `method` to get compass heading.
|
286
|
+
_start: function (method) {
|
287
|
+
self.method = method;
|
288
|
+
self._initing = false;
|
289
|
+
|
290
|
+
fire('init', [method]);
|
291
|
+
self._callbacks.init = [];
|
292
|
+
|
293
|
+
if ( method === false ) {
|
294
|
+
fire('noSupport', []);
|
295
|
+
}
|
296
|
+
self._callbacks.noSupport = [];
|
297
|
+
},
|
298
|
+
|
299
|
+
// Tell, that `DeviceOrientationEvent` was fired.
|
300
|
+
_checked: undefined,
|
301
|
+
|
302
|
+
// Check `DeviceOrientationEvent` to detect compass method.
|
303
|
+
_checkEvent: function (e) {
|
304
|
+
self._checked = true;
|
305
|
+
|
306
|
+
if ( defined(e.webkitCompassHeading) ) {
|
307
|
+
self._start('webkitOrientation');
|
308
|
+
|
309
|
+
} else if ( defined(e.alpha) && self._nav.geolocation ) {
|
310
|
+
self._gpsHack();
|
311
|
+
|
312
|
+
} else {
|
313
|
+
self._start(false);
|
314
|
+
}
|
315
|
+
|
316
|
+
self._win.removeEventListener('deviceorientation', self._checkEvent);
|
317
|
+
},
|
318
|
+
|
319
|
+
// Use GPS to detect difference between `alpha` orientation and real North.
|
320
|
+
_gpsHack: function () {
|
321
|
+
var first = true;
|
322
|
+
var alphas = [];
|
323
|
+
var headings = [];
|
324
|
+
|
325
|
+
fire('needGPS');
|
326
|
+
|
327
|
+
var saveAlpha = function (e) {
|
328
|
+
alphas.push(e.alpha);
|
329
|
+
}
|
330
|
+
self._win.addEventListener('deviceorientation', saveAlpha);
|
331
|
+
|
332
|
+
var success = function (position) {
|
333
|
+
var coords = position.coords
|
334
|
+
if ( !defined(coords.heading) ) {
|
335
|
+
return; // Position not from GPS
|
336
|
+
}
|
337
|
+
|
338
|
+
if ( first ) {
|
339
|
+
first = false;
|
340
|
+
fire('needMove');
|
341
|
+
}
|
342
|
+
|
343
|
+
if ( coords.speed > 1 ) {
|
344
|
+
headings.push(coords.heading);
|
345
|
+
if ( headings.length >= 5 && alphas.length >= 5 ) {
|
346
|
+
self._win.removeEventListener('deviceorientation', saveAlpha);
|
347
|
+
self._nav.geolocation.clearWatch(watcher);
|
348
|
+
|
349
|
+
self._gpsDiff = average5(headings) + average5(alphas);
|
350
|
+
self._start('orientationAndGPS');
|
351
|
+
}
|
352
|
+
} else {
|
353
|
+
headings = [];
|
354
|
+
}
|
355
|
+
};
|
356
|
+
var error = function () {
|
357
|
+
self._win.removeEventListener('deviceorientation', saveAlpha);
|
358
|
+
self._start(false);
|
359
|
+
};
|
360
|
+
|
361
|
+
var watcher = self._nav.geolocation.
|
362
|
+
watchPosition(success, error, { enableHighAccuracy: true });
|
363
|
+
}
|
364
|
+
|
365
|
+
};
|
366
|
+
|
367
|
+
})();
|
data/lib/compassjs.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: compassjs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrey "A.I." Sitnik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
type: :runtime
|
16
|
+
name: sprockets
|
17
|
+
prerelease: false
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '2'
|
23
|
+
none: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '2'
|
29
|
+
none: false
|
30
|
+
description: Compass.js allow you to get compass heading in JavaScript by PhoneGap,
|
31
|
+
iOS API or GPS hack.
|
32
|
+
email:
|
33
|
+
- andrey@sitnik.ru
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files:
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- ChangeLog
|
40
|
+
files:
|
41
|
+
- lib/assets/javascripts/compass.js
|
42
|
+
- lib/compassjs.rb
|
43
|
+
- LICENSE
|
44
|
+
- README.md
|
45
|
+
- ChangeLog
|
46
|
+
homepage: https://github.com/ai/compass.js
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
none: false
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
none: false
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.23
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Compass.js allow you to get compass heading in JavaScript
|
70
|
+
test_files: []
|