Three.js Transform Gizmos (TransformControls with Snapping & Undo)

Interactive translate, rotate and scale gizmos in Three.js with TransformControls, including click selection, world/local space, configurable snapping, W/E/R/Q keyboard shortcuts, frame-selection and an undo stack.

Key topics: three.js transformcontrols, gizmo, translate rotate scale, snapping, editor, undo

What this code does

- TransformControls: drag axis arrows, rotation rings or scale handles; since r169 the gizmo is added via `getHelper()`.
- Selection: clicking (not dragging) raycasts the objects and attaches the gizmo to the hit.
- Snapping: translation, rotation (degrees) and scale snaps quantize changes for precise layout.
- Spaces: world-space handles stay axis-aligned, and local-space handles follow the object's rotation.
- Undo: the transform is captured when a drag starts and pushed to a stack when it ends; Ctrl+Z restores it.
- Orbit Hand-off: orbiting is disabled while dragging, via the `dragging-changed` event.
- Controls: mode (W/E/R), space (Q), snapping and snap sizes, gizmo size and undo (F frames the selection).

Continue Learning

JavaScript Implementation

ES6+
import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js'

const gizmo = new TransformControls(camera, renderer.domElement)
scene.add(gizmo.getHelper())                 // r169+: the controls are not an Object3D
gizmo.attach(mesh)
gizmo.setTranslationSnap(0.5)
gizmo.setRotationSnap(THREE.MathUtils.degToRad(15))
gizmo.addEventListener('dragging-changed', (e) => { orbit.enabled = !e.value })
window.addEventListener('keydown', (e) => {
  if (e.key === 'w') gizmo.setMode('translate')
  if (e.key === 'e') gizmo.setMode('rotate')
  if (e.key === 'r') gizmo.setMode('scale')
})