Natural typing animations

Typecadence is a JavaScript library that provides an animated typing effect that automatically simulates natural human typing, including speed variations and mistakes. It applies the animation to any HTML element with the class typecadence, which plays only when the element is visible to the user.

<span class="typecadence">This is Typecadence with its default settings.</span>
This is Typecadence with its default settings.

Installation Options

CDN (Recommended)

Include the following script tag in your HTML document:

<script src="https://cdn.jsdelivr.net/npm/typecadence@^1"></script>

NPM

Install Typecadence using NPM in your project directory via the command line:

npm install typecadence

Then import Typecadence into your TypeScript/JavaScript project:

import Typecadence from 'typecadence';

Download

Download the latest version of Typecadence from the GitHub repository and include the /dist/typecadence.js file in your project.

Typing

Typing: Speed

The minimum and maximum typing speed. The value is a single number of milliseconds or a range in milliseconds separated by a comma. Each new keystroke is typed at the specified speed or a random speed within the specified range.

  • Attribute: data-typecadence-speed
  • Value: Number[,Number]
  • Default: 50,100
<span class="typecadence" data-typecadence-speed="200,1400">This is Typecadence with a custom speed.</span>
This is Typecadence with a custom speed.

Typing: Delay

The delay before typing starts. The value is a number of milliseconds.

  • Attribute: data-typecadence-delay
  • Value: Number
  • Default: 0
<span class="typecadence" data-typecadence-delay="3000">This is Typecadence with a three-second start delay.</span>
This is Typecadence with a three-second start delay.

Typing: Space Speed

A separate typing speed for space characters. When set, spaces are typed at this speed instead of the general typing speed. When not set, spaces use the general typing speed. The value format is the same as the general speed attribute.

  • Attribute: data-typecadence-space-speed
  • Value: Number[,Number]
  • Default: inherits from speed
<span class="typecadence" data-typecadence-space-speed="500,800">This is Typecadence with a slower space speed.</span>
This is Typecadence with a slower space speed.

Typing: Backspace Speed

A separate speed for backspace corrections. When set, mistakes are corrected at this speed instead of the general typing speed. When not set, backspacing uses the general typing speed. The value format is the same as the general speed attribute.

  • Attribute: data-typecadence-backspace-speed
  • Value: Number[,Number]
  • Default: inherits from speed
<span class="typecadence" data-typecadence-mistakes="25" data-typecadence-backspace-speed="20,40">This is Typecadence with fast backspace corrections.</span>
This is Typecadence with fast backspace corrections.

Mistakes

Mistakes: Chance

The chance of a mistake being made with each keystroke. The value is a percentage.

  • Attribute: data-typecadence-mistakes
  • Value: Number
  • Default: 5
<span class="typecadence" data-typecadence-mistakes="25">This is Typecadence with a 25% chance of a mistake being made with each keystroke.</span>
This is Typecadence with a 25% chance of a mistake being made with each keystroke.

Mistakes: Present

If there is a chance for mistakes, this is the maximum number of mistakes that can be present before backspacing to correct the earliest mistake.

  • Attribute: data-typecadence-mistakes-present
  • Value: Number
  • Default: 1
<span class="typecadence" data-typecadence-mistakes-present="3">This is Typecadence with a maximum of three mistakes present before backspacing to correct the earliest mistake.</span>
This is Typecadence with a maximum of three mistakes present before backspacing to correct the earliest mistake.

Mistakes: Keyboard Layout

If there is a chance for mistakes, this is the keyboard layout used to determine which keys are adjacent to each other for mistakes.

  • Attribute: data-typecadence-mistakes-keyboard
  • Value: String
  • Options: qwerty, qwertz, or azerty
  • Default: qwerty
<span class="typecadence" data-typecadence-mistakes-keyboard="azerty">This is Typecadence with a AZERTY keyboard layout.</span>
This is Typecadence with a AZERTY keyboard layout.

Caret

By default, the caret is a <span> with a class of typecadence-caret. The tag, class, and ID of the caret can be customized.

Caret: Display

Disable the caret.

  • Attribute: data-typecadence-caret
  • Value: Boolean
  • Default: true
<span class="typecadence" data-typecadence-caret="false">This is Typecadence with caret.</span>
This is Typecadence without a caret.

Caret: Tag

The HTML tag used for the caret element.

  • Attribute: data-typecadence-caret-tag
  • Value: String
  • Default: span
<span class="typecadence" data-typecadence-caret-tag="i">This is Typecadence with a caret rendered as an italic element.</span>
This is Typecadence with a caret rendered as an italic element.

Caret: Class

An additional CSS class to apply to the caret element. The typecadence-caret class is always applied.

  • Attribute: data-typecadence-caret-class
  • Value: String
  • Default: none
<span class="typecadence" data-typecadence-caret-class="my-caret">This is Typecadence with a custom caret class.</span>
This is Typecadence with a custom caret class.

Caret: ID

An ID to apply to the caret element.

  • Attribute: data-typecadence-caret-id
  • Value: String
  • Default: none
<span class="typecadence" data-typecadence-caret-id="my-caret-id">This is Typecadence with a custom caret ID.</span>
This is Typecadence with a custom caret ID.

Caret: Character

The caret character.

  • Attribute: data-typecadence-caret-char
  • Value: String
  • Default: |
<span class="typecadence" data-typecadence-caret-char="_">This is Typecadence with a custom caret character.</span>
This is Typecadence with a custom caret character.

Caret: Color

The caret color.

  • Attribute: data-typecadence-caret-color
  • Value: String
  • Default: inherit
<span class="typecadence" data-typecadence-caret-color="#cc2127">This is Typecadence with a custom caret color.</span>
This is Typecadence with a custom caret color.

Caret: Blink

Disable the caret's blinking animation.

  • Attribute: data-typecadence-caret-blink
  • Value: Boolean
  • Default: true
<span class="typecadence" data-typecadence-caret-blink="false">This is Typecadence without a blinking caret.</span>
This is Typecadence without a blinking caret.

Caret: Blink Speed

The caret blink speed. The value is in milliseconds.

  • Attribute: data-typecadence-caret-blink-speed
  • Value: Number
  • Default: 500
<span class="typecadence" data-typecadence-caret-blink-speed="1000">This is Typecadence with a slower blinking caret.</span>
This is Typecadence with a slower blinking caret.

Caret: Remain

The caret can be set to permanently remain at the end of the text.

  • Attribute: data-typecadence-caret-remain
  • Value: Boolean
  • Default: false
<span class="typecadence" data-typecadence-caret-remain="true">This is Typecadence with a caret that permanently remains at the end of the text.</span>
This is Typecadence with a caret that permanently remains at the end of the text.

Caret: Remain Timeout

The caret can be set to remain at the end of the text for a specific amount of time. The value is in milliseconds.

  • Attribute: data-typecadence-caret-remain-timeout
  • Value: Number
  • Default: 0
<span class="typecadence" data-typecadence-caret-remain-timeout="5000">This is Typecadence with a caret that remains at the end of the text for five seconds.</span>
This is Typecadence with a caret that remains at the end of the text for five seconds.

Callback

Callback: Function

The name of a global function to call when the animation is complete. The function receives the animated element as its argument.

  • Attribute: data-typecadence-callback
  • Value: String
  • Default: none
<span class="typecadence" data-typecadence-callback="onTypingComplete">This animation will call a function when complete.</span>
This animation will call a function when complete.

Callback: Event

Every animated element dispatches a typecadence:complete event when the animation finishes. The event bubbles and includes the element in event.detail.element.

document.querySelector('.typecadence')
  .addEventListener('typecadence:complete', (e) => {
    console.log('Typing finished!', e.detail.element);
  });
This animation will fire an event when complete.