CharWriter.js: Typing-like Animations in Vanilla JavaScript

A few days ago I opened a public repository on GitHub to share some JavaScript I had just refactored, today I would like to introduce it to you. CharWriter is an unpretentious JavaScript ES6 class whose instances are able to insert, delete and replace text into an HTML tag by animating the task in a typing-like fashion.

Peruse it on GitHub

To make the animation more realistic, the pace of typing is not as smooth as you would expect from a plain concatenation of characters, far from it: the characters are typed more or less quickly based on their type (uppercase, white space, number, and so on). But I have to note that despite my attempt to contaminate the typing-like animation by traits of the human factor, the script is anyway simple in nature, and as such it stays tied to the predictability of the virtual world.

Type, Delete, ReplaceWith ... Stop

The CharWriter class has four self-explanatory public methods:

  • CharWriter::type( text, callback, delay ) types into the target HTML tag the text received as input, by executing the optional callback once the animation is over. delay is an optional argument through which you can tell the method after how many milliseconds from the end of the animation callback must be executed.
  • CharWriter::delete( callback, delay ) animates the deletion of the text content of the target HTML tag. The two optional arguments have the same use as the homonymous arguments of the CharWriter::type() method.
  • CharWriter::replaceWith( text ) replaces the text content of the target HTML tag with the text passed as input by animating both the deletion and the insertion tasks.
  • CharWriter::stop( haltDelay, callback ) halts the currently running animation after an optional number of milliseconds set by haltDelay. callback is the optional function that must be executed once the current animation has been stopped.

An Example

The following code shows how by starting from a CharWriter object you can let it first type some text into the target HTML tag, and then let it replace the typed text when the target HTML tag receives a click event:

<!DOCTYPE html>
    <meta charset="UTF-8">
    <title>CharWriter Example</title>
    <script src="charwriter.js"></script>

<p id="text"></p>

// The animation starts once the document has been fully loaded.
window.addEventListener( 'load', () => {
    // CharWriter objects must be inizialised with the ID attribute
    // of the target HTML tag — 'text' in our example.
    const writer = new CharWriter( 'text' );
    // Function called when the target node receives a click event.
    let startReplacing = ( event ) => {
        // The 'replaceWith' animation is allowed to be launched only once. 'click', startReplacing );
        writer.replaceWith( 'Lorem ipsum dolor sit amet, 1 Jan 2022, consectetur adipisicing elit ...' );

    // Function called when the 'type' animation is over.
    let addClickEvent = () => 'click', startReplacing );

    // The 'type' animation starts 1 second after the document has been fully loaded.
    setTimeout( () => writer.type( 'Click to replace this text.', addClickEvent ), 1000 );
} );