There are numerous methods available to incorporate WebGL effects into websites, but creating a responsive, accessible, and easily disabled experience for mobile devices can be challenging. In many cases, it is necessary to have a more flexible approach that allows for alternative versions of the website or the ability to change the requirement for WebGL usage. At 14islands, we have adopted a Progressive Enhancement approach and developed a library called r3f-scroll-rig to assist with this. This library enables the use of semantic markup and CSS to create responsive layouts and easily enhance them with WebGL effects.
In this tutorial, you will learn how to extend your React website with WebGL items using react-three-fiber that are synchronized with your website’s layout during scrolling. We will be using our open-source library, r3f-scroll-rig, to achieve this. The tutorial will cover the following steps:
1. Adding the r3f-scroll-rig library: The scroll-rig library is compatible with most React frontend frameworks. We will import the necessary components and styles from the library and include them in our app. This will enable smooth scrolling with Lenis and provide a position fixed canvas that covers the viewport.
2. Enhancing images to render with WebGL: We will use the ScrollScene component to track a DOM image element and render a Threejs mesh with the same scale and position. This will allow the mesh to move in sync with the scrollbar. We will also hide the HTML image when the SmoothScrollbar is enabled.
3. Adding a 3D model tied to the layout and reacts to scroll events: We will use the ScrollScene component again to track a DOM element and add a 3D model that is tied to the layout. The model will react to scroll events and move accordingly.
4. Enhancing text with WebGL: Similar to the previous steps, we will use the ScrollScene component to track a DOM text element and replace it with WebGL text using the WebGLText component from the powerups import. We will also apply a custom material to the text to add a wobbling effect.
By following this tutorial and using the r3f-scroll-rig library, you will be able to easily incorporate WebGL effects into your React website in a responsive and accessible manner.