Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.js"></script>
<select id="lang"></select><select id="voice"></select>
<select id="rate"><option>2.0</option><option>1.5</option><option selected>1.0</option><option>0.75</option><option>0.5</option></select>
<select id="fontSize"></select><br>
<textarea id="textarea1" rows="20" cols="60" spellcheck="false"></textarea><br>
<button id="btnSpeak">Speak</button>

              
            
!

CSS

              
                
              
            
!

JS

              
                // CC0 http://creativecommons.org/publicdomain/zero/1.0/

const src = CodeMirror.fromTextArea(textarea1, {
  lineNumbers: true,
  lineWrapping: true
});
const srcElem = src.getWrapperElement();
srcElem.style.resize = "both";
src.on("focus", () => srcElem.style.outline = "1px solid black");
src.on("blur", () => srcElem.style.outline = "1px solid lightgray");
src.focus();
src.setSelectionRange = (s, e) => {
  src.setSelection(src.posFromIndex(s), src.posFromIndex(e));
};

let voices = [];
let speaking = false;
if (window.speechSynthesis) init();

async function init() {
  for (let size of ["default", "12pt", "16pt", "20pt", "24pt", "30pt", "36pt", "48pt"]) {
    addOption(fontSize, size);
  }
  fontSize.addEventListener("change", () => {
    const opt = fontSize.selectedOptions;
    if (opt.length) {
      const v = opt[0].value;
      srcElem.style.fontSize = v == "default" ? "" : v;
      src.refresh();
    }
  });
  await initVoices();
  function changeLang() {
    const opt = lang.selectedOptions;
    if (opt.length) {
      setVoices(opt[0].value, voice, btnSpeak);
    }
  }
  changeLang();
  lang.addEventListener("change", changeLang);
  btnSpeak.addEventListener("click", () => speak(voice, src, btnSpeak));
}

// Web Speech API

async function initVoices() {
  if (voices.length) return;
  voices = speechSynthesis.getVoices();
  if (!voices.length) {
    await new Promise((resolve, reject) =>
      speechSynthesis.addEventListener("voiceschanged", resolve)
    );
    voices = speechSynthesis.getVoices();
  }
  const langs = [];
  for (const v of voices) {
    const l = getLanguage(v.lang);
    if (l && langs.findIndex(x => x == l) < 0) langs.push(l);
  }
  langs.sort();
  for (const l of langs) {
    addOption(lang, l);
  }
}

function getLanguage(lang) {
  let vl = lang.replace(/(?<=^[a-z]+)_/, "-").replace(/_#.+/, ""); // for Android Chrome
  let p = vl.indexOf("-");
  return p > 0 ? vl.substring(0, p) : "";
}

function addOption(select, text) {
  const opt = document.createElement("option");
  opt.text = text;
  select.appendChild(opt);
  return opt;
}

function setVoices(lang, select, button) {
  select.innerHTML = "";
  let sel;
  const targets = voices.filter((v) => getLanguage(v.lang) == lang);
  for (const v of targets) {
    let opt = addOption(select, v.name);
    opt.voice = v;
    if (v.name.includes("(Natural)")) sel = opt;
  }
  if (sel) sel.selected = true;
  if (targets.length == 0) {
    addOption(select, "Microsoft Edge Required");
    button.disabled = true;
  }
}

async function speak(select, textarea, button1, button2) {
  if (speaking) {
    speechSynthesis.cancel();
    return;
  }
  const opt = select.selectedOptions;
  if (!opt.length || !opt[0].voice) return;
  speaking = true;
  const tc = button1.textContent;
  button1.textContent = "Stop";
  if (button2) button2.disabled = true;
  const result = await new Promise((resolve, reject) => {
    const u = new SpeechSynthesisUtterance(textarea.getValue());
    u.voice = opt[0].voice;
    u.lang = u.voice.lang;
    u.rate = parseFloat(rate.value);
    u.onend = () => resolve(true);
    u.onerror = () => resolve(false);
    u.addEventListener("boundary", (e) => {
      if (e.name != "word") return;
      textarea.focus();
      textarea.setSelectionRange(e.charIndex, e.charIndex + e.charLength);
    });
    speechSynthesis.speak(u);
  });
  if (result) textarea.setSelectionRange(0, 0);
  speaking = false;
  button1.textContent = tc;
  if (button2) button2.disabled = false;
}

function showLanguages() {
  const languages = Array.from(lang.childNodes)
    .flatMap((el) => {
      for (v of voices)
        if (getLanguage(v.lang) == el.text && v.name.includes("Online"))
          return [v.lang + ": " + v.name.replace(/^.*- (.*) \(.*/g, "$1")];
      return [];
    })
    .sort();
  src.setValue(languages.join("\n"));
  // console.log(languages.length);
}

              
            
!
999px

Console