Function attachControl
-
attachControl(
camera: ArcRotateCamera,
canvas: HTMLCanvasElement,
scene?: SceneContext,
options?: AttachControlOptions,
): () => voidParameters
- camera: ArcRotateCamera
- canvas: HTMLCanvasElement
Optionalscene: SceneContextOptionaloptions: AttachControlOptions
Returns () => void
A cleanup function that removes all canvas listeners and the
_beforeRenderinertia hook. Safe to call multiple times.
Attach orbit/zoom/pan controls to an ArcRotateCamera. Matches Babylon.js ArcRotateCameraPointersInput behavior with inertia:
Mouse and pen button actions can be configured independently through AttachControlOptions.pointerMappings. Touch and wheel behavior are fixed so remapping desktop pointer buttons cannot alter mobile gestures. Keyboard input is a separate opt-in installed by enableArcRotateKeyboardControls; mappings and sensitivities are configured through AttachControlOptions.keyboard.
Input handlers accumulate into the camera's inertial offset properties. Inertia is applied each frame via scene._beforeRender (the engine's render loop); a scene is required for inertia, there is no standalone rAF fallback.
Orbit/zoom limits are entirely opt-in via setCameraLimits; the camera self-clamps in its setters, so this loop carries no limit code.
Camera stays plain data — this function reads/writes its properties.
Lifecycle / cleanup (important)
The returned function detaches everything this call attached: it removes the canvas DOM listeners (pointer/wheel/contextmenu/touch/gesture plus any listeners owned by installed input modules) and, when a
scenewas supplied, removes its_beforeRenderinertia hook. It is idempotent — calling it more than once is safe (the hook is removed only if still present, and removing a DOM listener twice is a no-op).The controls are not automatically tied to the scene's lifetime. Passing a
sceneonly enables inertia (it registers the per-frame hook); it does not register this cleanup for scene disposal. Because the DOM listeners live on thecanvasrather than on the scene, disposeScene clears scene-owned state (including_beforeRender) but does not remove the canvas listeners added here. The caller owns the controls' lifetime and must detach them explicitly:To tie the controls to the scene so
disposeScene(scene)also detaches them, register the returned cleanup with onSceneDispose: