Search

Back

Copy Link

Library

/

Free

/

1

3D Tilt

1

3D Tilt

Not loading? Preview

Made by

@frameroverrides

override

hover

Override

3D Tilt

Copy

Override

Copied to Clipboard!

Instructions

Apply to frames, not images

Get more overrides like this one and make your own at frameroverrides.com/generator

Code Preview

// Apply to frames only, not images

import type { ComponentType } from "react"
import { motion, useMotionValue, useTransform, useSpring } from "framer-motion"

export function with3DTilt(Component): ComponentType {
    return (props) => {
        const x = useMotionValue(0.5)
        const y = useMotionValue(0.5)

        const rotateX = useTransform(y, [0, 1], [30, -30])
        const rotateY = useTransform(x, [0, 1], [-30, 30])

        const animatedRotateX = useSpring(rotateX, {
            stiffness: 300,
            damping: 30,
        })
        const animatedRotateY = useSpring(rotateY, {
            stiffness: 300,
            damping: 30,
        })

        const handleMove = (event) => {
            const rect = event.currentTarget.getBoundingClientRect()
            // Adjusted to work with both mouse and touch events
            const clientX = event.clientX || event.touches[0].clientX
            const clientY = event.clientY || event.touches[0].clientY
            const mouseX = clientX - rect.left
            const mouseY = clientY - rect.top

            x.set(mouseX / rect.width)
            y.set(mouseY / rect.height)
        }

        return (
            <Component
                {...props}
                as={motion.div}
                style={{
                    ...props.style,
                    rotateX: animatedRotateX,
                    rotateY: animatedRotateY,
                    transformOrigin: "center",
                }}
                onMouseMove={handleMove}
                onTouchMove={handleMove}
                onMouseLeave={() => {
                    x.set(0.5)
                    y.set(0.5)
                }}
                onTouchEnd={() => {
                    x.set(0.5)
                    y.set(0.5)
                }}
            />
        )
    }
}

Previous

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.