Quick GSAP Utils

Date Added - 07/25/2025

Topics - GSAP, animation

Scramble Text

We can use this helper function to setup on load text scrambling/deciphering.

QWERTYUIOP

    
        
import gsap from "gsap";
import { ScrambleTextPlugin } from "gsap/all";
gsap.registerPlugin(ScrambleTextPlugin);
const container = document.getElementById('scramble-text') as HTMLDivElement;

const cipher = container.querySelector('#cipher') as HTMLButtonElement;
const decipher = container.querySelector('#decipher') as HTMLButtonElement;
const originalText = "Scrambled Mess";
const cipherText = "QWERTYUIOP"; // example cipher text or scrambled chars

// Cipher: scramble to random chars
cipher.addEventListener("click", () => {
	gsap.to('#scramble-this-text', {
	duration: 1.5,
	scrambleText: {
		text: cipherText,
		revealDelay: 0,
		speed: 20,
		chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	}
	});
	cipher.classList.add("hidden")
	decipher.classList.remove("hidden")
});

// Decipher: reveal original text
decipher.addEventListener("click", () => {
	gsap.to('#scramble-this-text', {
	duration: 2,
	scrambleText: {
		text: originalText,
		revealDelay: 0.5,
		speed: 10,
		chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
	}
	});
	cipher.classList.remove("hidden")
	decipher.classList.add("hidden")
});