scramble()
Signature
function scramble(element: HTMLElement, options?: ScrambleOptions): ScrambleInstanceParameters
element
The target DOM element whose textContent will be animated.
options
See Options for the full list.
Returns
A ScrambleInstance with playback controls:
| Method | Description |
|---|---|
play() | Start or resume the animation |
pause() | Pause the animation |
restart() | Restart from the beginning |
destroy() | Stop and clean up resources |
isPlaying | boolean — whether the animation is playing |
progress | number — current progress (0 to 1) |
Example
import { scramble } from '@scrambl/core'
const el = document.querySelector('#title')
const instance = scramble(el, { text: 'Hello World', chars: 'katakana', from: 'center', duration: 1000, ease: 'easeOutCubic', onComplete: () => console.log('Done!'),})
// Later...instance.pause()instance.restart()Behavior
- Reads the element’s current
textContentas the “from” text (unlessoverrideis set). - Starts a
requestAnimationFrameloop that updatestextContenteach frame. - Characters transition through random glyphs from the
charsset before settling on their final value. - The reveal order is determined by the
fromdirection, optionally shuffled byperturbation. - On completion, the exact target text is rendered and
onCompletefires.
Stable Layout
Some glyph sets, especially symbols, braille, blocks, and katakanaFull, may use fallback fonts or glyph metrics that differ from your target text. Scrambl uses stable cell rendering automatically for those glyphs.
You can also force the strategy explicitly:
scramble(el, { text: 'Hello World', chars: 'braille', renderMode: 'cells',})This renders each character inside a fixed-width inline cell during the animation and keeps that stable layout on the final frame. Cells are cleaned up when the instance is destroyed or replayed.