Skip to content

Configuration ​

All configuration is passed to createTypewriter(options). This page covers every option and its default value.

createTypewriter(options) ​

ts
import { createTypewriter, domRenderer } from "eo-typewriterjs";

const tw = createTypewriter({
  renderer: domRenderer(el),
  cursor: { kind: "pipe", animation: "blink" },
  audio: { enabled: false },
});

renderer (required) ​

An object implementing IRenderer. The two built-in factories are domRenderer(target) and stringRenderer(). See Renderers for details.

ts
import { domRenderer, stringRenderer } from "eo-typewriterjs";

// browser
const tw = createTypewriter({ renderer: domRenderer("#output") });

// server / test
const tw = createTypewriter({ renderer: stringRenderer() });

cursor ​

Controls the appearance and initial state of the "main" cursor. Cursors created later (for example by { cursor: "b" } on a command) start with the main cursor's options.

FieldTypeDefaultDescription
kindTCursorKind"pipe"Cursor shape. See cursor kinds below.
contentstring(kind default)Custom glyph string; overrides the kind's default character.
animation"blink" | "none" | TCursorAnimationOptions"blink"The built-in blink, no animation, or a custom CSS animation ({ name, duration, timingFunction, delay, iterationCount, direction, fillMode, playState }).
visiblebooleantrueWhether the cursor is visible.
classNamestring""Extra classes added to the cursor <span>, next to typewriter-cursor.
attrsRecord<string, string>{}Extra attributes on the cursor <span>. Event handler attributes (on*) are ignored.
ts
import { createTypewriter, domRenderer, ECursorKind } from "eo-typewriterjs";

const tw = createTypewriter({
  renderer: domRenderer(el),
  cursor: {
    kind: ECursorKind.BLOCK,
    animation: "blink",
    visible: true,
  },
});

Cursor kinds ​

Kind constantString valueDefault glyph
ECursorKind.PIPE"pipe"|
ECursorKind.UNDERSCORE"underscore"_
ECursorKind.BLOCK"block"▋
ECursorKind.BLOCK_UNDERSCORE"block-underscore"▄
ECursorKind.CARET"caret"^
ECursorKind.CUSTOM"custom"(empty)

Pass content: "" to create a CSS-only cursor with no rendered glyph, controlled entirely via CSS on .typewriter-cursor.

Changing cursor options at runtime ​

ts
tw.setCursorOptions({ kind: ECursorKind.UNDERSCORE }); // all cursors
tw.setCursorVisible(false);                             // hide all cursors
tw.setCursorVisible(true, "main");                      // show a specific cursor

Both accept an optional cursor selector (an id or an array of ids) and take effect immediately without stopping playback. Called from inside a hook or call() callback, a change only applies to the current run; called anywhere else, it also applies to replays and seeks.

audio ​

Controls the built-in audio engine. Audio is disabled when the audio field is omitted; passing any audio object turns it on unless it sets enabled: false.

FieldTypeDefaultDescription
enabledbooleantrueMaster switch.
volumenumber1Master volume, clamped to [0, 1].
sfxsRecord<string, TAudioSfx>built-in packNamed sound effects, each with a samples array of URLs or data URLs.
typingTAudioChannelOptionsOptions of the typing channel.
deleteTAudioChannelOptionsOptions of the delete channel.
moveTAudioChannelOptionsOptions of the cursor move channel.

Typing, deleting and moving each play on their own channel. Other commands are silent unless they set an audio override. Every channel draws from the same samples by default but varies their pitch and volume differently: typing sounds natural, deleting lower and heavier, moving is a light, quiet tick. The delete and move channels reuse the typing channel's sample selection (sfx, sfxs, strategy, avoidImmediateRepeat, overlap) unless they set their own.

Channel optionTypeDefaultDescription
sfxstringThe sfx to play.
sfxsstring[]Several sfxs to pick from; wins over sfx. When neither is set, every sfx in the pack is used.
strategyTAudioStrategy"shuffleBag"How the next sample is picked.
avoidImmediateRepeatbooleantrueNever play the same sample twice in a row (applies to "random" and across shuffle-bag rounds).
playbackRateJitter{ min, max }per channelRandom playback rate; it shifts the pitch too. Typing: 0.92–1.08, delete: 0.72–0.82, move: 1.3–1.45. Set it to undefined to disable.
volumeJitter{ min, max }per channelRandom volume factor. Typing: 0.85–1, delete: 0.8–0.95, move: 0.4–0.55.
overlapbooleantrueLet rapid sounds overlap instead of cutting the previous one off.
ts
import { createTypewriter, domRenderer, EAudioStrategy } from "eo-typewriterjs";

const tw = createTypewriter({
  renderer: domRenderer(el),
  audio: {
    volume: 0.7,
    sfxs: {
      key: { samples: ["/sounds/key1.mp3", "/sounds/key2.mp3"] },
    },
    typing: { sfx: "key", strategy: EAudioStrategy.SHUFFLE_BAG },
    move: { volumeJitter: { min: 0.2, max: 0.3 } },
  },
});

Audio strategies ​

ConstantValueBehavior
EAudioStrategy.SHUFFLE_BAG"shuffleBag"Default. Plays every sample once in a shuffled order, then reshuffles.
EAudioStrategy.ROUND_ROBIN"roundRobin"Cycles through samples in order.
EAudioStrategy.RANDOM"random"Picks a random sample each time.

Built-in SFX pack ​

The library ships a built-in pack of keyboard-click samples, used when sfxs is not provided. It lives in its own chunk that is only downloaded the first time a typewriter enables audio without custom sfxs; sounds requested before it arrives are skipped. To reference or extend it in your own code, import it from the subpath entry:

ts
import { DEFAULT_SFX_PACK } from "eo-typewriterjs/audio-pack";

const tw = createTypewriter({
  renderer: domRenderer(el),
  audio: {
    enabled: true,
    sfxs: {
      ...DEFAULT_SFX_PACK,
      mykey: { samples: ["/sounds/custom.mp3"] },
    },
  },
});

Changing audio options at runtime ​

ts
tw.setAudioEnabled(false);         // mute without stopping playback
tw.setAudioEnabled(true);          // unmute
tw.setAudioVolume(0.5);            // master volume [0, 1]
tw.setAudioOptions({ volume: 0.3 }); // replaces the whole audio configuration
tw.getAudioOptions();              // read the current configuration

Per-command audio override ​

Each command accepts an audio option that overrides the global config for that command only. An object also makes a normally silent command (such as wait or select) play the typing sound:

ts
tw.timeline
  .type("Audible line", { interval: 80 })
  .type("Silent line",  { interval: 80, audio: false })
  .type("Quieter line", { interval: 80, audio: { volume: 0.3 } })
  .type("Other sound",  { interval: 80, audio: { sfx: "key" } })
  .wait(500, { audio: { volume: 1 } });

Runtime controls reference ​

The TTypewriter object returned by createTypewriter() exposes these methods:

MethodReturnsDescription
play()Promise<void>Start, resume a pause, or replay once completed. Resolves when playback completes or is paused, stopped or cancelled; rejects when a hook or callback throws.
pause()voidPause on exactly what is rendered.
stop()voidStop and reset to the initial state.
cancel()voidStop and keep the current output on screen.
replay()Promise<void>Restart from the beginning.
seek(ms)voidJump to a time, clamped to the timeline. Keeps playing if playing; otherwise pauses there.
stepForward()voidApply the next step and pause.
stepBackward()voidUndo the last step and pause.
setRate(n)voidSet the speed multiplier (e.g. 2 for double speed); the playhead stays in place.
destroy()voidStop playback and unmount the renderer.
getState()TPlaybackControllerStatestatus, live currentTime, duration and rate.
getLiveState()TTypewriterStateCurrent document, cursor positions, and selections.
setCursorVisible(visible, cursor?)voidShow or hide cursors (all of them when no selector is given).
setCursorOptions(opts, cursor?)voidUpdate cursor appearance (all cursors when no selector is given).
setAudioEnabled(enabled)voidToggle audio without stopping playback.
setAudioVolume(volume)voidSet master volume [0, 1].
setAudioOptions(opts)voidReplace audio configuration.
getAudioOptions()TAudioOptionsRead current audio configuration.

Full example ​

ts
import { createTypewriter, domRenderer, ECursorKind, EAudioStrategy } from "eo-typewriterjs";

const el = document.getElementById("output")!;

const tw = createTypewriter({
  renderer: domRenderer(el),
  cursor: {
    kind: ECursorKind.PIPE,
    animation: "blink",
    visible: true,
  },
  audio: {
    volume: 0.6,
    typing: { strategy: EAudioStrategy.RANDOM },
  },
});

tw.timeline
  .type("Configuration demo", { by: "char", interval: 70 })
  .wait(500)
  .delete(-4, { by: "char", interval: 40 })
  .type("reference.", { by: "char", interval: 70 });

await tw.play();

Type reference ​

Released under the MIT License.