Search

Back

Copy Link

Library

/

Free

/

67

Scrambler

67

Scrambler

FREE

Not loading? Preview

Made by

FramerUniversity

override

none

Override

Scrambler

Copy

Override

Copied to Clipboard!

Instructions

Code Preview

import type { ComponentType } from "react"
import { useEffect, useState, useRef } from "react"

// Loop only
const loopDelay = 1000
const initDelay = 100

// Do not configure
const letters = "abcdefghijklmnopqrstuvwxyz-.,+*!?@&%/="

export function onAppear(Component): ComponentType {
    return (props) => {
        const value = props.children.props.children.props.children

        const [isVisible, setIsVisible] = useState(false)
        const [iteration, setIteration] = useState(0)
        const intersectionRef = useRef(null)

        const encrypt = (iteration: number) => {
            return value
                .split("")
                .map((letter, index) => {
                    if (index < iteration) {
                        return value[index]
                    }
                    return letters[Math.floor(Math.random() * 38)]
                })
                .join("")
        }

        useEffect(() => {
            const observer = new IntersectionObserver((entries) => {
                entries.forEach((entry) => {
                    if (entry.isIntersecting) {
                        setIsVisible(true)
                    } else {
                        setIsVisible(false)
                    }
                })
            })

            observer.observe(intersectionRef.current)

            return () => observer.disconnect()
        }, [])

        useEffect(() => {
            let interval = null

            if (isVisible) {
                interval = setTimeout(() => {
                    setIteration((prev) => prev + 5 / 6)
                    interval = setInterval(() => {
                        setIteration((prev) => prev + 5 / 6)
                    }, 50)
                }, 100)
            }

            return () => clearInterval(interval)
        }, [isVisible])

        return (
            <Component
                ref={intersectionRef}
                {...props}
                text={isVisible ? encrypt(iteration) : value}
            />
        )
    }
}

export function onLoop(Component): ComponentType {
    return (props) => {
        const value = props.children.props.children.props.children

        const [isVisible, setIsVisible] = useState(false)
        const [iteration, setIteration] = useState(0)
        const [delayTime, setDelayTime] = useState(loopDelay)
        const intersectionRef = useRef(null)

        const encrypt = (iteration: number) => {
            const length = value.length
            let result = ""

            for (let i = 0; i < length; i++) {
                const letter = value[i]

                if (i < iteration) {
                    result += letter
                } else {
                    result += letters[Math.floor(Math.random() * 38)]
                }
            }

            if (iteration >= length) {
                setTimeout(() => {
                    setIteration(0)
                }, delayTime)
            }

            return result
        }

        useEffect(() => {
            const observer = new IntersectionObserver((entries) => {
                entries.forEach((entry) => {
                    if (entry.isIntersecting) {
                        setIsVisible(true)
                    } else {
                        setIsVisible(false)
                    }
                })
            })

            observer.observe(intersectionRef.current)

            return () => observer.disconnect()
        }, [])

        useEffect(() => {
            let interval = null

            if (isVisible) {
                interval = setTimeout(() => {
                    setIteration((prev) => prev + 5 / 6)
                    interval = setInterval(() => {
                        setIteration((prev) => prev + 5 / 6)
                    }, 50)
                }, initDelay)
            }

            return () => clearInterval(interval)
        }, [isVisible])

        return (
            <Component
                ref={intersectionRef}
                {...props}
                text={isVisible ? encrypt(iteration) : value}
            />
        )
    }
}

Terms of Use for Free Assets

By accessing and using this Framer Override Library, you agree to the following terms:

All code snippets provided are available for your personal and professional use. This includes personal websites, client projects, and other website projects in Framer. You are strictly prohibited from sharing, redistributing, selling these code snippets, or creating derivative works for resale or distribution. Unauthorized sharing, distribution, or selling of these code snippets is a breach of these terms and may result in termination of your access to this library, along with potential legal action.