super-typer.js
super-typer.js is a JavaScript library that allows you to create a typing effect on your website.
Table of Contents
- Installation
- Usage
- Typer API
- Constructor
- Properties
- Methods
setGlobalOptions(options)
setGlobalCallbacks(callbacks)
addCommand(command)
addCommands(commands)
clearCommands()
start()
pause()
reset()
type(text, options, callbacks)
backspace(count, options, callbacks)
arrowLeft(count, options, callbacks)
arrowRight(count, options, callbacks)
wait(time, options, callbacks)
Installation
Installing from CDN
In the CDN installation selection, you have the choice between the Global or ES Module import methods.
Using the Global Build
The Global import method is as follows:
<html>
<head>
...
<script src="https://unpkg.com/super-typer/dist/super-typer.global.js"></script>
...
</head>
<body>
...
</body>
</html>
Using the ES Module Build
The ES Module import method is as follows:
<html>
<head>
...
</head>
<body>
...
<script type="module">
import Typer from "https://unpkg.com/super-typer/dist/super-typer.esm.js";
</script>
</body>
</html>
Installing from NPM
You can install super-typer using package managers like npm, yarn, or pnpm.
npm
npm install super-typer
yarn
yarn add super-typer
pnpm
pnpm install super-typer
Usage
super-typer.js is very easy to use and has a very simple API.
Basic Usage
The basic usage of super-typer.js is as follows:
const superTyper = new Typer(
{
speed: 100
},
{
onChange: (text) => {
console.log(text);
},
}
);
const commands = [
{
command: "type",
argument: "Hello!",
},
{
command: "backspace",
argument: -1,
}
];
superTyper.addCommands(commands);
superTyper.start();
Typer API
Constructor
The constructor of Typer accepts three arguments, the global options object, the global callbacks object, and the commands array, for example:
new Typer(, , );
Global Options
The global options object is used to configure the Typer instance, it accepts the following properties:
{
speed: 100
}
Global Callbacks
The global callbacks object is used to configure the callbacks of the Typer instance, it accepts the following properties:
{
onChange: (text, cursorPosition) => {},
onBeforeChange: (text, cursorPosition) => {},
onAfterChange: (text, cursorPosition) => {},
}
Commands
The commands array is a list of commands that will be executed by the Typer instance. A command is an object that has four properties, for example:
{
command: "type",
argument: "Hello!",
options: {
speed: 80
},
callbacks: {
onChange: (text, cursorPosition) => {},
onBeforeChange: (text, cursorPosition) => {},
onAfterChange: (text, cursorPosition) => {},
}
}
Properties
isRunning
The isRunning property is a getter that returns a boolean value indicating whether the Typer instance is running or not.
isPaused
The isPaused property is a getter that returns a boolean value indicating whether the Typer instance is paused or not.
Methods
setGlobalOptions(options)
The setGlobalOptions method is used to set the global options of the Typer instance.
setGlobalCallbacks(callbacks)
The setGlobalCallbacks method is used to set the global callbacks of the Typer instance.
addCommand(command)
The addCommand method is used to add a command to the Typer instance, it accepts a command as an argument.
addCommands(commands)
The addCommands method is used to add commands to the Typer instance, it accepts an array of commands as an argument.
clearCommands()
The clearCommands method is used to clear all commands from the Typer instance.
start()
The start method is used to start the execution of the commands.
pause()
The pause method is used to pause the execution of the commands.
reset()
The reset method is used to stop execution and clear the all commands.
type(text, options, callbacks)
The type method simulates text input, it is used to add a type command and start the execution of the commands. It accepts three arguments, following is the description of each argument:
text: The content to be typed.
options: The options of this command, it will override the global options.
callbacks: The callbacks of this command, it will override the global callbacks.
backspace(count, options, callbacks)
The backspace method simulates a backspace keypress, it is used to add a backspace command and start the execution of the commands. It accepts three arguments, following is the description of each argument:
count: The number of characters to delete, if the value is negative, the text will be delete (all characters length + count + 1) characters. For example, with the text "Hello!" and a value of -1, the result is "Hello". If the value is -2, the text becomes "Hello". Thus, a value of -1 is equivalent to deleting the entire text.
options: The options of this command, it will override the Global Options.
callbacks: The callbacks of this command, it will override the Global Callbacks.
arrowLeft(count, options, callbacks)
The arrowLeft method simulates a left arrow keypress, it is used to add a arrowLeft command and start the execution of the commands. It accepts three arguments, and same as the backspace method, following is the description of each argument:
count: The number of characters to delete, if the value is negative, the text will be delete (all characters length + count + 1) characters. For example, with the text "Hello!" and a value of -1, the result is "Hello". If the value is -2, the text becomes "Hello". Thus, a value of -1 is equivalent to deleting the entire text.
options: The options of this command, it will override the Global Options.
callbacks: The callbacks of this command, it will override the Global Callbacks.
arrowRight(count, options, callbacks)
The arrowRight method simulates a right arrow keypress, it is used to add a arrowRight command and start the execution of the commands. It accepts three arguments, and same as the backspace method, following is the description of each argument:
count: The number of characters to delete, if the value is negative, the text will be delete (all characters length + count + 1) characters. For example, with the text "Hello!" and a value of -1, the result is "Hello". If the value is -2, the text becomes "Hello". Thus, a value of -1 is equivalent to deleting the entire text.
options: The options of this command, it will override the Global Options.
callbacks: The callbacks of this command, it will override the Global Callbacks.
wait(time, options, callbacks)
The wait method is used to add a wait command and start the execution of the commands. It accepts one argument, following is the description of each argument:
time: The time to wait in milliseconds.
options: The options of this command, it will override the Global Options.
callbacks: The callbacks of this command, it will override the Global Callbacks.
License
MIT