/
move-objects-using-voice.html
195 lines (172 loc) · 6.59 KB
/
move-objects-using-voice.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
185
186
187
188
189
190
191
192
193
194
195
<!--JavaScript - Move Object with Arrow Keys using JavaScript Function.-->
<html>
<head>
<title>JavaScript - Move Object with Arrow Keys using JavaScript Function.</title>
<style>
#myObj {
width: 200px;
height: 200px;
background: #ff0000;
border-radius: 50%;
}
</style>
<script type="text/javascript">
var objImage = null;
function init() {
objImage = document.getElementById("myObj");
objImage.style.position = "relative";
objImage.style.left = "0px";
objImage.style.top = "0px";
}
function getKeyAndMove(e) {
var key_code = e.which || e.keyCode;
switch (key_code) {
case 37: //left arrow key
moveLeft();
break;
case 38: //Up arrow key
moveUp();
break;
case 39: //right arrow key
moveRight();
break;
case 40: //down arrow key
moveDown();
break;
}
}
function moveLeft() {
objImage.style.left = parseInt(objImage.style.left) - 100 + "px";
}
function moveUp() {
objImage.style.top = parseInt(objImage.style.top) - 100 + "px";
}
function moveRight() {
objImage.style.left = parseInt(objImage.style.left) + 100 + "px";
}
function moveDown() {
objImage.style.top = parseInt(objImage.style.top) + 100 + "px";
}
window.onload = init;
</script>
</head>
<body onkeydown="getKeyAndMove(event)">
<p id="output"></p>
<button id="start"> Start! </button>
<div id="myObj"> </div>
<script>
(() => {
const startBtn = document.querySelector('#start');
const output = document.querySelector('#output');
//
function getKeyAndMove(e) {
var key_code = e.which || e.keyCode;
switch (key_code) {
case 37: //left arrow key
moveLeft();
break;
case 38: //Up arrow key
moveUp();
break;
case 39: //right arrow key
moveRight();
break;
case 40: //down arrow key
moveDown();
break;
}
}
function moveLeft() {
objImage.style.left = parseInt(objImage.style.left) - 100 + "px";
}
function moveUp() {
objImage.style.top = parseInt(objImage.style.top) - 100 + "px";
}
function moveRight() {
objImage.style.left = parseInt(objImage.style.left) + 100 + "px";
}
function moveDown() {
objImage.style.top = parseInt(objImage.style.top) + 100 + "px";
}
//
function start() {
const recognition = new webkitSpeechRecognition();
recognition.interimResults = true;
recognition.lang = "pt-BR";
recognition.continuous = true;
recognition.start();
recognition.onresult = function(event) {
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
const content = event.results[i][0].transcript.trim();
output.textContent = content;
console.log(content);
document.getElementById("myObj").style.backgroundColor= content;
switch (content) {
case 'esquerda': //left arrow key
moveLeft();
break;
case 'duas esquerdas': //left arrow key
moveLeft();
moveLeft();
break;
case 'cima': //Up arrow key
moveUp();
break;
case 'direita': //right arrow key
moveRight();
break;
case 'duas direita': //right arrow key
moveRight();
moveRight();
break;
case 'baixo': //down arrow key
moveDown();
break;
}
}
}
};
};
startBtn.addEventListener('click', () => start());
})();
</script>
<!-- Code injected by live-server -->
<script type="text/javascript">
// <![CDATA[ <-- For SVG support
if ('WebSocket' in window) {
(function () {
function refreshCSS() {
var sheets = [].slice.call(document.getElementsByTagName("link"));
var head = document.getElementsByTagName("head")[0];
for (var i = 0; i < sheets.length; ++i) {
var elem = sheets[i];
var parent = elem.parentElement || head;
parent.removeChild(elem);
var rel = elem.rel;
if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
}
parent.appendChild(elem);
}
}
var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
var address = protocol + window.location.host + window.location.pathname + '/ws';
var socket = new WebSocket(address);
socket.onmessage = function (msg) {
if (msg.data == 'reload') window.location.reload();
else if (msg.data == 'refreshcss') refreshCSS();
};
if (sessionStorage && !sessionStorage.getItem('IsThisFirstTime_Log_From_LiveServer')) {
console.log('Live reload enabled.');
sessionStorage.setItem('IsThisFirstTime_Log_From_LiveServer', true);
}
})();
}
else {
console.error('Upgrade your browser. This Browser is NOT supported WebSocket for Live-Reloading.');
}
// ]]>
</script></body>
</html>