Coder Social home page Coder Social logo

theaterjs's Introduction

TheaterJS

TheaterJS is basically a javascript typing effect plugin. Its particularity is to mimic human behavior. But what's different between a robot and a human typing then?

  • We make mistakes.
  • We're not that fast and our speed is variable.
  • Those traits are based on our experience.

TheaterJS is also built on top of:

  • Powerful and simple scenario creation.
  • Multiple actors management (each one having its very own characteristics).
  • Chainable methods.
  • Event handling.

Links

  • Everything you need to know is demonstrated and explained in this codepen.
  • The demo is also available on the TheaterJS page.

Example

  var theater = new TheaterJS();
  
  theater
    .describe("Vader", .8, "#vader")
    .describe("Luke", .6, "#luke");
    
  theater
    .write("Vader:Luke.", 600)
    .write("Luke:What?", 400)
    .write("Vader:I am...", 400, " your father.");
    
  theater
    .on("say:start, erase:start", function () { /* do something when say or erase starts */ })
    .on("say:end, erase:end", function () { /* do something when say or erase ends */ })
    .on("*", function () { /* called whenever an event is triggered */ });

Documentation

describe

Arguments
  1. name (string): actor's name. e.g.: Vader:I am your father. would be a reference to the actor named "Vader".
  2. experience (float int - optional): set the actor's experience, between 0 to 1. Default value is .6
  3. voice (function, string or HTMLElement - optional): voice is used to set the actor's speech. Default value is a function logging the new value to the console.

If actor's voice is a function, it'd be called with 4 arguments:

  1. newValue: the new value of actor's speech.
  2. newChar: the new character.
  3. prevChar: the previous character.
  4. speech: the complete string being typed.

Note: as all function called by TheaterJS, the voice's context is set to the current TheaterJS instance.

write

Accepts an indefinite number of parameters. There's 5 "types" accepted:

  • string: a speech to type. If the string contains an actor's name (e.g.: "Vader:I am your father."), it also add a scene to update the current actor. When omitting the actor's name, the value will be appended instead of replaced.
  • positive integer: create a break scene lasting for the amount of the argument (ms).
  • negative integer: e.g.: -3 erase 3 characters.
  • function: a function to call when the scene is executed (context is set to the current TheaterJS instance). If the function has some asynchrone tasks, you'll need to pass true as second argument. To do so, build a scene: theater.write({ name: "call", args: [function () { return this.next(); }, true] });. Don't forget to call this.next() when you are ready to get the next scene.
  • object: a scene object with two keys: name and args. e.g.: theater.write({ name: "say", args: ["Hello!"] });

events

TheaterJS has a built-in event handler. To register an event:

  theater
    .on("say:start", function (eventName, args...) { console.log("a say scene started"); })
    .on("say:end", function (eventName, args...) { console.log("a say scene ended"); });

The value before : is the scene's name (scope) while the other part of the string is the event itself.

Note: use theater.on("*", function (eventName, realEventName, args...) {}); if you want to listen to all events.

The emit method accepts up to three arguments. The first being the "scope", the second the event and the third the arguments.

  theater
    .emit("myevent", "start", ["your", "arguments", "go", "here"])
    .emit("myevent", ["you might not need the event part"]);

theaterjs's People

Contributors

bronzehedwick avatar zhouzi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.