Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>#JS30 - 20 - Speech Detection</title>
  6. <style>
  7. html {
  8. font-size: 10px;
  9. }
  10.  
  11. body {
  12. background:#ffc600;
  13. font-family: 'helvetica neue';
  14. font-weight: 200;
  15. font-size: 20px;
  16. }
  17.  
  18. .words {
  19. max-width:500px;
  20. margin:50px auto;
  21. background:white;
  22. border-radius:5px;
  23. box-shadow:10px 10px 0 rgba(0,0,0,0.1);
  24. padding:1rem 2rem 1rem 5rem;
  25. background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
  26. background-size: 100% 3rem;
  27. position: relative;
  28. line-height:3rem;
  29. }
  30. p {
  31. margin: 0 0 3rem;
  32. }
  33.  
  34. .words:before {
  35. content: '';
  36. position: absolute;
  37. width: 4px;
  38. top: 0;
  39. left: 30px;
  40. bottom: 0;
  41. border: 1px solid;
  42. border-color: transparent #efe4e4;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47.  
  48. <div class="words" contenteditable>
  49. </div>
  50.  
  51. <script>
  52. const words = document.querySelector('.words')
  53. window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
  54. const recognition = new SpeechRecognition()
  55.  
  56. recognition.interimResults = true
  57.  
  58. let p = document.createElement('p')
  59. words.appendChild(p)
  60.  
  61. recognition.addEventListener('result', e => {
  62. const transcript = Array.from(e.results)
  63. .map(result => result[0])
  64. .map(result => result.transcript)
  65. .join('')
  66.  
  67. p.textContent = transcript
  68.  
  69. if (e.results[0].isFinal) {
  70. p = document.createElement('p')
  71. words.appendChild(p)
  72. }
  73. })
  74.  
  75. recognition.addEventListener('end', recognition.start)
  76.  
  77. recognition.start()
  78. </script>
  79. </body>
  80. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement