Pen Settings

HTML

CSS

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

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

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.

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

              
                <!-- <div class="mainGrid"> -->
  <div class="nav">
    <div class="title">nbtt/type</div>
    <div class="menu">
      <div class="config">config</div>
      <div class="readme">readme</div>
      <div class="git">github</div>
    </div>
  </div>
<!--   <div class="gradient"></div> -->
  <div class="middle">
    <div class="test hidden">
      <div class="timer"></div>
      <div class="separator indicator"></div>
      <div class="input">
      <div class="inputBox"></div>
      </div>
      <div class="futureWrap">
        <div class="future"></div>
      </div>
    </div>
    <div class="result hidden">
      <div class="key">key<div class='res'>190/10 (200)</div></div>
      <div class="acc">acc<div class='res'>100%</div></div>
      <div class="wrd">wrd<div class='res'>80</div></div>
<!--       <div class="err">err<span>5</span></div> -->
<!--       <div>
        ///
      </div> -->
      <div class="wpm">wpm<div class="res">85</div></div>
      
    </div>
  </div>
<!--   <div class="footer">
    <div class="text"></div>
  </div> -->
<!-- </div> -->
              
            
!

CSS

              
                
@import url('https://fonts.googleapis.com/css?family=Lato&display=swap');
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap');

/* light mode */
:root{
  --main-color: #222;
  --sub-color: #ddd;
  --bg-color: white;
}

/* dark mode */
:root{
  --main-color: #eee;
  --sub-color: #353535;
  --bg-color: #222;
}

html{
  height: 100%;
}

body{
  font-family: 'Roboto Mono';
  letter-spacing: 1px;
  margin: 0;
/*   height: 100%; */
  padding: 0;
  grid-gap: 2rem;
  margin: 0 2rem;
  background: var(--bg-color);
  min-width: 800px;
  min-height: 100%;
  display: grid;
  grid-auto-flow: column;
  grid-template-columns: 1fr;
  grid-template-rows: 4rem auto;
  box-sizing: border-box;
  border: 2rem solid transparent;
}

/* .mainGrid{
  min-height: 100%;
  display: grid;
  grid-template-rows: 1fr auto;
} */

.nav{
/*   position: fixed;
  top: 1rem;
  left: 5rem; */
  height: 4rem;
  font-weight: 900;
  line-height: 4rem;
  font-size: 3rem;
  display: flex;
}

.nav .menu{
  display: flex;
}

.nav .title, .nav .menu div{
  color: var(--sub-color);
  margin-right: 2rem;
  transition: .2s;
}

.nav .title{
  color: var(--main-color);
}

.nav .menu div:hover{
  cursor: pointer;
  color: var(--main-color);
}

.footer{
/*   position: fixed;
  top: calc(100vh - 1rem - 100px);
  left: 5rem; */
  height: 4rem;
  font-weight: 900;
  line-height: 4rem;
  font-size: 3rem;
  color: var(--sub-color);
  grid-row-start: 3;
  grid-row-end: 4;
}

.middle{
  display: flex;
  align-items: center;
  overflow: hidden;
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 1;
  grid-column-end: 2;
}

.gradient{
  width: 100%;
/*   background: red; */
  background-image: linear-gradient(to right, transparent, var(--bg-color)); 
  z-index:99;
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 1;
  grid-column-end: 2;
}

.inputBox{
  height: 100px;
  box-sizing: border-box;
  display: grid;
  grid-auto-flow: column;
  justify-content: start;
}

.test, .title, .future{
  display: grid;
  grid-auto-flow: column;
}

.input{
  margin-right: 2rem;
}

.futureWrap{
  display: grid;
}

.futureWrap .gradient, .futureWrap .future{
  grid-row: 1;
  grid-column: 1;
}

.future{
  max-width: 50vw;
  max-width: calc(100vw - 352px - 5rem);
  overflow: hidden;
  z-index:98;
}

.futureBox{
  height: 100px;
  font-weight: 900;
  line-height: 100px;
  font-size: 3rem;
  margin-right: 2rem;
  color: var(--sub-color);
}

.hidden{
  display: none;
}


.result{
  font-weight: 900;
/*   line-height: 100px; */
  font-size: 3rem;
  color: var(--sub-color);
}

.result div div{
  display: inline-block;
  margin-left: 2rem;
  color: var(--main-color);
}

.timer{
  height: 100px;
    font-weight: 900;
    line-height: 100px;
    font-size: 3rem;
    color: var(--main-color);
    width: 5.2rem;
    max-width: 5.2rem;
    text-align: right;
    min-width: 5.2rem;
}





.separator{
  height: 100px;
  background-color: var(--sub-color);
  width: 6px;
  margin: 0 2rem;
}


.inputBox letter{
  font-weight: 900;
  height: 100px;
  line-height: 100px;
  font-size: 3rem;
  color: var(--sub-color);
}

letter.correct{
  color: var(--main-color);;
}

letter.incorrect{
  color: #F44336;
}


              
            
!

JS

              
                var words = [
  "a",
  "about",
  "all",
  "also",
  "and",
  "as",
  "at",
  "be",
  "because",
  "but",
  "by",
  "can",
  "come",
  "could",
  "day",
  "do",
  "even",
  "find",
  "first",
  "for",
  "from",
  "get",
  "give",
  "go",
  "have",
  "he",
  "her",
  "here",
  "him",
  "his",
  "how",
  "if",
  "in",
  "into",
  "it",
  "its",
  "just",
  "know",
  "like",
  "look",
  "make",
  "man",
  "many",
  "me",
  "more",
  "my",
  "new",
  "no",
  "not",
  "now",
  "of",
  "on",
  "one",
  "only",
  "or",
  "other",
  "our",
  "out",
  "people",
  "say",
  "see",
  "she",
  "so",
  "some",
  "take",
  "tell",
  "than",
  "that",
  "the",
  "their",
  "them",
  "then",
  "there",
  "these",
  "they",
  "thing",
  "think",
  "this",
  "those",
  "time",
  "to",
  "two",
  "up",
  "use",
  "very",
  "want",
  "way",
  "we",
  "well",
  "what",
  "when",
  "which",
  "who",
  "will",
  "with",
  "would",
  "year",
  "you",
  "your"
];


var currentWord = "";
var currentInput = "";
var future = [];
var timerconfig = 30;
var futureconfig = 20;
var timer;
var timerId = null;
var timerRunning = false;
var correctCount = 0;
var correctKeyCount = 0;
var incorrectCount = 0;
var incorrectKeyCount = 0;
var focused = false;

var maincolorconfig = getComputedStyle(
  document.documentElement
).getPropertyValue("--main-color");
var subcolorconfig = getComputedStyle(
  document.documentElement
).getPropertyValue("--sub-color");
var bgcolorconfig = getComputedStyle(document.documentElement).getPropertyValue(
  "--bg-color"
);

//timer ----------------------------------------------------------------------------------------------------------------------

function countdown() {
  if (timer == 0) {
    clearTimeout(timerId);
    timerRunning = false;
    showResult();
  } else {
    $(".timer").text(timer);
    timer--;
  }
}

function showResult() {
  focus(false);
  // $('.result .key .res').html(correctKeyCount+'/<span style="color:#F44336">'+incorrectKeyCount+'</span>'+' ('+(correctKeyCount+incorrectKeyCount)+')');
  // $('.result .wrd .res').html(correctCount+'/<span style="color:#F44336">'+incorrectCount+'</span>');
  $('.title').text('mmtt/result');
  $('.result .key .res').html(correctKeyCount+'/'+incorrectKeyCount);
  $('.result .wrd .res').html(correctCount+'/'+incorrectCount);
  $('.result .acc .res').html(Math.round((correctKeyCount/(correctKeyCount+incorrectKeyCount)) * 100)+'%');
  $('.result .wpm .res').html((correctCount + incorrectCount) * (60 / timerconfig));
  
  $('.test').css("opacity","1").stop(true,true).animate({opacity:0},250,function(){
    $('.test').addClass('hidden');
    $('.result').css("opacity","0").removeClass('hidden').stop(true,true).animate({opacity:1},250);
  });
}

// other funcitons ------------------------------------------------------------------------------------------------------------

function flashIndicator(correct) {
  let color = maincolorconfig;
  let subcolor = subcolorconfig;
  if (!correct) {
    color = "#F44336";
  }

  $(".indicator")
    .stop(true, true)
    .animate({backgroundColor: color},0)
    .animate({backgroundColor: subcolor},500);
}

function focus(inOrOut) {
  if ((inOrOut && focused) || (!inOrOut && !focused)) return;
  focused = inOrOut;
  let opacity = 1;
  let titlecol = maincolorconfig;
  if (inOrOut) {
    opacity = 0;
    titlecol = subcolorconfig;
  }
  $(".menu").animate({ opacity: opacity }, 500);
  $(".footer").animate({ opacity: opacity }, 500);
  $(".title").animate({ color: titlecol }, 500);
}

// input ---------------------------------------------------------------------------------------------------------------------

function wordToInput(word) {
  $(".inputBox").empty();
  for (let i = 0; i < word.length; i++) {
    $(".inputBox").append("<letter>" + word[i] + "</letter>");
  }
}

function scoreKey(keystroke){
  if(keystroke == currentWord[currentInput.length-1]){
    correctKeyCount++;
  }else{
    incorrectKeyCount++;
  }
  console.log("cor "+correctKeyCount);
  console.log("err "+incorrectKeyCount);
  // console.log(currentWord);
  // console.log(keystroke);
  // console.log(currentInput);
}

function compareInput() {
  $(".inputBox").empty();
  let ret = "";
  for (let i = 0; i < currentInput.length; i++) {
    if (currentWord[i] == currentInput[i]) {
      ret += '<letter class="correct">' + currentInput[i] + "</letter>";
    } else {
      ret += '<letter class="incorrect">' + currentInput[i] + "</letter>";
    }
  }
  if (currentInput.length < currentWord.length) {
    for (let i = currentInput.length; i < currentWord.length; i++) {
      ret += "<letter>" + currentWord[i] + "</letter>";
    }
  }
  $(".inputBox").html(ret);
}

// future ---------------------------------------------------------------------------------------------------------------------

function showFuture() {
  $(".future").empty();
  let text = "";
  for (let i = 0; i < futureconfig; i++) {
    text += '<div class="futureBox">' + future[i] + "</div>";
  }
  $(".future").html(text);
}

function moveFuture() {
  currentWord = future[0];
  wordToInput(future[0]);
  for (let i = 0; i < futureconfig - 1; i++) {
    future[i] = future[i + 1];
  }
  initLastFuture();
}

//init functions ----------------------------------------------------------------------------------------------------------------

function initInput() {
  currentInput = "";
  $(".inputBox").empty();
  let randomWord = words[Math.floor(Math.random() * words.length)];
  for (let i = 0; i < randomWord.length; i++) {
    $(".inputBox").append("<letter>" + randomWord[i] + "</letter>");
  }
  currentWord = randomWord;
}

function initFuture() {
  for (let i = 0; i < futureconfig; i++) {
    let randomWord = words[Math.floor(Math.random() * words.length)];
    future.push(randomWord);
  }
  showFuture();
}

function initLastFuture() {
  let randomWord = words[Math.floor(Math.random() * words.length)];
  future[futureconfig - 1] = randomWord;
}

function init() {

 $('.test').css('opacity','0').removeClass('hidden').stop(true,true).animate({opacity:1},250);

  $('.title').text('nbtt/type');
  future = [];
  correctCount = 0;
  incorrectCount = 0;
  correctKeyCount = 0;
  incorrectKeyCount = 0;
  timer = timerconfig - 1;
  $(".timer").text(timerconfig);
  timerRunning = false;
  clearTimeout(timerId);
  timerId = null;
  initInput();
  initFuture();
  focus(false);
}

// event handlers ------------------------------------------------------------------------

//letters
$(document).keypress(function(event) {
  if (event["keyCode"] == 13) return;
  if (event["keyCode"] == 32) return;
  currentInput += event["key"];
  if (timerId == null) {
    timerId = setInterval(countdown, 1000);
    timerRunning = true;
  }
  scoreKey(event["key"]);
  compareInput();
  if (timerRunning && !focused) focus(true);
});

//mouse
$(document).mousemove(function(event) {
  focus(false);
});

//other
$(document).keydown(function(event) {
  
  //tab
  if (event["keyCode"] == 9) {
    event.preventDefault();
    if($('.result').hasClass('hidden')){
      $('.test').css("opacity","1").stop(true,true).animate({opacity:0},250,function(){
        init();
      })
    }else{
      $('.result').stop(true,true).animate({opacity:0},250,function(){
        $('.result').addClass('hidden');
        init();
      });
    }
    
  }
  
  //backspace
  if (event["keyCode"] == 8) {
    currentInput = currentInput.substring(0, currentInput.length - 1);
    compareInput();
  }
  
  //space
  if (event["keyCode"] == 32) {
    if (currentInput == "") return;
    if (!timerRunning) return;
    if (timerId == null) {
      timerId = setInterval(countdown, 1000);
      timerRunning = true;
    }
    if (currentWord == currentInput) {
      currentInput = "";
      correctCount++;
      flashIndicator(true);
    } else {
      currentInput = "";
      incorrectCount++;
      flashIndicator(false);
    }
    $(".input").stop(true, true).animate({ marginLeft: 0 }, 0, function() {
        $(".input")
          .stop(true, true)
          .animate({ marginLeft: $(".input").outerWidth(true) }, 0, function() {
            $(".input")
              .stop(true, true)
              .animate({ marginLeft: 0 }, 200);
          });
        compareInput();
        moveFuture();
        showFuture();
      });

    // compareInput();
    // moveFuture();
    // showFuture();
  }
});

init();
$('.test').css('opacity','0').removeClass('hidden').stop(true,true).animate({opacity:1},250);
              
            
!
999px

Console