Skip to content

Codemaine/React-Mesh-Gradient

 
 

Repository files navigation

Stargazers Issues MIT License LinkedIn


Logo

React Mesh Gradient

Create beautiful and interactive gradients with React.JS
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Current props available
  5. Roadmap
  6. Contributing
  7. License
  8. Contact
  9. Acknowledgments

About The Project

This package is a revamp of the archived react-mesh-gradient package.

Example of a Wireframe Mesh Gradient Example of a regular gradient

(back to top)

Built Using:


React Three Vite Rollup

(back to top)

Getting Started

Utilizing React Mesh Gradient is incredibly simple. Simply install the package and you can begin using the renderer in any of your components

Prerequisites

  • npm
    npm install npm@latest -g

Installation

  1. Install the package
    npm install react-mesh-gradient
  2. In the jsx/tsx file you wish to include the gradient:
    import { MeshGradientRenderer } from 'react-mesh-gradient';
    
    function App() {
     <MeshGradientRenderer
         className="gradient"
         colors={[
             "#C3E4FF", 
             "#6EC3F4", 
             "#EAE2FF", 
             "#B9BEFF", 
             "#B3B8F9"
         ]}
     />
    } 

(back to top)

Usage

There are a few different ways to use React Mesh Gradient. You have complete control over which colors are used, the speed of the gradient, and how it presents itself.

Wireframe mode

A cool way to present the gradient is to display the gradient as a wireframe. This gives the gradient an incredible 3D look.

const Gradient = () => {
    return (
        <MeshGradientRenderer
            colors={[
                "#C3E4FF", 
                "#6EC3F4", 
                "#EAE2FF", 
                "#B9BEFF", 
                "#B3B8F9"
            ]}
            wireframe={true}
        />
    );
}

Adding interactivity

All of the props on the gradient can be set to state variables which allows them to become interactive and change based on external input.

Here, we toggle wireframe mode everytime a user clicks a button:

import { useState } from 'react';

const ToggleWireframeGradient = () => {
    const [isWireframe, setIsWireframe] = useState(false);

    return (
        <div>
            <MeshGradientRenderer
                colors={[
                    "#C3E4FF", 
                    "#6EC3F4", 
                    "#EAE2FF", 
                    "#B9BEFF", 
                    "#B3B8F9"
                ]}
            />
            <button onClick={() => setIsWireframe(!isWireframe)}>Toggle Wireframe</button>
        </div>
    );
}

Custom Pointer Events

The Gradient Renderer exposes various pointer events for you to use to cause various interactions with the gradient. These events are:

  • onGradientClick
  • onGradientContextMenu
  • onGradientDoubleClick
  • onGradientWheel
  • onGradientPointerUp
  • onGradientPointerDown
  • onGradientPointerOver
  • onGradientPointerOut
  • onGradientPointerEnter
  • onGradientPointerLeave
  • onGradientPropsUpdate
  • onGradientPointerMove

These event functions are passed the THREE.Event that occurs on these events, along with data from the native browser event.

Using these functions, we can do things like cycle between colors when a user clicks the gradient

const GradientClick = () => {
    const palettes = [
        ["#F9B409", "#F9D16A", "#2A687A", "#72A25E", "#C3B49E"],
        ["#C3E4FF", "#6EC3F4", "#EAE2FF", "#B9BEFF", "#B3B8F9"],
        ["#69D2E7", "#A7DBD8", "#E0E4CC", "#F38630", "#FA6900"],
        ["#FE4365", "#FC9D9A", "#F9CDAD", "#C8C8A9", "#83AF9B"]
  ];

  const [colorIndex, setColorIndex] = useState(0);

  return (
    <MeshGradientRenderer
        colors={palettes[colorIndex]}
        onGradientClick={() => setColorIndex(colorIndex === palettes.length - 1 ? 0 : colorIndex + 1)}
    />
  )
}

Pausing the gradient

The gradient can be paused by setting the paused prop to true. This is useful if you want to pause the gradient when a user is interacting with another part of your application.

import { useState } from 'react';

const PausedGradient = () => {
    const [isPaused, setIsPaused] = useState(false);

    return (
        <div>
            <MeshGradientRenderer
                colors={[
                    "#C3E4FF", 
                    "#6EC3F4", 
                    "#EAE2FF", 
                    "#B9BEFF", 
                    "#B3B8F9"
                ]}
                paused={isPaused}
            />
            <button onClick={() => setIsPaused(!isPaused)}>{isPaused ? "Resume" : "Pause"}</button>
        </div>
    );
}

Pausing when not in view

The gradient can be paused when it is not in view by setting the pauseWhenNotInView prop to true. This is useful for saving resources when the gradient is not visible to the user.

const PausedGradient = () => {
    return (
        <div>
            <MeshGradientRenderer
                colors={[
                    "#C3E4FF", 
                    "#6EC3F4", 
                    "#EAE2FF", 
                    "#B9BEFF", 
                    "#B3B8F9"
                ]}
                pauseWhenNotInView={true}
            />
        </div>
    );
}

Pausing after a period of inactivity

The gradient can be paused after a period of inactivity by setting the idleTime prop to the number of seconds to wait before pausing the gradient. This is useful for saving resources when the user is not interacting with the page.

const PausedGradient = () => {
    return (
        <div>
            <MeshGradientRenderer
                colors={[
                    "#C3E4FF", 
                    "#6EC3F4", 
                    "#EAE2FF", 
                    "#B9BEFF", 
                    "#B3B8F9"
                ]}
                idleTime={5}
            />
        </div>
    );
}

Loading

The gradient has a loading state to show a loading overlay while it's being prepared. This is useful for providing feedback when gradients are heavy or when you want to highlight initialisation.

import { useState, useEffect } from 'react';

const LoadingGradient = () => {
    const [loading, setLoading] = useState(true);

    useEffect(() => {
        const timer = setTimeout(() => setLoading(false), 2000); // simulate loading
        return () => clearTimeout(timer);
    }, []);

    return (
        <MeshGradientRenderer
            colors={["#C3E4FF", "#6EC3F4", "#EAE2FF", "#B9BEFF", "#B3B8F9"]}
            loading={loading}
            paused={loading} // optional: pause gradient while loading
        />
    );
}

This package is an improved version of the archived react-mesh-gradient package.

(back to top)

Current props available

colors (required) - Array of colors to use represented as a hex string.

wireframe - Whether or not the gradient should be rendered in wireframe mode.

speed - The speed at which the gradient should move. The speed should be a number between 0 and 1.

paused - Whether or not the gradient is paused.

pauseWhenNotInView - Whether or not to pause the gradient when it is not in view.

idleTime - The time in seconds to wait before pausing the gradient due to inactivity.

loading - Boolean indicating whether the gradient is loading. While true, you can display a loading overlay.

backgroundColor - The background color of the gradient. The color should be in hex string.

backgroundOpacity - The opacity of the background. The opacity should be a number between 0 and 1.

onGradientClick - Click handler for the gradient. Will be called with the native Three.JS Event object.

onGradientContextMenu - Context menu handler for the gradient. Will be called with the native Three.JS Event object.

onGradientDoubleClick - Double click handler for the gradient. Will be called with the native Three.JS Event object.

onGradientWheel - Wheel handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerUp - Pointer up handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerDown - Pointer down handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerOver - Pointer over handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerOut - Pointer out handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerEnter - Pointer enter handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerLeave - Pointer leave handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPointerMove - Pointer move handler for the gradient. Will be called with the native Three.JS Event object.

onGradientPropsUpdate - Update handler for the gradient. Will be called with the native Three.JS Event object.

(back to top)

Roadmap

  • Allow for a customizable # of colors (right now you must use 5)

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Jermaine Antwi - @Codemaine

Project Link: https://github.com/Codemaine/React-Mesh-Gradient

(back to top)

Acknowledgments

(back to top)

About

Generate interactive mesh gradients using React.JS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 61.4%
  • JavaScript 14.1%
  • GLSL 12.2%
  • CSS 10.5%
  • HTML 1.8%