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

              
                <div id='type-me' class='text-cursor'>
  Hello World! I SAID HELLO
</div>
<div class='buttons'>
  <button id='again'>Type again</button>
  <button id='stop'>Stop</button>
  <button id='clear'>Clear</button>
</div>

              
            
!

CSS

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

body {
  font-family: 'Special Elite', sans-serif;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #4f7b86;
  color: #f2f1c6;
  font-size: 2rem;
}

.text-cursor::after {
  content: '|';
  color: black;
  opacity: 1;
  animation: blinkTextCursor 1s infinite;
}

.text-cursor.typing::after {
  animation: none;
}

@keyframes blinkTextCursor {
  0%, 25%, 80%, 100% {
    opacity: 1;
  }
  30%, 75% {
    opacity: 0;
  }
}

.buttons {
  position: absolute;
  top: 20px;
  right: 20px;
}

              
            
!

JS

              
                class TextTyper {
  constructor(el, minTypingTime=30, randomTypingTime=175) {
    this.container = el;
    this.cursorBlinkerTimeoutId;
    this.waitCharacters = '.?!';

    this.minTypingTime = minTypingTime;
    this.randomTypingTime = randomTypingTime;

    this.stopAnimation = false;
    this.currPromiseChain = Promise.resolve();
  }

  type(text) {
    for( let char of text ) { 
      this.typeLetter(char);
      if (this.waitCharacters.includes(char)) this.wait(1000);
    }
    return this;
  }

  typeLetter(char) {
    this.chain( () => new Promise( resolve => {
      if (this.stopAnimation) return resolve();

      setTimeout( () => {
        this.container.innerText += char;
        this.stopCursorBlinking();
        resolve();
      }, this.getRandomTimeout());
    }));
    return this;
  }

  getRandomTimeout() {
    // simulates real person's typing
    return Math.random() * this.randomTypingTime + this.minTypingTime; 
  }

  stopCursorBlinking() {
    this.container.classList.add('typing');
    clearTimeout( this.cursorBlinkerTimeoutId );
    this.cursorBlinkerTimeoutId = setTimeout( () => {
      this.container.classList.remove('typing');
    }, 200);
  }

  remove(num) {
    for( let i = 0; i < num; i++ ) {
      this.removeLetter();
    }
    return this;
  }

  removeLetter() {
    this.chain( () => new Promise( resolve => {
      if (this.stopAnimation) return resolve();

      setTimeout( () => {
        let currText = this.container.innerText;
        this.container.innerText = currText.slice( 0, currText.length - 1);
        this.stopCursorBlinking();
        resolve();
      }, this.getRandomTimeout() / 2.5);
      // removing characters is usually much faster than typing

    }))
    return this;
  }

  chain( callback ) {
    this.currPromiseChain = this.currPromiseChain.then( callback );
    return this;
  }
  wait( time ) {
    this.chain( () => new Promise( resolve => {
      if (this.stopAnimation) return resolve();
      setTimeout(resolve, time)
    }));
    return this;
  }
  clear() {
    this.chain( () => this.container.innerText = '');
    return this;
  }
  stop() {
    this.stopAnimation = true;
    this.chain( () => this.stopAnimation = false );
    return this;
  }
  clearNow() {
    this.stop().clear();
    return this;
  }

}

let typer = new TextTyper( document.getElementById('type-me') );

document.querySelector('.buttons').addEventListener('click', (e) => {
  let btnId = e.target.id;
  switch (btnId) {
    case 'stop':
      typer.stop();
      break;
    case 'again':
      typer
        .clearNow()
        .type("You liked that animation, didn\'t you? Don't forget to press the like button! NOW!", 1000);
      break;
    case 'clear':
      typer.clearNow();
      break;
  }
});

const init = () => {
  // typing is devided into a few function calls just to demonstrate flexibility
  typer
    .clearNow()
    .wait(1200)
    .type("I am ven")
    .type("geance!")
    .type(" I am the night! I am ")
    .wait(300)
    .type('Superman')
    .wait(600)
    .remove('Superman'.length)
    .wait(900)
    .type('Captain Ame')
    .wait(200)
    .remove('Captain Ame'.length)
    .wait(1000)
    .type("Batman!")
    .wait(4000)
    .clear();
}
init();

              
            
!
999px

Console