From our sponsor: Meco is a distraction-free space for reading and discovering newsletters, separate from the inbox.
We will be looking at how to pull apart SVGs in 3D space with Three.js and React, using abstractions that allow us to break the scene graph into reusable components.
React and Three.js, what’s the problem?
My background in the past had more to do with front-end work than design, and React has been my preferred tool for a couple of years now. I like it because it pretty much maps the way i think. The ideas in my head are puzzle-pieces, which in React turn to composable components. It makes prototyping faster, and from a visual/design standpoint, it’s even fun, because it allows you to play around without repercussions. If everything is a self-contained lego-brick, you can rip it out, place it here, or there, and observe the result from different angles and perspectives. Especially for visual coding this can make a difference.
The problems that arise when handling programming tasks in an imperative way are always the same. Once we have created a sufficiently complex dependency-graph then things tend to be cobbled together, which causes the whole to be less flexible. Adding, updating or deleting items in sync with state and other operations can get complex. Orchestrating animations makes it even worse, because now you need to await animations to conclude before you continue with other operations and so on. Without a clear component-model it can be a reasonable challenge to keep it all together.
We run into this when working with user interfaces, as well as when creating scenes with Three.js, which can lend to especially unwieldy structures as it forces us to create a ton of objects that we have to track, mutate and manage. But React can solve that, too.
Think of React as a standard that defines what a component is and how it functions. React needs a so called “reconciler” to tell it what to do with these components and how to render them into a host. The browsers dom is a host, hence the react-dom package, which instructs React about the dom. React-native is another one you may be familiar with, but really there are dozens, reaching into all kinds of platforms, from AR, VR, console shells to, you guessed it, Three.js. The reconciler we will be using in this tutorial is called react-three-fiber, it renders components into a Three.js scene graph. Think of it as a portal into Three.js.
Let’s build!
Setting up the scene
Our portal into Three.js will be react-three-fiber’s “Canvas” component. Everything that goes in there will be cast into Three.js-native objects. The following will create a responsive canvas with some lights in it.
function App() {
return (
<Canvas>
<ambientLight intensity={0.5} />
<spotLight intensity={0.5} position={[300, 300, 4000]} />
</Canvas>
)
}
Converting SVGs into shapes
Our goal is to extract SVG paths, once we have that we can display them in all sorts of interesting ways. We will be using fairly simple sketches for that, they won’t create many layers and the effect will be less pronounced.
In order to transform SVGs into shape geometries we use Three.js’s SVGLoader. The following will give us a nested array of objects that contains the shapes and colors. We collect the index, too, which we will be using to offset the z-vector.
const svgResource = new Promise(resolve =>
new loader().load(url, shapes =>
resolve(
flatten(
shapes.map((group, index) =>
group.toShapes(true).map(shape => ({ shape, color: group.color, index }))
)
)
)
)
)
Next we define a “Shape” component which renders a single shape. Each shape is offset 50 units by its own index.
function Shape({ shape, position, color, opacity, index }) {
return (
<mesh position={[0, 0, index * 50]}>
<meshPhongMaterial attach="material" color={color} />
<shapeBufferGeometry attach="geometry" args={[shape]} />
</mesh>
)
}
All we are missing now is a component that maps through the shapes we have created. Since the resource we have created is a promise we have to await its resolved state. Once it has loaded, we wrote it into the local component state and forward each shape to the “Shape” component we have just created.
function Scene() {
const [shapes, set] = useState([])
useEffect(() => void svgResource.then(set), [])
return (
<group>
{shapes.map(item => <Shape key={item.shape.uuid} {...item} />)}
</group>
)
}
This is it, our canvas shows an offset SVG.
Adding animations
If you wanted to animate Three.js you would most likely do it manually and use tools like GSAP. And since we want to animate elements that go in and out you need to have some system in place that orchestrates it, which is not an easy task to pull off.
Here comes the nice part, we are rendering React components and that opens up a lot of possibilities. We can use pretty much everything that exists in the eco system, including animation and transition tools. In this case we use react-spring.
Really all we need to do is convert out shapes into a transition-group. A transition group is something that watches state for changes and helps to retain and transition old state until it can be safely removed. In react-springs case it is called “useTransition”. It takes the original data, shapes in this case, keys in order to identify changes in the data-set, and a couple of lifecycles in which we can define what happens when state is added, removed or changed.
The following takes care of everything. If shapes are added, they will transition into the scene in a trailed motion. If shapes are removed, they will transition out.
const transitions = useTransition(shapes, item => item.shape.uuid, {
from: { position: [0, 50, -200], opacity: 0 },
enter: { position: [0, 0, 0], opacity: 1 },
leave: { position: [0, -50, 10], opacity: 0 },
})
return (
<group>
{transitions.map(({ item, key, props }) => <Shape key={key} {...item} {...props} />)}
</group>
)
useTransition creates an array of objects which contain generated keys, the data items (our shapes) and animated properties. We spread everything over the Shape component. Now we just need to prepare that component to receive animated values and we are done.
react-spring exports a little helper called “animated”, as well as a shortcut called “a”. If you extend any element with it, it will be able to handle these properties. Basically, if you had a div, it would become a.div, if you had a mesh, it now becomes a.mesh.
I hope you had fun! You will find detailed explanations for everything in the respective docs for react-three-fiber and react-spring. The full code for the original demo can be found here.