Index of every page on this site: llms.txt
All skills
impertio-studio avatar

/threejs-syntax-controls

@6c190f0
Run it yourself

Use when adding camera controls to a Three.js scene: orbit, pan, zoom, fly, first-person, or object manipulation. Prevents the common mistake of forgetting controls.update() in the render loop, not calling dispose(), or mixing controls. Covers OrbitControls, MapControls, FlyControls, PointerLockControls, TransformControls. Keywords: OrbitControls, controls, camera controls, orbit, pan, zoom, MapControls, FlyControls, PointerLockControls, TransformControls, rotate view, move camera, mouse controls, drag to rotate.

Use this Skill: https://skilld.dev/gh/impertio-studio/three.js-claude-skill-package/threejs-syntax-controls

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesanti-patterns.md

≈1.9k tokens on demand. Your agent reads this file only when SKILL.md points to it.

threejs-syntax-controls -- Anti-Patterns

Anti-Pattern 1: Forgetting controls.update() with Damping

WRONG:

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;

function animate() {
  requestAnimationFrame(animate);
  // Missing controls.update() !
  renderer.render(scene, camera);
}

What happens: The camera moves while the user drags, but snaps to a stop the instant the user releases the mouse. The damping/inertia effect NEVER activates. With autoRotate, the camera NEVER rotates.

CORRECT:

function animate() {
  requestAnimationFrame(animate);
  controls.update(); // ALWAYS call when enableDamping or autoRotate is true
  renderer.render(scene, camera);
}

Anti-Pattern 2: Not Calling dispose()

WRONG:

function switchToFlyMode() {
  // Just replace controls without cleanup
  controls = new FlyControls(camera, renderer.domElement);
}

What happens: The old OrbitControls' event listeners (pointermove, pointerdown, pointerup, wheel, keydown, contextmenu) remain active on renderer.domElement. This causes:

  • Memory leaks from accumulated listeners
  • Ghost interactions where the old controls still respond to input
  • Conflicting camera movement from two control systems

CORRECT:

function switchToFlyMode() {
  controls.dispose(); // ALWAYS dispose before replacing
  controls = new FlyControls(camera, renderer.domElement);
}

Anti-Pattern 3: Attaching to document Instead of renderer.domElement

WRONG:

const controls = new OrbitControls(camera, document.body);

What happens: Controls capture pointer events on the entire page. Any UI elements (buttons, sliders, dropdowns) above or beside the canvas become unusable because the controls intercept their events. Scroll events on the page trigger camera zoom.

CORRECT:

const controls = new OrbitControls(camera, renderer.domElement);

ALWAYS pass renderer.domElement so controls ONLY respond to events on the canvas.


Anti-Pattern 4: Mixing Two Camera Controls Without Disabling

WRONG:

const orbitControls = new OrbitControls(camera, renderer.domElement);
const flyControls = new FlyControls(camera, renderer.domElement);

function animate() {
  requestAnimationFrame(animate);
  orbitControls.update();
  flyControls.update(delta);
  renderer.render(scene, camera);
}

What happens: Both controls fight over the camera's position and rotation every frame. The camera jitters, teleports, or moves unpredictably. Each control overwrites what the other set.

CORRECT:

let activeControls = orbitControls;
flyControls.enabled = false; // or don't create until needed

function switchControls(type) {
  activeControls.dispose();
  if (type === 'fly') {
    activeControls = new FlyControls(camera, renderer.domElement);
  } else {
    activeControls = new OrbitControls(camera, renderer.domElement);
  }
}

NEVER have two camera controls active simultaneously on the same camera. ALWAYS dispose or disable one before enabling another.


Anti-Pattern 5: Not Disabling OrbitControls During TransformControls Drag

WRONG:

const orbitControls = new OrbitControls(camera, renderer.domElement);
const transformControls = new TransformControls(camera, renderer.domElement);
scene.add(transformControls);
transformControls.attach(cube);
// No dragging-changed listener!

What happens: When the user drags a TransformControls gizmo handle, OrbitControls also responds to the drag. The camera orbits while the object moves, making precise positioning impossible.

CORRECT:

transformControls.addEventListener('dragging-changed', (event) => {
  orbitControls.enabled = !event.value;
});

ALWAYS listen to dragging-changed and disable camera controls while event.value is true.


Anti-Pattern 6: Not Adding TransformControls to the Scene

WRONG:

const transformControls = new TransformControls(camera, renderer.domElement);
transformControls.attach(cube);
// Forgot scene.add(transformControls) !

What happens: The gizmo handles are invisible. The TransformControls object exists and processes events, but nothing renders on screen. The user cannot see or click the translate/rotate/scale handles.

CORRECT:

const transformControls = new TransformControls(camera, renderer.domElement);
scene.add(transformControls); // MUST add to scene for gizmo visibility
transformControls.attach(cube);

Anti-Pattern 7: Calling PointerLockControls.lock() Without User Gesture

WRONG:

const controls = new PointerLockControls(camera, renderer.domElement);
controls.lock(); // Called immediately on page load

What happens: The browser silently rejects the pointer lock request. The Pointer Lock API requires a user-initiated event (click, keydown). The console shows no error, but the pointer is never locked and isLocked remains false.

CORRECT:

const controls = new PointerLockControls(camera, renderer.domElement);

renderer.domElement.addEventListener('click', () => {
  controls.lock(); // ALWAYS from a user gesture
});

Anti-Pattern 8: Omitting delta in FlyControls.update()

WRONG:

function animate() {
  requestAnimationFrame(animate);
  controls.update(); // Missing delta argument!
  renderer.render(scene, camera);
}

What happens: FlyControls.update() receives undefined as delta, resulting in NaN or zero movement. The camera either freezes completely or moves at an unpredictable speed depending on internal fallback behavior.

CORRECT:

const clock = new THREE.Clock();

function animate() {
  requestAnimationFrame(animate);
  const delta = clock.getDelta();
  controls.update(delta); // ALWAYS pass delta for FlyControls
  renderer.render(scene, camera);
}

Anti-Pattern 9: Setting target Without Calling update()

WRONG:

controls.target.set(10, 0, 5);
// Camera still looks at (0, 0, 0) until next user interaction

What happens: Changing target only sets the value. The camera does NOT re-orient until update() is called. If enableDamping is false and the render loop does not call update(), the camera appears stuck at the old target.

CORRECT:

controls.target.set(10, 0, 5);
controls.update(); // ALWAYS call after programmatic target changes

Anti-Pattern 10: Using OrbitControls with Orthographic Camera Zoom Properties on Perspective Camera

WRONG:

const camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
const controls = new OrbitControls(camera, renderer.domElement);
controls.minZoom = 0.5;  // Has NO effect on PerspectiveCamera
controls.maxZoom = 5.0;  // Has NO effect on PerspectiveCamera

What happens: minZoom and maxZoom ONLY affect OrthographicCamera.zoom. For PerspectiveCamera, these properties are silently ignored. The camera zooms without any limits.

CORRECT:

// For PerspectiveCamera -- use distance limits
controls.minDistance = 2;
controls.maxDistance = 50;

// For OrthographicCamera -- use zoom limits
// controls.minZoom = 0.5;
// controls.maxZoom = 5.0;

ALWAYS use minDistance/maxDistance for PerspectiveCamera and minZoom/maxZoom for OrthographicCamera. NEVER mix them up.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts5mo3 checks · Risk SAFE
  • Gen Agent Trust Hub5mo

    The skill provides comprehensive documentation, API references, and code examples for implementing Three.js camera controls. It focuses on promoting library best practices, such as proper event listener cleanup and resource management, with no detected security risks.

  • Socket5mo

    No alerts

  • Snyk5mo

    Risk: LOW · No issues

Signed by skilld at 6c190f0. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago
compatibility
Designed for Claude Code. Requires Three.js r160+.
metadata
{
  "author": "OpenAEC-Foundation",
  "version": "1.0"
}

README badge

README badge for impertio-studio/three.js-claude-skill-package/threejs-syntax-controls