Create beautiful and interactive gradients with React.JS
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
This package is a revamp of the archived react-mesh-gradient package.
Utilizing React Mesh Gradient is incredibly simple. Simply install the package and you can begin using the renderer in any of your components
- npm
npm install npm@latest -g
- Install the package
npm install react-mesh-gradient
- 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" ]} /> }
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.
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}
/>
);
}
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>
);
}
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)}
/>
)
}
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>
);
}
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>
);
}
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>
);
}
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.
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.
- 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).
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Jermaine Antwi - @Codemaine
Project Link: https://github.com/Codemaine/React-Mesh-Gradient
- React Three Fiber
- Yuri Artiukh for the inspiration and shader tutorials