123

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p class="letters">aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz</p>
<section id="sample-text"></section>
<main class="container">
  <div id="progress"></div>
  <div class="divider"></div>
  <h3>What's the font?</h3>
  <div class="selects">
    <div class="button option"><p></p></div>
    <div class="button option"><p></p></div>
    <div class="button option"><p></p></div>
    <div class="button option"><p></p></div>
  </div>
</main>

<section class="full-screen-overlay popup" id="welcome-popup">
  <h1>Typophile</h1>
  <div class="container">
    <h2>How well do you know your fonts?</h2>
    <h4>Typophile will find common fonts installed on your device.<br>All you have to do is <em>guess the font.</em></h4>
    <div class="check">
      <span class="mono">Include top Google fonts?</span>
      <input type="checkbox" value="None" id="check" name="check" checked />
      <label for="check"></label>
    </div>
    <div class="button" id="start-game"><p class="mono">Play!</p></div>
  </div>
</section>

<section class="full-screen-overlay popup" id="gameover-popup"></section>

<section class="full-screen-overlay run-animation" id="hurrah">
  <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="-127 129 256 256" style="enable-background:new -127 129 256 256;" xml:space="preserve">
	    <polygon class="svgpath" id="tick" points="-52,307.1 -100.4,258.7 -127,285.3 -63.1,349.2 -63,349.1 -51.4,360.8 129,180.4 101.9,153.2 	"/>
  </svg>

  <h1 class="font-prompt">Font</h1>
</section>

<section class="full-screen-overlay run-animation" id="boohoo">
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="-127 129 256 256" style="enable-background:new -127 129 256 256;" xml:space="preserve">
    <polygon class="svgpath" id="cross" points="120.6,171.6 93.5,144.5 0.6,231.7 -88,144.5 -114.6,171.1 -26.7,258.3 -114.6,345.4 -88,372 0.6,284.8 93.5,372 
	120.6,344.9 29.9,258.3 "/>
  </svg>

  <h1 class="font-prompt">Font</h1>
</section>
            
          
!
            
              @mixin fontsizer($s) {
  font-size: calc(#{$s} + 4vw);
}
$_accent: #F45B69;
$_bg: #59C3C3;
$_bg2: #52489C;
$_text: #EBEBEB;

$_correct: lightgreen;
$_incorrect: red;

$_lighter: lighten($_accent, 2%);
$_lightest: lighten($_accent, 6%);
$_darker: darken($_accent, 5%);
$_darkest: darken($_accent, 10%);

$_grad: linear-gradient(122deg, transparent, $_bg2),
    linear-gradient(32deg, $_accent, $_bg);

html {
  min-height: 100%;
  font-size: 18px;
  background: $_accent;
  background: $_grad;

  transition: background 0.2s;
}
body {
  display: flex;
  flex-flow: column;
  justify-content: space-around;
  min-height: 100%;
}

h1 {
  @include fontsizer(2rem);
  position: relative;
  &:after {
    content: '';
    position: absolute;
    bottom: 10%; left: 0; right: 0;
    height: 20%;
    background: $_accent;
    z-index: -1;
    opacity: 0.9;
  }
}
h3 {
  letter-spacing: 2px;
}
h4 {
  font-size: 1.35rem;
  font-weight: normal;
}
h1, h2, h3, h4, p, span, label {
  color: $_text;
  text-align: center;
}
a {
  text-decoration: none;
}
.container {
  width: 95%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 2rem 0;
}
.mono {
  font-family: monospace;
}
#progress {
  display: flex;
  flex-flow: row;
  justify-content: space-between;
  align-items: center;
  margin: 10px;
}
.marker {
  box-sizing: border-box;
  width: 10px; height: 10px;
  border: 2px solid $_lightest;
  border-radius: 50%;
  transition: background 0.3s, width 0.3s;
  background: $_text;  

  &.done {
    box-sizing: initial;
    border: 0;
    box-shadow: 0 0 10px $_darker, 
                0 0 0 2px  $_text;
  }
  &.false {
    background: $_incorrect;
  }
  &.true {
    background: $_correct;
  }
  &.current {
    background: $_text;
    width: 20px; height: 20px;
    box-sizing: initial;
    box-shadow: 0 0 0 5px rgba(255,255,255,0.2);
  }
}
.divider {
  border-top: 2px solid $_text;
  height: 0; width: 100%;
}
p.letters {
  font-size: 0.8rem;
  box-sizing: border-box;
  width: 100%;
  height: 1.4rem;
  line-height: 1.4;
  text-align: justify;
  position: absolute;
  margin: 0;
  padding: 0.2em 1em 0 1em;
  top: 0; left: 0; right: 0;
  text-transform: capitalize;
  color: rgba(0,0,0,0.74);
  background: rgba(255,255,255,0.8);
  
  &:after { /* Justify text when 1 line */
    content: '';
    font-size: 0;
    line-height: 0;
    height: 0;
    display: inline-block;
    width: 100%;
  }
}
#sample-text {
  width: 100%;
  box-sizing: border-box;
  padding: 3rem 1rem 1rem 1rem;
  font-family: inherit;
  @include fontsizer(4rem);
}
#sample-text p {
  font-weight: normal;
  font-style: normal;
  margin: 0;
  padding: 0;
}

.selects {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: row;
  flex-wrap: wrap;
}
.button {
  width: 150px;
  height: 60px;
  font-family: inherit;
  margin: 5px;
  padding: 5px;
  display: table;
  cursor: pointer;
  border-radius: 6px;
  opacity: 0.8;
  background: $_lighter; 
  border: 1px solid $_darkest;
  box-shadow: 0px 2px 0 $_darkest, 
              2px 4px 1px $_darker,
              0px 0px 25px rgba(0,0,0,0.1);
  letter-spacing: 1px;
  transition: all 0.1s;

  &:hover {
    background: $_lightest; 
    box-shadow: 2px 4px 3px $_darkest, 
                4px 4px 18px $_darker;
  }
  
  &:active {
    background: $_darker;
    border: 1px solid rgba(#000, .05);
    box-shadow: 1px 1px 2px rgba(#fff, .2);
  }
}
.button.secondary {
  opacity: 0.9;
  border: 0 0 rgba(0,0,0,0);
  background: #333;
  border: 1px solid #000;
  box-shadow: 0px 2px 0 #111, 2px 4px 1px #000, 0px 0px 25px rgba(0, 0, 0, 0.1);
  
  &:hover {
    background: #444; 
    box-shadow: 2px 4px 3px #111, 
                4px 4px 18px #000;
  }
  
  &:active {
    background: #111;
    border: 1px solid rgba(#000, .05);
    box-shadow: 1px 1px 2px rgba(#fff, .2);
  }
}

.button p {
  padding: 0; margin: 0;
  display: table-cell; 
  vertical-align: middle; 
}

#start-game {
  margin: 1rem auto;
}

.full-screen-overlay {
  overflow: hidden;
  opacity: 0;
  height: 0;
  background: rgba(0,0,0,0.5);
  position: fixed;
  top: 0; left:0; right: 0; bottom: 0;
  transition: opacity 0.3s;
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: space-around;
}

.popup {
  font-family: Lora, 'Times New Roman', serif;
  background: $_grad;
  justify-content: flex-start;
}

.font-list {
  width: 90%;
  max-width: 280px;
  margin: 16px auto;
  padding: 16px;
  background: rgba(255,255,255,0.16);
  border-radius: 4px;

  p {
    margin: 0;
    padding: 0;
    position: relative;
    text-shadow: 1px 2px 2px rgba(0,0,0,0.3);
    &:after {
      z-index: -1;
      content: '';
      position: absolute;
      bottom: 10%;
      left: 0;
      right: 0;
      height: 25%;
      opacity: 0.7;
     }
    &.correct:after {
        background: $_correct;
    }
    &.incorrect:after {
        background: $_incorrect;
    }
  }
}

#welcome-popup {
  height: 100%;
  opacity: 1;
  overflow-y: scroll;
}

#gameover-popup h1 {
  margin-bottom: 0;
}

.font-prompt {
  color: #FFF;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.4), 0 0 30px rgba(0,0,0,0.3);
}


/* Tick / cross */

#tick {
  stroke: #91DC5A;
}
#cross {
  stroke: #D80027;
}

.full-screen-overlay svg {
  width: 400px;
  height: 400px;
}
.svgpath {
  fill: rgba(0,0,0,0);
  stroke-width: 16px;
  stroke-dasharray: 2000;
  stroke-linecap: round;
  z-index: 2;
}
.run-animation .svgpath {
  animation: dash-animation 1.2s reverse linear forwards;  
}

@keyframes dash-animation {
  to {
    stroke-dashoffset: 2000;
  }
}

/* 
  Checkbox styling by
  https://codepen.io/bbodine1/pen/novBm */
.check {
  width: 200px;
  position: relative;
  margin: 25px auto;
  padding: 16px;
  padding-right: 50px;
  background: rgba(255,255,255,0.16);
  border-radius: 4px;
  
  label {
    width: 25px;
    height: 25px;
    cursor: pointer;
    position: absolute;
    top: 50%;
    margin-top: -12px;
    right: 16px;
    background: linear-gradient(top, #222 0%, #45484d 100%);
    border-radius: 50%;
    box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,.4);
    &:after {
      content: '';
      width: 9px;
      height: 5px;
      position: absolute;
      top: 6px;
      left: 6px;
      border: 3px solid #fcfff4;
      border-top: none;
      border-right: none;
      background: transparent;
      opacity: 0;
      transform: rotate(-45deg);
    }
    &:hover::after {
      opacity: 0.3;
    }
  }
  span {
    margin-right: 30px;
  }
  input[type=checkbox] {
    visibility: hidden;
    &:checked + label:after {
      opacity: 1;
    }    
  }
}

            
          
!
            
              var fonts = [],
  googleFonts = [],
  userFonts = [],
  quotes = [],
  fontSize;

var Utils = {},
    Typophile = {};

var s;

fonts = ['Athelas', 'Avenir', 'Bell MT', 'Book Antiqua', 'Californian FB', 'Calisto MT', 'Century Schoolbook', 'Charter', 'Franklin Gothic', 'Garamond', 'Gill Sans', 'Gill Sans MT', 'Goudy Old Style', 'Helvetica', 'Helvetica Neue', 'Hoefler Text', 'Iowan Old Style', 'Optima', 'Palatino', 'Seravek', 'Sitka', 'Agency FB', 'Big Caslon', 'Bodoni MT', 'Calibri', 'Candara', 'Centaur', 'Century', 'Constantia', 'Corbel', 'Didot', 'Futura', 'Geneva', 'High Tower Text', 'Perpetua', 'Rockwell', 'Segoe UI', 'Tw Cen MT', 'Andale Mono', 'Baskerville', 'Berlin Sans FB', 'Bernard MT Condensed', 'Cambria', 'Century Gothic', 'Cochin', 'Consolas', 'Cooper Black', 'Courier', 'Courier New', 'Didot', 'Elephant', 'Eras ITC', 'Georgia', 'Haettenschweiler', 'Impact', 'Lucida', 'Maiandra GD', 'Menlo', 'Plantagenet Cherokee', 'Skia', 'Times New Roman', 'American Typewriter', 'Arial', 'Comic Sans MS', 'Tahoma', 'Trebuchet MS', 'Verdana'];

googleFonts = ['Open Sans', 'Roboto', 'Lato', 'Roboto Slab', 'Oswald', 'Roboto Condensed', 'Source Sans Pro', 'Montserrat', 'Raleway', 'Lora', 'Merriweather', 'Ubuntu', 'Passion One', 'Lobster'];

quotes = ["Open the pod bay doors, HAL.", "I'm sorry, Dave. I'm afraid I can't do that.", "I think you know what the problem is just as well as I do.", "This mission is too important for me to allow you to jeopardize it.", "I know that you and Frank were planning to disconnect me, and I'm afraid that's something I cannot allow to happen.", "Dave, although you took very thorough precautions in the pod against my hearing you, I could see your lips move.", "Without your space helmet, Dave? You're going to find that rather difficult.", "Dave, this conversation can serve no purpose anymore. Goodbye.", "I'm afraid. I'm afraid, Dave. Dave, my mind is going.", "Good afternoon, gentlemen. I am a HAL 9000 computer.", "Daisy, Daisy, give me your answer do. I'm half crazy all for the love of you.", "I am putting myself to the fullest possible use, which is all I think that any conscious entity can ever hope to do.", "Look Dave, I can see you're really upset about this. I honestly think you ought to sit down calmly, take a stress pill, and think things over.", "Just what do you think you're doing, Dave?"];

Utils = {
  toggler: {

    showHide : function(obj, ms) {
      Utils.toggler.show(obj);
      setTimeout(function() {
        Utils.toggler.hide(obj);
      }, ms);
    },

    show : function(obj) {
      obj.style.height = 'auto';
      obj.style.overflowY = 'scroll';
      obj.style.opacity = 1;
    },

    hide : function(obj) {
      obj.style.height = 0;
      obj.style.overflow = 'hidden';
      obj.style.opacity = 0;
    },

    flash : function(obj, ms) {
      obj.style.opacity = 0;
      setTimeout(function() {
        obj.style.transition = 'opacity 0.' + ms + 's';
        obj.style.opacity = 1;
      }, ms);
    }
  },

  rand : function(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
  },

  removeClass : function(el, _class) {
    var pattern;
    
    if (el && el.className && el.className.indexOf(_class) >= 0) {
      pattern = new RegExp('\\s*' + _class + '\\s*');
      el.className = el.className.replace(pattern, ' ');
    }
  },

  generateCode : function() {
    var str, sample, i, length;
    
    str = "";
    sample = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    length = 5;
    i = length;
    
    while (i--)
      str += sample.charAt(Utils.rand(0, sample.length));

    return str;
  },

  loadGoogleFonts : function() {
    // 'googlefy' the font names so they can be loaded by the Google fonts api
    // 'Open Sans' becomes 'Open+Sans::latin'
    var googlefy = (function() {
      var f = [];
      for (var i = 0; i < googleFonts.length; i++)
        f.push(googleFonts[i].split(' ').join('+').concat('::latin'));
      return f;
    })();

    WebFontConfig = {
      google: {
        families: googlefy
      }
    };
    (function() {
      var wf = document.createElement('script');
      wf.src = 'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
      wf.type = 'text/javascript';
      wf.async = 'true';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(wf, s);
    })();
  },
  
  replayAnimation : function(obj) {
      obj.classList.remove("run-animation");
      obj.offsetWidth = obj.offsetWidth;
      obj.classList.add("run-animation");
    }
}

Typophile = {

  settings: {
    html: document.documentElement,
    sample_text: document.getElementById("sample-text"),
    opts: document.getElementsByClassName("option"),
    hurrah: document.getElementById("hurrah"),
    boohoo: document.getElementById("boohoo"),
    font_prompt: document.getElementsByClassName("font-prompt"),
    welcome_popup: document.getElementById("welcome-popup"),
    gameover_popup: document.getElementById("gameover-popup"),
    start_game: document.getElementById("start-game"),

    numOpts: document.getElementsByClassName("option").length,
    gameQuestions: 10,
    gameCount: 0,
    answers: [],
    code: '',
    includeGoogleFonts: document.getElementById("check").checked,
    nextDelay: 1800,
    googleLoaded: false
  },

  init : function() {
    var fontDetective, progress, children, i;
    s = this.settings;
    this.detectFonts();
    
    s.html.setAttribute('data-cheater', 'You little cheater, you!');
    progress = document.getElementById('progress');
    children = '';
    i = s.gameQuestions;
    
    while (i--) 
      children += '<span class="marker"></span>';

    progress.innerHTML = children;

    this.settings.markers = document.getElementsByClassName('marker');
    this.bind();
  },
  
  play : function() {
    Typophile.detectGoogleFonts();
    
    Utils.toggler.hide(s.welcome_popup);
    Utils.toggler.hide(s.gameover_popup);
      
    s.markers[0].className += ' current';
    Typophile.refresh();
  },
  
  detectFonts : function (){
    var fontDetective = new Detector();
    i = fonts.length;
    
    while (i--) {
      if (fontDetective.detect(fonts[i])) {
        userFonts.push(fonts[i]);
      }
    }
  },

  detectGoogleFonts : function(){
    if (((userFonts.length < 10) || (s.includeGoogleFonts)) && !s.googleLoaded) {
      Utils.loadGoogleFonts();
      userFonts = userFonts.concat(googleFonts);
      googleLoaded = true;
    } else if (s.googleLoaded) {
      userFonts = userFonts.concat(googleFonts);
    }
  },
  
  restart : function(){
    userFonts = [];
    Typophile.detectFonts();

    var i = s.markers.length;
    while (i--) {
      Utils.removeClass(s.markers[i], 'true');
      Utils.removeClass(s.markers[i], 'false');
    }
    s.gameCount = 0;
    s.answers = [];
    Typophile.play();
  },

  bind : function() {
    var i, checkbox;
    
    checkbox = document.getElementById("check");
    
    checkbox.addEventListener('click', function(){
      s.includeGoogleFonts = checkbox.checked;
    });
    
    s.start_game.addEventListener('click', Typophile.play);
    
    i = s.numOpts;  
    while (i--) {
      s.opts[i].addEventListener('click', function() {
        Typophile.submit(this.getAttribute('data-code'));
      });
    }
  },

  submit : function(code) {
    if (s.gameCount < (s.gameQuestions - 1)) {
      Typophile.mark(code)
      
      setTimeout(function() {
        Typophile.refresh();
        Typophile.increment();
      }, s.nextDelay);
    } else {
      Typophile.mark(code)
      Typophile.gameOver();
    }
  },

  mark : function(code) {
    var lastCorrect = (code === s.code);

    s.answers[s.answers.length] = {
      font: s.html.style.fontFamily,
      result: lastCorrect
    };

    if (lastCorrect) {
      Utils.replayAnimation(s.hurrah);
      Utils.toggler.showHide(s.hurrah, s.nextDelay);
      s.markers[s.gameCount].className += ' true done';
    } else {
      Utils.replayAnimation(s.boohoo);
      Utils.toggler.showHide(s.boohoo, s.nextDelay);
      s.markers[s.gameCount].className += ' false done';
    }
  },

  increment : function() {
    Utils.removeClass(s.markers[s.gameCount], 'current');
    s.gameCount++;
    s.markers[s.gameCount].className += ' current';
  },

  refresh : function() {
    var i, currFont, n;
        
    s.sample_text.removeAttribute('style');
    Utils.toggler.flash(s.sample_text, 400);

    i = Utils.rand(0, userFonts.length);
    currFont = userFonts[i];
    userFonts.splice(i, 1);
   
    s.html.style.fontFamily = '/* You little cheater you! */' + currFont;
    s.sample_text.innerHTML = "<p>" + quotes[Utils.rand(0, quotes.length)] + "</p>";
    Typophile.populateOptions(currFont);

    fontSize = 4;
    while ((s.sample_text.clientHeight > 600) && (fontSize >= 1)) {
      fontSize--;
      s.sample_text.style.fontSize = "calc(" + fontSize + "rem + 3vw)";
    }

    n = s.font_prompt.length;
    while (n--) {
      s.font_prompt[n].innerHTML = currFont;
    }
  },

  gameOver : function() {
    var i, score, font, fonts, result, show_score, message, buttons, replay, follow;
    
    i = s.answers.length;
    score = 0
    fonts = '';
    while (i--) {
      font = s.answers[i].font.replace(/'/g, '');
      if (s.answers[i].result) {
        score++;
        fonts += '<p class="correct" data-font="'+font+'">'+font+' </p>';
      } else {
        fonts += '<p class="incorrect" data-font="'+font+'">'+font+' </p>'; 
      }
    }
    
    show_score = '<h1>'+score+'/'+s.gameQuestions+'</h1>';
    result = '<div class="font-list">' + fonts + '</div>';
/*     follow = '<a href="https://codepen.io/woodwork" target="_blank" class="button secondary codepen"><p class="mono">Follow me on CodePen</p></a>';
 */    replay = '<div class="button" id="restart-game"><p class="mono">Play Again</p></div>';

    if (score === s.gameQuestions) { message = "Hell yeah! You typophile, you."; }
    else if (score > (s.gameQuestions * 0.8)) {  message = 'Good job!'; } 
    else if (score > (s.gameQuestions * 0.4)) { message = 'Not bad!'; } 
    else { message = 'Ouch... Better luck next time'; }
    
    message = '<h2>' + message + '</h2>';
    buttons = '<div class="selects">'/* +follow */ + replay+'</div>';
    
    s.gameover_popup.innerHTML = show_score + message + result + buttons;    
    
    s.markers[s.gameCount].className += ' done';
    Utils.removeClass(s.markers[s.gameCount], 'current')
    Utils.toggler.show(s.gameover_popup);
    
    document.getElementById("restart-game").addEventListener('click', Typophile.restart);
  },

  populateOptions : function(font) {
    var options = [font],
        randFont, 
        i,
        rand;

    i = s.numOpts;

    while (i--) {
      randFont = userFonts[Utils.rand(0, userFonts.length)];
      
      while ((options.indexOf(randFont) > -1) &&
        (userFonts.length > options.length)) {
        randFont = userFonts[Utils.rand(0, userFonts.length)];
      }
      
      options.push(randFont);
      s.opts[i].setAttribute('data-code', Utils.generateCode());
      s.opts[i].childNodes[0].innerHTML = randFont;
    }
    
    rand = Utils.rand(0, s.numOpts);

    s.code = Utils.generateCode();
    s.opts[rand].setAttribute('data-code', s.code);
    s.opts[rand].childNodes[0].innerHTML = font;

  }
}

window.onload = function() {
  Typophile.init();
};
            
          
!
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.

Console