Create beautiful and interactive gradients with React.JS
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
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 @johnn-e/React-Mesh-Gradient
- 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" ]} /> }
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)}
/>
)
}
For more examples, please refer to the Documentation (coming soon)
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.
- 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).
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.
Johnny Leek - LinkedIn
Project Link: https://github.com/JohnnyLeek1/React-Mesh-Gradient
- React Three Fiber
- Yuri Artiukh for the inspiration and shader tutorials