DEV Community

Cover image for πŸš€ Exploring 3DTilt: I Built the NPM Library for ✨ 3D Tilt Effects in React Apps 🎯
Sivasubramaniyam
Sivasubramaniyam

Posted on • Edited on

πŸš€ Exploring 3DTilt: I Built the NPM Library for ✨ 3D Tilt Effects in React Apps 🎯

npm version npm downloads npm license npm bundle size GitHub stars GitHub forks

Introduction

Adding interactive elements to your web application can significantly enhance the user experience. One such effect is the 3D tilt, which provides a dynamic, responsive look when users hover over elements. In this article, I'll introduce the 3DTilt component, a lightweight React component that allows you to easily add 3D tilt effects to your applications.

What is 3DTilt?

The 3DTilt component is a React component designed to add a 3D tilt effect to any element. The effect is achieved by tilting the element based on the user's mouse position relative to the element. This interaction creates a smooth, engaging visual effect, ideal for cards, images, buttons, or any interactive element in your application.

Why Use 3DTilt?

  • Ease of Use: The 3DTilt component is simple to integrate into any React application. With minimal configuration, you can add complex 3D tilt effects to your UI elements.
  • Customization: The component offers customizable properties, allowing you to adjust the tilt intensity, scale, glare effects, and more to fit your design requirements.
  • Performance: Built with performance in mind, 3DTilt ensures that the effect is smooth without compromising the app's performance.

Installation

To start using 3DTilt in your project, you can install it via npm or yarn:

npm install 3dtilt
Enter fullscreen mode Exit fullscreen mode

or

yarn add 3dtilt
Enter fullscreen mode Exit fullscreen mode

Basic Usage

Once installed, you can import and use the 3DTilt component in your React application:

import React from 'react';
import ThreeDTilt from '3dtilt';

const TiltedCard = () => {
  return (
    <ThreeDTilt>
      <div className="card">
        <h2>Interactive 3D Tilt</h2>
        <p>This card has a 3D tilt effect!</p>
      </div>
    </ThreeDTilt>
  );
};

export default TiltedCard;
Enter fullscreen mode Exit fullscreen mode

Customizing the 3DTilt Effect

The 3DTilt component provides several props to customize the tilt effect:

  • perspective: Controls the depth of the 3D effect. A higher value gives a more subtle tilt.
  • maxTilt: Defines the maximum tilt angle in degrees.
  • scale: Determines the scale of the element when the tilt effect is applied.
  • speed: Sets the speed of the tilt animation.
  • easing: Applies a custom easing function for a smooth animation transition.
  • transition: Enables the transition effect when the mouse enters and leaves the element.

Here’s an example of a customized 3DTilt component:

import React from 'react';
import ThreeDTilt from '3dtilt';

const HeroImage = ({ profile }) => {
  return (
    <ThreeDTilt
      options={{
        perspective: 1000,
        maxTilt: 10,
        scale: 1,
        speed: 300,
        easing: 'cubic-bezier(.03,.98,.52,.99)',
        transition: true,
      }}
      className='hero-image-wrapper'
    >
      <div>
        <img
          className='hero-image-fluid'
          src={profile?.profilePicture.url}
          alt='hero main image'
        />
      </div>
    </ThreeDTilt>
  );
};

export default HeroImage;

Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

Here are some practical scenarios where you might use the 3DTilt component:

  1. Product Cards: Showcase product images and details with an interactive 3D effect to grab users' attention.
  2. Call-to-Action Buttons: Make your CTAs stand out with a subtle 3D tilt that encourages clicks.
  3. Portfolio Items: Add a dynamic tilt effect to portfolio pieces to create a modern, engaging presentation.

Conclusion

The 3DTilt component is a versatile and powerful tool for adding interactive 3D tilt effects to your React applications. Whether you're enhancing product cards, buttons, or entire sections, 3DTilt can help you create a more engaging user experience with minimal effort.

Feel free to check out the 3DTilt GitHub repository for more details, and give it a try in your next project!

Top comments (0)