Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="pt-br">
- <head>
- <meta charset="utf-8" />
- <title>Speech Commands</title>
- <style>
- body, html {
- margin: 0;
- }
- html {
- height: 100%;
- }
- body {
- height: inherit;
- overflow: hidden;
- max-width: 800px;
- margin: 0 auto;
- }
- h1, p {
- font-family: sans-serif;
- text-align: center;
- padding: 15px;
- }
- .control {
- height: 100px;
- overflow: auto;
- position: absolute;
- bottom: 0px;
- right: 0;
- left: 0;
- background-color: rgba(255,255,255,0.2);
- text-align: center;
- }
- #box{
- width: 100px;
- height: 100px;
- border: 2px solid black;
- border-radius: 4px;
- position: absolute;
- left: 300px;
- top:300px;
- transition: top 2s, left 2s;
- }
- </style>
- </head>
- <body>
- <h1>Comandos por voz com a Web Speech API</h1>
- <div id="box"></div>
- <p class="hints">Use os comandos: 'azul', 'verde',
- 'vermelho', 'branco', 'mover para a direita',
- 'mover para a esquerda', 'mover para cima',
- 'mover para baixo'.</p>
- <div class="control">
- <p class="output"><em>...Mensagens</em></p>
- <button id="btn-start">Iniciar</button>
- <button id="btn-stop">Parar</button>
- </div>
- <script>
- var x = 300;
- var y = 300;
- var color = 'white';
- var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
- var recognition = new SpeechRecognition();
- recognition.continuous = true;
- recognition.lang = 'pt-BR';
- recognition.interimResults = false;
- recognition.maxAlternatives = 1;
- var diagnostic = document.querySelector('.output');
- var box = document.querySelector('#box');
- recognition.onresult = function(event) {
- let length = event.results.length;
- let command = event.results[length-1][0].transcript;
- diagnostic.textContent = 'Resultado recebido: ' + command + '.';
- if (command.toLowerCase().indexOf("direita") > -1){
- x += 50;
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("esquerda") > -1){
- x -= 50;
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("cima") > -1){
- y -= 50;
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("baixo") > -1){
- y += 50;
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("verde") > -1){
- color = 'green';
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("azul") > -1){
- color = 'blue';
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("vermelho") > -1){
- color = 'red';
- updateBoxProperties();
- }
- else if (command.toLowerCase().indexOf("branco") > -1){
- color = 'white';
- updateBoxProperties();
- }
- }
- function updateBoxProperties(){
- box.style.left = x+"px";
- box.style.top = y+"px";
- box.style.backgroundColor = color;
- }
- document.querySelector('#btn-start').onclick = function() {
- recognition.start();
- }
- document.querySelector('#btn-stop').onclick = function() {
- recognition.stop();
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment