Text Typing Animations with Real-time Markdown/HTML Rendering – TypeMorph
1. Install TypeMorph with NPM and import it into your project.
# NPM $ npm install typemorphjs
import TypeMorph from "typemorphjs";
2. Or load the UMD version (Includes parsers) in your HTML document.
<!-- Local --> <script src="./dist/typemorph.umd.min.js"></script> <!-- CDN --> <script src="https://cdn.jsdelivr.net/npm/typemorphjs/dist/typemorph.umd.min.js"></script>
3. Create a container element where the typed text will appear:
<div id="typing-target"></div>
4. Initialize the library and start a basic animation.
// Get reference to the target element
const targetElement = document.getElementById("typing-target");
// Initialize TypeMorph with configuration
const typer = new TypeMorph({
parent: targetElement,
});
// Start typing animation
typer.type("Hello world!"); 5. This example demonstrates how to handle streaming Markdown rendering:
const typer = new TypeMorph({
parent: document.getElementById("typing-target"),
parseMarkdown: true,
markdownInline: true
});
// Type markdown - renders as formatted HTML
typer.type("**This is real fun** with *italic* and `code`"); 6. Create sequential animation flow:
const typer = new TypeMorph({
parent: document.getElementById("typing-target"),
loopCount: 1, // Single loop iteration
loopType: "backspace", // Backspace when looping
loopFinalBehavior: "remove", // Remove text after final loop
loopEndDelay: 500 // Delay before backspacing
});
// Chain multiple animations using async/await
await typer.loop("**This is real fun**");
await typer.loop("**I want more**", {
loopFinalBehavior: "keep" // Override: keep this text visible
}); 7. All configuration options:
text: Optional initial text for this instance.parent: The target element or its id where the text will appear.speed: Delay in milliseconds per character (or chunk).chunkSize: Number of characters typed in one iteration.loopCount: Number of loops before stopping.loopType: Defines how text is removed between loops ("clear" or "backspace").loopFinalBehavior: Defines behavior at the final loop ("keep" or "remove").loopStartDelay: Delay before restarting the typing after clearing/backspacing.loopEndDelay: Delay after typing finishes, before starting backspacing/clearing.backspaceSpeed: Delay in milliseconds per character when backspacing.showCursor: Shows a blinking cursor (|) at the end of the text.cursorChar: The character used for the cursor.parseMarkdown: If true, parses markdown syntax into HTML (implies parseHtml = true).markdownInline: Parses markdown inline, avoiding unwanted block wrappers for short text.parseHtml: Whether to interpret HTML in the text.markdownParse: Custom markdown parser function.hideCursorOnFinishTyping: Automatically hides the cursor when typing completes (if not looping).autoScroll: Automatically scrolls the parent element to end while typing.scrollInterval: Number of chunks typed before auto-scroll triggers.clearBeforeTyping: If true, clears the parents text before typing new text.htmlSanitize: Custom HTML sanitizer function.onStop: Called when a typing operation is stopped manually via .stop().onFinish: Called when typing completes naturally.onDestroy: Called when the instance is destroyed and all resources are cleaned up.const typer = new TypeMorph({
text: null,
parent: null,
speed: 50,
chunkSize: 1,
loopCount: Infinity,
loopType: "backspace",
loopFinalBehavior: "keep",
loopStartDelay: 300,
loopEndDelay: 800,
backspaceSpeed: 50,
showCursor: true,
cursorChar: "|",
parseMarkdown: false,
markdownInline: false,
parseHtml: true,
markdownParse: null,
hideCursorOnFinishTyping: true,
autoScroll: true,
scrollInterval: 1,
clearBeforeTyping: true,
htmlSanitize: null,
onStop: (instance) => {},
onFinish: (instance) => {},
onDestroy: (instance) => {},
});
8. API methods.
type(text, parent, options): Types the provided text into the target element once. Returns a Promise.loop(text, parent, options): Starts looping typing animation using the configured loopType. Returns a Promise.stop(): Gracefully stops any ongoing typing or looping operation. Returns a Promise.destroy(): Stops all operations, removes timers, event listeners, and the cursor.isTyping(): Returns whether the instance is currently typing, looping or backspacing.getCurrentLoop(): Returns the current loop iteration index.Q: Does TypeMorph work on mobile devices?
A: Yes, the library is fully compatible with mobile browsers. The auto-scroll feature detects touch-based scrolling and disables automatic scrolling when users manually interact with the content.
Q: How do I handle dynamically added content after initialization?
A: Call the type() or loop() methods again with new text. Set clearBeforeTyping to false in the options if you want to append rather than replace existing content.
Q: Can I use custom markdown or HTML parsers instead of the bundled libraries?
A: Yes, provide your own parser function via the markdownParse option or sanitizer via the htmlSanitize option.
Q: Why does my inline markdown create unexpected line breaks?
A: Set markdownInline to true when using parseMarkdown. This prevents Marked from wrapping content in block-level paragraph tags that cause layout issues.
The post Text Typing Animations with Real-time Markdown/HTML Rendering – TypeMorph appeared first on CSS Script.
Forza Horizon 6 for PC and Xbox was released on May 19. This is the…
Tom Hardy may not return for MobLand Season 3 after reportedly butting heads with cast…
Heading into Memorial Day weekend, there are some incredible deals on tons of video games…
If you're an iPhone user, then don't miss this opportunity to pick up a pair…
LEGO produces a lot of new sets each month, with more and more of these…
Google has publicly released proof-of-concept (PoC) exploit code for a critical, still-unpatched vulnerability in the…
This website uses cookies.