cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="intro">
  This was put together as a result of me having a great time learning from lesson 20 of <a href="https://javascript30.com">#javascript30</a> by <a href="https://twitter.com/wesbos">@wesbos</a>.
</div>
<div class="controls">
  <div>
    <input type="radio" name="language" value="da-DK" checked>
    <label for="male">Dansk</label>
    <input type="radio" name="language" value="es-MX">
    <label for="female">Español (MX)</label>
    <input type="radio" name="language" value="en-US">
    <label for="other">English (US)</label>
  </div>
  <div class="controls__text">
    <p><span class="say">Sig</span> <span class="trigger">skift farve</span> <span class="explain">for at skifte baggrundsfarve</span></p>
  </div>
</div>
<div class="words" contenteditable>
</div>
            
          
!
            
              :root {
  --bg: #ffc600;
}

html {
  font-size: 10px;
}

body {
  background: var(--bg);
  font-family: 'helvetica neue';
  font-weight: 200;
  font-size: 20px;
}

.intro {
  font-weight: bold;
}
.controls {
  max-width: 500px;
  margin: 50px auto;
}

.controls__text {
  margin-top: 20px;
}

.trigger {
  font-style: italic;
}

.words {
  max-width: 500px;
  margin: 50px auto;
  background: white;
  border-radius: 5px;
  box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.1);
  padding: 1rem 2rem 1rem 5rem;
  background: -webkit-gradient(
      linear,
      0 0,
      0 100%,
      from(#d9eaf3),
      color-stop(4%, #fff)
    )
    0
    4px;
  background-size: 100% 3rem;
  position: relative;
  line-height: 3rem;
}

p {
  margin: 0 0 3rem;
}

.words:before {
  content: '';
  position: absolute;
  width: 4px;
  top: 0;
  left: 30px;
  bottom: 0;
  border: 1px solid;
  border-color: transparent #efe4e4;
}

            
          
!
            
              window.SpeechRecognition =
  window.SpeechRecognition || window.webkitSpeechRecognition;

const recognition = new SpeechRecognition();
recognition.lang = "da-DK ";
recognition.interimResults = true;

const langStrings = {
  "da-DK": {
    say: "Sig",
    trigger: "skift farve",
    explain: "for at skifte farve"
  },
  "es-MX": {
    say: "Di",
    trigger: "cambia el color",
    explain: "para cambiar el color de fondo"
  },
  "en-US": {
    say: "Say",
    trigger: "change background",
    explain: "to change the background color"
  }
};

const controls = document.querySelector(".controls");
const say = controls.querySelector(".say");
const trigger = controls.querySelector(".trigger");
const explain = controls.querySelector(".explain");
controls.addEventListener("change", e => {
  if (!e.target.matches("input") || !e.target.checked) {
    // Not what we are listening for, so return.
    return;
  }
  // Set the language.
  recognition.lang = e.target.value;
  // Update the controls text.
  say.textContent = langStrings[e.target.value].say;
  trigger.textContent = langStrings[e.target.value].trigger;
  explain.textContent = langStrings[e.target.value].explain;
});

let p = document.createElement("p");
const words = document.querySelector(".words");
words.append(p);

function changeColor() {
  document.documentElement.style.setProperty(
    "--bg",
    `hsl(${360 * Math.random()}, ${100 * Math.random()}%, ${90 *
    Math.random()}%)`
  );
}

recognition.addEventListener("result", e => {
  const transcript = Array.from(e.results)
  .map(result => result[0])
  .map(result => result.transcript)
  .join("");
  p.textContent = transcript;
  if (e.results[0].isFinal) {
    p = document.createElement("p");
    words.appendChild(p);

    if (transcript.includes(trigger.textContent)) {
      changeColor();
    }
  }
});

recognition.addEventListener("end", e => {
  recognition.start();
});
recognition.start();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console