Overview
This article discusses the use of the react-three-paper library in creating React-based apps. It highlights the advantages of using this library, such as easily porting Vanilla-JS scripts to React, separating UI logic from the core ThreeJS app, and the small size of the library. The article also mentions the position awareness feature of the library, where the canvas pauses the render loop when it is out of the viewport and resumes it when it is back in the viewport, which helps with performance. The installation and usage of the react-three-paper library are also explained.
Features
- Port Vanilla-JS scripts to React: Easily convert Vanilla-JS examples to React using the react-three-paper library.
- Separate UI logic from ThreeJS app: Keep your UI logic separate from the core ThreeJS app using react-three-paper.
- Small size: The react-three-paper library is tiny in size.
- Position awareness: The canvas in react-three-paper knows when it is out of the viewport and pauses the render loop, improving performance.
Installation
To install the react-three-paper library, you need to have react version 16.8.0 or higher. Then you can import the Paper component and use it in your script.
import { Paper } from 'react-three-paper';
function MyComponent() {
return (
<Paper script={myThreeJSScript} />
);
}
The script
prop accepts a function that should look like this:
function myThreeJSScript(canvas) {
return new Promise((resolve) => {
// Your render loop without requestAnimationFrame
const render = () => {
// render loop logic
};
// An optional cleanup function
const cleanup = () => {
// cleanup logic
};
resolve({ render, cleanup });
});
}
You can also customize the behavior and style of the canvas by using different props like style
, onExit
, onEntry
, and onError
.
Summary
This article introduces the react-three-paper library and its features. It explains how to install and use the library in your React-based apps. The article highlights the advantages of using react-three-paper, such as easily porting Vanilla-JS scripts to React and the position awareness feature of the library. Overall, react-three-paper provides a convenient way to integrate ThreeJS into React applications.