-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallinone.html
184 lines (164 loc) · 7.06 KB
/
allinone.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
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
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- basic.html -->
<title>webAPI.html</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>The Vibration API</h1>
<p>
Almost every existing mobile device includes some vibration hardware, which allows the device to shake to
provide feedback to the user. <br>
This Web-API provides control of this features to web apps and does nothing if the hardware does not support it.
<br>
Vibration is described as a pattern of on/off intervals, e.g. the 'Vibrate Pattern' Button is described like
this [300,100,300,100,300]:<br>
The device vibrates for 300ms than does nothing for 100ms and again.<br>
To acces this API use navigator.vibrate:<br>
<code>navigator.vibrate([300, 100, 300, 100, 300]);</code> <br>
Try it out below!
<br>
</p>
<button class="button" onclick="vibrate(1000)">Vibrate Single</button><br>
<button class="button" onclick="vibratePattern()">Vibrate Pattern</button>
<script>
function vibrate(ms) {
navigator.vibrate(ms);
}
function vibratePattern() {
navigator.vibrate([300, 100, 300, 100, 300]);
}
</script>
<br><br><br>
<h1>The Notification API</h1>
<p>
This API allows web apps to send notifications at system level and not only at website level. <br>
This way an application can notify the user, even when it is idle or in background. <br>
An typical use-case would be a chat or mail application, where you want to user get the message immediatly. <br>
To use this API you must check for the right permissions, they can be accessed via <br>
<code> Notification.permission </code> <br>
and can be <br>
<ul>
<li><b>default</b> - the user hasn't been asked yet, so no notifications</li>
<li><b>denied</b> - no notificaitons can be displayed</li>
<li><b>granted</b> - you are good to go and can display notificaitons</li>
</ul>
</p>
<p>
Dont worry if you dont have the right permissions set, they can be requested like this:<br>
<code>
if (Notification.permission !== "denied") { <br>
Notification.requestPermission().then(function (permission) { <br>
//Do whatever you want to do <br>
});
</code> <br>
And finally the notification is created like this: <br>
<code>
new Notification("Hi there!");
</code> <br>
Try it out below!
<br>
</p>
<button onclick="notifyMe()">Notify me!</button>
<script>
function notifyMe() {
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
} else if (Notification.permission === "granted") {
new Notification("Hello World!");
} else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
if (permission === "granted") {
new Notification("Hi there!");
}
});
}
}
</script>
<br><br><br>
<h1>The Geolocation API</h1>
<p>
This API is used to access the current position of the user.<br>
It delivers the coordinates as latitude and longitude which can be used in an google maps or openstreetmap URL
to display the position in a visual way. <br>
To use this API you need to call <br>
<code> navigator.geolocation.getCurretnPosition(success, error)</code> <br>
In this example u need to define two functions: <b>success</b> and <b>error</b>. <br>
error is used to display a message like "Unable to retrieve your location" and success is used to deisplay the
accurat position. <br>
As said before, use a function like this: <br>
<code>
function success(position) {<br>
const latitude = position.coords.latitude;<br>
const longitude = position.coords.longitude;<br>
status.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;<br>
}<br>
</code>
To use this correctly you first need to declare a text field with the ID <i>status</i>.<br>
View the source code of this website to see how it is done and try it out below!<br>
</p>
<button id="find-me" onclick="geoFindMe()">Show my location</button><br />
<p id="status"></p>
<script>
function geoFindMe() {
const status = document.querySelector("#status");
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
status.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;
}
function error() {
status.textContent = "Unable to retrieve your location due to anything";
}
if (!navigator.geolocation) {
status.textContent = "Geolocation is not supported by your browser";
} else {
status.textContent = "Locating…";
navigator.geolocation.getCurrentPosition(success, error);
}
}
</script>
<br><br><br>
<h1> The Camera API </h1>
<p>
This paragraph is about the getUserMedia()-API, which is about to replace the deprecated camera-API.<br>
The method <br>
<code>
navigator.mediaDevices.getUserMedia()
</code>
<br>
asks for the users permission to access an input device like the camera. <br>
If accepted, it returns a <i>Promise</i> which resolves into a <i>MediaStream</i>. The <i>MediaStream</i> can
contain a video track, an audio track and various other tracks.<br>
This stream can than be set as the <i>srcObject</i> of an video element in the HTML-Appliaction which the live
stream of the selected input device. <br>
Use it like this: <br>
<code>
var video = document.getElementById('video'); <br>
<br>
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { <br>
navigator.mediaDevices.getUserMedia({ <br>
video: true <br>
}).then(function (stream) { <br>
video.srcObject = stream; <br>
video.play(); <br>
}); <br>
} <br>
</code>
Try it out below! <br>
</p>
<video id="video" width="640" height="480" autoplay></video>
<script>
var video = document.getElementById('video');
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({
video: true
}).then(function (stream) {
video.srcObject = stream;
video.play();
});
}
</script>
</body>