Skip to content

JohnnyLeek1/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

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 @johnn-e/React-Mesh-Gradient
  2. In the jsx/tsx file you wish to include the gradient:
    import { MeshGradientRenderer } from '@johnn-e/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)}
    />
  )
}

For more examples, please refer to the Documentation (coming soon)

(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.

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

  • Work on reducing bundle size
  • 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

Johnny Leek - LinkedIn

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

(back to top)

Acknowledgments

(back to top)