• Stars
    star
    3,232
  • Rank 13,880 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Typing animation mimicking human behavior.

TheaterJS

Typing animation mimicking human behavior.

If you're not particularly interested in managing multiple actors and the several features TheaterJS has to offer (e.g mistakes, variable speed, callbacks, html support, and so on), have a look at this fiddle. It's a minimalist version that supports play/stop, it has a lot of comments so you understand what's going on under the hood. It might well be enough for you usage :)

Installation

With npm:

npm install theaterjs

With yarn:

yarn add theaterjs

Example

<div id="vader"></div>
<div id="luke"></div>

<script src="path/to/theater.min.js"></script>
<script>
  var theater = theaterJS();

  theater
    .on("type:start, erase:start", function() {
      // add a class to actor's dom element when he starts typing/erasing
      var actor = theater.getCurrentActor();
      actor.$element.classList.add("is-typing");
    })
    .on("type:end, erase:end", function() {
      // and then remove it when he's done
      var actor = theater.getCurrentActor();
      actor.$element.classList.remove("is-typing");
    });

  theater.addActor("vader").addActor("luke");

  theater
    .addScene("vader:Luke...", 400)
    .addScene("luke:What?", 400)
    .addScene("vader:I am", 200, ".", 200, ".", 200, ". ")
    .addScene("Your father!")
    .addScene(theater.replay);
</script>

Documentation

To get started, you'll first need to create a new TheaterJS object by eventually providing some options.

Example

var theater = theaterJS({ locale: "fr" });

Usage

theaterJS(<options>)
Param Default Description
options {autoplay, locale, minSpeed, maxSpeed} Options (see below).

Breakdown of the available options:

Option Default Description
autoplay true If true, automatically play the scenario (when calling addScene).
locale detect Determine which keyboard to use when typing random characters (mistakes). Note: "detect" is an option to detect the user's locale and use if it's supported.
minSpeed { erase: 80, type: 80 } Minimum delay between each typed characters (the lower, the faster).
maxSpeed { erase: 450, type: 450 } The maximum delay between each typed characters (the greater, the slower).

Regarding minSpeed and maxSpeed, you can also just pass a number instead of an object. If you do so, this value will be used for both the erase and type speed, e.g:

{
  "minSpeed": {
    "erase": 80,
    "type": 80
  },

  "maxSpeed": {
    "erase": 450,
    "type": 450
  }
}

Is equivalent to:

{
  "minSpeed": 80,
  "maxSpeed": 450
}

TheaterJS objects have two public (read only) properties:

  • theater.options: object's options.
  • theater.status: object's status (whether "playing", "stopping" or "ready").

addActor

Add an actor to the casting.

Example

var theater = theaterJS();

theater
  .addActor("vader")
  .addActor("luke", 0.8, ".luke-selector")
  .addActor("yoda", { accuracy: 0.4, speed: 0.6 }, function(displayValue) {
    console.log("%s said yoda", displayValue);
  });

Usage

theater.addActor(<name>, <options>, <callback>)
Param Default Description
name Name used to identify the actor.
options 0.8 Actor's options (see below).
callback (see below) A function to call when actor's display value changes.

Actors have two options:

  • accuracy (number between 0 and 1): used to determine how often an actor should make mistakes.
  • speed (number between 0 and 1): used to determine how fast the actor types.

Note: the delay between each typed character varies to "mimick human behavior".

An actor callback is a function that is called when its display value is set. It can also be a string, in such case TheaterJS will assume it's a DOM selector and will look for the corresponding element. It's then going to set the element's innerHTML when the value changes. You can safely ignore this argument if you gave the target element an id with the name of the actor, i.e:

theater.addActor("vader");

In this situation, TheaterJS will look for an element that matches the selector #vader. Also note that the actor will have an additional $element property referring to the DOM element when using a selector string.

getCurrentActor

Return the actor that is currently playing.

Example

var theater = theaterJS();

theater
  .addActor("vader")
  .addScene("vader:Luke...")
  .addScene(function(done) {
    var vader = theater.getCurrentActor();
    vader.$element.classList.add("dying");
    done();
  });

Usage

theater.getCurrentActor();

addScene

Add scenes to the scenario and play it if options.autoplay is true.

Example

var theater = theaterJS();

theater
  .addActor("vader")
  .addScene("vader:Luke... ", "Listen to me!", 500)
  .addScene(theater.replay);

Usage

theater.addScene(<scene>)

A scene can be of 5 different types:

theater
  .addScene("vader:Luke... ") // 1
  .addScene(800) // 2
  .addScene("I am your father!") // 3
  .addScene(-7) // 4
  .addScene("mother!")
  .addScene(function(done) {
    // do stuff
    done();
  }); // 5
  1. .addScene('vader:Luke... ') erase actor's current display value, then type the new value.
  2. .addScene(800) make a break of 800 milliseconds before playing the next scene.
  3. .addScene('I am your father!') append value to the current actor's display value.
  4. .addScene(-7) erase 7 characters.
  5. .addScene(fn) call fn which receives a done callback as first argument (calling done() plays the next scene in the scenario).

Note that addScene actually accepts an infinite number of arguments so you could just do:

theater
  .addScene("vader:Luke... ", 800, "I am your father!")
  .addScene(-7, "mother!")
  .addScene(fn);

getCurrentSpeech

Return the speech that is currently playing.

Example

var theater = theaterJS();

theater
  .addActor("vader")
  .addScene("vader:Luke...")
  .on("type:start", function() {
    console.log(theater.getCurrentSpeech()); // outputs 'Luke...'
  });

Usage

theater.getCurrentSpeech();

play

Play the scenario.

Example

var theater = theaterJS({ autoplay: false });

theater.addActor("vader").addScene("vader:Luke...");

document.querySelector("button").addEventListener(
  "click",
  function() {
    theater.play();
  },
  false
);

Usage

theater.play();

replay

Replay the scenario from scratch (can be used as a callback to create a loop).

Example

var theater = theaterJS();

theater
  .addActor("vader")
  .addScene("vader:Luke...")
  .addScene(theater.replay);

Usage

theater.replay();

stop

Stop the scenario after the current playing scene ends.

Example

var theater = theaterJS();

theater.addActor("vader").addScene("vader:Luke... ", "I am your father...");

document.querySelector("button").addEventListener(
  "click",
  function() {
    theater.stop();
  },
  false
);

Usage

theater.stop();

on

Add a callback to execute when an event is emitted (e.g when a scene starts/ends).

Example

var theater = theaterJS();

theater
  .on("type:start, erase:start", function() {
    var actor = theater.getCurrentActor();
    actor.$element.classList.add("blinking-caret");
  })
  .on("type:end, erase:end", function() {
    var actor = theater.getCurrentActor();
    actor.$element.classList.remove("blinking-caret");
  });

theater.addActor("vader").addScene("vader:Luke...");

Usage

theater.on(<eventName>, <callback>)
Param Default Description
eventName Event's name to listen to.
callback Function to call when the event got published.

The callback function receives the event's name as first argument.

A couple of things to note:

  • Listen to all event by using the shortcut: theater.on('*', callback).
  • An event is emitted when a sequence starts (sequence:start) and ends (sequence:end), e.g theater.addScene('vader:Luke.', 'vader:I am your father.') is one sequence.
  • An event is emitted when the scenario starts and ends, respectively scenario:start and scenario:end.
  • The scenario is stoppable within :end event listeners. It means that calling theater.stop() within a callback that listen for the :end of a scene will stop the scenario. This is useful for asynchronous callbacks (e.g animations).

Localized Keyboards

When making a mistake, an actor's gonna type a random character near the one he intended to. Those characters are taken from a "mapped" keyboard that you can configure on TheaterJS' instantiation: theaterJS({locale: 'en'}).

Change Log

3.2.0 - 2018-06-04

  • add "getCurrentSpeech()"

3.1.0 - 2016-11-14

  • add "main" property to the package.json
  • remove irrelevant files from the npm package

3.0.0 - 2016-03-20

  • disabling the erase option should still clear display value

2.2.1 - 2016-03-19

  • fix end scenes event that throwed an error due to how .replay() works

2.2.0 - 2016-03-17

  • publish an event when the scenario starts and ends
  • scenario should be stoppable in :end events callbacks

2.1.0 - 2016-03-15

  • emit an event when a sequence starts and ends

2.0.2 - 2016-03-13

  • compile a non-minified version along with the minified one
  • fix window detection
  • fix bower.json configuration
  • add support for slash-less void elements (e.g <br> instead of <br/>)
  • fix play/stop issue #49
  • add option to configure erase's min/max speed independently

2.0.1 - 2015-11-02

  • publish to npm, fix for non-browser environment
  • add a .npmignore file
  • add source map

2.0.0 - 2015-11-02

  • Brand new version

More Repositories

1

awesome-perceived-performance

💫 Perceived performance best practices & resources.
185
star
2

logicalornot

A Game About JavaScript Logical Operators.
JavaScript
45
star
3

contrib-awakens

Play games in GitHub's contribution graph.
HTML
37
star
4

GentleForm

Accessible and user-friendly HTML5 form validation library.
JavaScript
36
star
5

broll

Éditeur de carte vidéo YouTube
TypeScript
30
star
6

graphql-codegen-factories

Generate factories from a GraphQL schema and operations to mock data.
TypeScript
29
star
7

docusaurus-graphql-plugin

Docusaurus plugin generating Markdown documentation from a GraphQL schema.
TypeScript
24
star
8

awesome-gwent

Awesome resources for GWENT®: The Witcher Card Game.
20
star
9

hide-files-from-bitbucket-pr

Hide irrelevant files from Bitbucket pull requests.
JavaScript
11
star
10

included-with-xbox-game-pass

Browser extension bringing the Xbox Game Pass to Steam.
TypeScript
10
star
11

Poffer

Automate sharing Pocket links to Twitter through Buffer.
JavaScript
10
star
12

cheerladders

Support ain't about money.
JavaScript
7
star
13

gwent-api-client

HTTP client for the non-official Gwent API.
JavaScript
5
star
14

funkollect.io

Have fun managing your funko's funky pop collection.
CoffeeScript
4
star
15

gulp-revise

Opinionated, straight to the point assets revisioning.
JavaScript
4
star
16

linkhunter

Detect links that real users actually type.
JavaScript
3
star
17

name-the-gwent-card

In this mini-game, your goal is to name a random Gwent card from its illustration.
TypeScript
2
star
18

shrtlnk

TypeScript
2
star
19

react-sheets-import

Let users load a sheet and map its columns to your model.
JavaScript
2
star
20

gulp-bucket

Maximize gulp tasks reusability.
JavaScript
2
star
21

pizzapp

TypeScript
1
star
22

zhouzi

1
star
23

sequelize-require-models

Import all Sequelize models from a folder and associate them together.
JavaScript
1
star
24

content-placeholder

Content placeholder made easy.
CSS
1
star
25

partenaires-trackdechets

Application web listant des outils ayant pour objectif de faciliter l'embarquement de ses partenaires Trackdéchets.
TypeScript
1
star
26

indewiz

Trouve enfin le statut qui convient le mieux à ton business.
TypeScript
1
star
27

pizzapp-server

TypeScript
1
star
28

experiments

JavaScript
1
star
29

kay

JavaScript
1
star
30

docusaurus-plugin

JavaScript
1
star
31

sync-copies

Assistant to keep duplicated copies of a file identical, aka the ultimate copy/paste tool.
TypeScript
1
star
32

react-click-outside

Listen to clicks happening outside a given container
TypeScript
1
star