Implementing a Robust Undo/Redo Mechanism in a 3D Application with Redux

Chamal Lakshika Perera
5 min readNov 4, 2023

--

Introduction

In the world of interactive 3D applications, the ability to undo and redo actions is not just a convenience — it’s a necessity. Whether tweaking the scale, rotation, position, or color of a 3D object, users expect the freedom to experiment and the flexibility to revert changes. In this article, we’ll explore how to implement a sophisticated Undo/Redo mechanism in a 3D application using Redux.

Prerequisites

Before we delve into the intricacies of the Undo/Redo mechanism, it’s important to have a foundational 3D application in place. For the purpose of this guide, we’ll be using an existing React and Three.js project as our starting point. The repository can be found at chamallakshika09/react-threejs. This project provides a simple yet effective setup of a 3D environment using React and Three.js, along with a set of controls for manipulating a 3D cube object.

If you haven’t already set up your environment, clone the repository and follow the instructions to get the basic 3D application running. Our focus will be on extending this application with a sophisticated Undo/Redo feature, leveraging Redux for state management. Ensure that you’re comfortable with the existing codebase, as we’ll be building upon the controls and state management already implemented in this repository.

Understanding the Application Structure

Before we begin, let’s set the stage. Our application is a 3D editor powered by Three.js, embellished with a React front-end, and its state managed by Redux. Users can manipulate a 3D cube, altering its scale, rotation, position, and color through a series of controls. Our goal is to give users the power to undo or redo any of these changes seamlessly.

State Management with Redux

In Redux, the state of our application is stored in a single immutable object. For our 3D editor, the relevant slices of this state are cube-slice, which holds the properties of the cube (like scale, rotation, position, and color), and history-slice, which will soon become the backbone of our Undo/Redo mechanism.

Implementing History Slice

Our history-slice is a Redux slice designed to record each state change. It keeps an array of past states, allowing us to travel back in time. Here’s a simplified version of our history slice:

import { createSlice } from '@reduxjs/toolkit';

const initialState = {
past: [],
future: [],
};

const historySlice = createSlice({
name: 'history',
initialState,
reducers: {
addHistory: (state, action) => {
state.past.push(action.payload);
},
undo: (state) => {
const previous = state.past.pop();
if (previous) {
state.future.push(previous);
}
},
redo: (state) => {
const next = state.future.pop();
if (next) {
state.past.push(next);
}
},
},
});

export const { addHistory, undo, redo } = historySlice.actions;
export default historySlice.reducer;

The Core of Undo/Redo

At the heart of our Undo/Redo functionality is an action stack, represented by the past and future arrays in our history-slice. When a user performs an action, like scaling the cube, we push the previous state into past. When they undo, we pop from past and push into future. Redoing simply reverses the process.

To visualize the flow of actions within our undo/redo mechanism, refer to the following flowchart. It delineates the sequence of user interactions, state updates, and middleware interventions that culminate in the ability to undo and redo actions in our application.

Undo/Redo Mechanism in Redux

Integrating Undo/Redo with Controls

Each control component in our application, be it ScaleControl, RotationControl, PositionControl, or ColorControl, dispatches actions that update both the cube's state and the history. Here’s an example with ScaleControl:

const handleScaleChange = (axis) => (event, value) => {
dispatch(
addHistory({
oldValue: scale,
newValue: { ...scale, [axis]: value },
})
);
dispatch(setScale({ ...scale, [axis]: value }));
};

Enhancing Undo/Redo with Middleware

While the history-slice handles the recording of state changes, to seamlessly revert to a previous state, we'll employ Redux middleware. Middleware in Redux allows us to intercept actions before they reach the reducer, giving us a chance to perform additional logic. Let's create an undoRedoMiddleware to manage our Undo/Redo actions effectively.

Here’s how we can implement it:

import { undo, redo } from './history-slice';

const undoRedoMiddleware = (store) => (next) => (action) => {
if (action.type === undo.type) {
const state = store.getState();
const lastAction = state.history.past[state.history.past.length - 1];
if (lastAction) {
store.dispatch({ type: lastAction.type, payload: lastAction.oldValue });
}
} else if (action.type === redo.type) {
const state = store.getState();
const nextAction = state.history.future[state.history.future.length - 1];
if (nextAction) {
store.dispatch({ type: nextAction.type, payload: nextAction.newValue });
}
}

return next(action);
};

export default undoRedoMiddleware;

In the undoRedoMiddleware, we intercept the undo and redo actions. When an undo action is dispatched, we retrieve the last action from the past array and dispatch it with the old value, effectively reverting the state change. Similarly, for redo actions, we take the last action from the future array and apply the new value.

Integrating the Middleware

To integrate this middleware into our Redux store, we simply apply it when creating the store:

import { configureStore } from '@reduxjs/toolkit';
import cubeReducer from './cube-slice';
import historyReducer from './history-slice';
import undoRedoMiddleware from './undoRedoMiddleware';

export const store = configureStore({
reducer: {
cube: cubeReducer,
history: historyReducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(undoRedoMiddleware),
});

Handling Edge Cases

We need to be mindful of edge cases. For instance, if the user drags a slider rapidly, we don’t want to record every minute change. Throttling or debouncing our addHistory dispatches can mitigate this issue.

Performance Considerations

An extensive history can lead to performance bottlenecks. Strategies like capping the history stack size and implementing shallow comparisons when recording states can help maintain snappy performance.

Conclusion

With undoRedoMiddleware now in place, our Undo/Redo mechanism is robust and integrated seamlessly into our Redux workflow. It elegantly handles the complexity of state reversal without cluttering component logic, making our 3D application more intuitive and user-friendly.

By implementing this feature, we not only enhance user experience but also demonstrate the power of Redux middleware in managing complex state interactions. Whether you’re a seasoned developer or just starting out, incorporating such mechanisms can significantly improve the usability and functionality of your applications.

The ability to undo and redo actions is a feature users expect and rely on, and now you know how to implement it in your 3D applications. Use this guide as a starting point, and don’t be afraid to adapt the concepts to fit the unique requirements of your project.

Additional Resources

Complete Source Code

For those who are interested in seeing the complete implementation of the Undo/Redo mechanism as described in this article, the entire source code is available on GitHub. You can access the repository here: chamallakshika09/threejs-undo-redo. Feel free to clone the repository, explore the code, and experiment with it to further your understanding.

--

--

Chamal Lakshika Perera
Chamal Lakshika Perera

Written by Chamal Lakshika Perera

Tech Lead - love learning and talking about new technologies

No responses yet