Camera controls for a custom UI
Build camera controls for a modular room planner. Let customers switch projection, zoom, and bookmark a view. When they open a details drawer, fit the layout into the remaining space.
Use window.mimeeqApp.observers.camera for live camera state and window.mimeeqApp.actions for changes. The example keeps the built-in scene editing controls and supplies its own zoom controls.
Complete example
Replace YOUR_MODULAR_SHORT_CODE with the short code for a modular embed that renders inline. Fit works once the scene contains visible product geometry.
<style>
#camera-stage {
position: relative;
overflow: hidden;
}
#camera-stage mmq-embed {
display: block;
}
#details-drawer {
position: absolute;
inset: 0 0 0 auto;
width: min(240px, 35%);
box-sizing: border-box;
padding: 12px;
background: white;
color: #202020;
z-index: 2;
}
</style>
<fieldset id="camera-controls" disabled>
<legend>Camera</legend>
<label>
Projection
<select id="camera-projection">
<option value="perspective">Perspective</option>
<option value="orthographic">Orthographic</option>
</select>
</label>
<output id="view-mode"></output>
<button type="button" id="zoom-in">Zoom in</button>
<button type="button" id="zoom-out">Zoom out</button>
<button type="button" id="hold-zoom">Hold to zoom in</button>
<label>Zoom level <input id="zoom-level" type="range" step="any" /></label>
<label><input id="auto-rotate" type="checkbox" /> Auto-rotate</label>
<button type="button" id="pause-rotation">Pause rotation</button>
<button type="button" id="save-view">Save view</button>
<button type="button" id="restore-view" disabled>Restore view</button>
<button type="button" id="toggle-details" aria-controls="details-drawer" aria-expanded="false">
Toggle details
</button>
</fieldset>
<p id="camera-message" role="status"></p>
<div id="camera-stage">
<mmq-embed
short-code="YOUR_MODULAR_SHORT_CODE"
locale="en"
render-at-mount
hide-zoom-buttons></mmq-embed>
<aside id="details-drawer" hidden>
<h2>Layout details</h2>
<p>Use this space for notes about the layout.</p>
</aside>
</div>
<script>
document.addEventListener(
'mimeeq-app-loaded',
() => {
const { actions, observers } = window.mimeeqApp;
const { camera, status } = observers;
const controls = document.getElementById('camera-controls');
const projection = document.getElementById('camera-projection');
const view = document.getElementById('view-mode');
const slider = document.getElementById('zoom-level');
const autoRotate = document.getElementById('auto-rotate');
const hold = document.getElementById('hold-zoom');
const restore = document.getElementById('restore-view');
const toggle = document.getElementById('toggle-details');
const drawer = document.getElementById('details-drawer');
const message = document.getElementById('camera-message');
if (
!(controls instanceof HTMLFieldSetElement) ||
!(projection instanceof HTMLSelectElement) ||
!(slider instanceof HTMLInputElement) ||
!(autoRotate instanceof HTMLInputElement) ||
!(hold instanceof HTMLButtonElement) ||
!(restore instanceof HTMLButtonElement) ||
!(toggle instanceof HTMLButtonElement) ||
!view ||
!drawer ||
!message
)
return;
const zoomOptions = { pauseAutoRotate: true };
/** @type {{ position: NonNullable<ReturnType<typeof camera.position.getValue>>, target: NonNullable<ReturnType<typeof camera.target.getValue>>, projection: NonNullable<ReturnType<typeof camera.projectionMode.getValue>>, zoom: number } | null} */
let saved = null;
/** @type {{ id: number, press: Promise<void> } | null} */
let held = null;
let movingDrawer = false;
const render = () => {
const mode = camera.projectionMode.getValue();
const level = camera.zoomLevel.getValue();
const limits = camera.zoomLimits.getValue();
controls.disabled = !status.firstRender.getValue() || !status.settled.getValue() || !mode;
if (mode) projection.value = mode;
view.textContent = camera.viewMode.getValue() === 'top' ? 'Top view' : '';
autoRotate.checked = camera.autoRotateEnabled.getValue() === true;
restore.disabled = !saved;
toggle.disabled = movingDrawer;
if (limits?.min != null && limits.max != null && limits.min < limits.max && level != null) {
slider.min = String(limits.min);
slider.max = String(limits.max);
slider.value = String(level);
slider.disabled = false;
} else {
slider.disabled = true;
}
};
/** @param {() => Promise<void>} action */
const run = async (action) => {
try {
await action();
} catch (error) {
message.textContent =
error instanceof Error ? error.message : 'Could not update the camera.';
} finally {
render();
}
};
projection.addEventListener('change', () => {
const mode = projection.value;
if (mode === 'perspective' || mode === 'orthographic') {
void run(() => actions.setCameraProjectionMode(mode));
}
});
autoRotate.addEventListener('change', () => {
void run(() => actions.setCameraAutoRotate(autoRotate.checked));
});
document.getElementById('pause-rotation')?.addEventListener('click', () => {
void run(actions.pauseCameraAutoRotate);
});
// decreaseZoom shortens the eye-to-target distance (closer); increaseZoom lengthens it.
document.getElementById('zoom-in')?.addEventListener('click', () => {
void run(() => actions.decreaseZoom(undefined, zoomOptions));
});
document.getElementById('zoom-out')?.addEventListener('click', () => {
void run(() => actions.increaseZoom(undefined, zoomOptions));
});
slider.addEventListener('input', () => {
void run(() => actions.setZoom(slider.valueAsNumber, zoomOptions));
});
hold.addEventListener('pointerdown', (event) => {
if (held || event.button !== 0) return;
hold.setPointerCapture(event.pointerId);
held = {
id: event.pointerId,
press: run(() => actions.applyZoomInput('in', true, zoomOptions)),
};
});
/** @param {PointerEvent} event */
function endHold(event) {
if (!held || held.id !== event.pointerId) return;
const { press } = held;
held = null;
void run(async () => {
await press;
await actions.releaseZoomInput(zoomOptions);
});
}
hold.addEventListener('pointerup', endHold);
hold.addEventListener('pointercancel', endHold);
hold.addEventListener('lostpointercapture', endHold);
hold.addEventListener('click', (event) => {
if (event.detail === 0) void run(() => actions.decreaseZoom(undefined, zoomOptions));
});
document.getElementById('save-view')?.addEventListener('click', () => {
const position = camera.position.getValue();
const target = camera.target.getValue();
const mode = camera.projectionMode.getValue();
const zoom = camera.zoomLevel.getValue();
if (!position || !target || !mode || zoom == null) return;
saved = { position, target, projection: mode, zoom };
message.textContent = 'View saved.';
render();
});
restore.addEventListener('click', () => {
const bookmark = saved;
if (!bookmark) return;
void run(async () => {
await actions.setCameraProjectionMode(bookmark.projection);
await actions.setCameraPose({ position: bookmark.position, target: bookmark.target });
await actions.setZoom(bookmark.zoom, zoomOptions);
message.textContent = 'View restored.';
});
});
toggle.addEventListener('click', () => {
if (movingDrawer) return;
movingDrawer = true;
render();
void run(async () => {
const opening = drawer.hidden;
const wasLocked = camera.autoFitLocked.getValue() === true;
try {
if (!wasLocked) await actions.setCameraAutoFitLocked(true);
drawer.hidden = false;
const closed = 'translateX(100%)';
const open = 'translateX(0)';
await drawer.animate(
{ transform: opening ? [closed, open] : [open, closed] },
{ duration: 220, easing: 'ease-out' },
).finished;
} finally {
drawer.hidden = !opening;
toggle.setAttribute('aria-expanded', String(opening));
if (!wasLocked) await actions.setCameraAutoFitLocked(false);
}
const result = await actions.fitCameraToScene({
insets: { right: opening ? drawer.getBoundingClientRect().width : 0 },
durationMs: 300,
});
switch (result.status) {
case 'finished':
message.textContent = result.fits
? 'Layout framed.'
: 'The full layout could not fit.';
break;
case 'interrupted':
message.textContent = 'Framing was interrupted.';
break;
case 'skipped':
message.textContent = {
noCamera: 'No interactive camera is available.',
noContent: 'Add a module to frame the layout.',
locked: 'Automatic framing is locked.',
}[result.reason];
break;
}
}).finally(() => {
movingDrawer = false;
render();
});
});
const subscriptions = [
...[status.firstRender, status.settled].map((observer) => observer.subscribe(render)),
camera.projectionMode.subscribe(render),
camera.viewMode.subscribe(render),
camera.zoomLevel.subscribe(render),
camera.zoomLimits.subscribe(render),
camera.autoRotateEnabled.subscribe(render),
];
window.addEventListener(
'pagehide',
() => {
if (held)
void held.press.then(() => actions.releaseZoomInput(zoomOptions)).catch(console.error);
subscriptions.forEach((subscription) => subscription.unsubscribe());
},
{ once: true },
);
},
{ once: true },
);
</script>
<script src="https://cdn.mimeeq.com/read_models/embed/app-embed.js" async></script>
How it works
- Register the
mimeeq-app-loadedlistener ondocumentbefore loading the embed script. Wait for a ready scene and an interactive camera before enabling the controls. - Set projection with
setCameraProjectionMode(). The orthographic projection keeps parallel lines parallel. The top-view indicator comes fromcamera.viewMode, so it works with either projection. - Pass
{ pauseAutoRotate: true }to zoom actions to restart the rotation idle wait. UseapplyZoomInput('in', true, options)for a held button. Release withreleaseZoomInput(options)on pointer release or cancellation, using the same option. The ordinary zoom buttons passundefinedfor the step to use the default. - Read
zoomLevelandzoomLimitstogether to configure the slider, and pass its value tosetZoom(). Smaller values frame a tighter view. This slider requires two finite endpoints and disables itself when an endpoint is unbounded. - Toggle idle rotation with
setCameraAutoRotate(). CallpauseCameraAutoRotate()to restart its idle wait while keeping the setting enabled. - Lock auto-fit while the drawer slides. Release the lock this UI acquired before fitting. Pass the overlapping drawer width as a right inset in CSS pixels and await the fit result before reporting its outcome.
- Save the eye and target together, then restore them with
setCameraPose(). Keep the projection and zoom alongside the pose to restore framing, especially the orthographic scale. This bookmark lives in memory for the current page.
Observer payloads
Subscriptions emit { newValue, oldValue } and replay the current value immediately. The first emission can omit oldValue. Call getValue() for a synchronous read. Camera points and limits are copies. Every camera field can be null when there is no interactive camera.
| Observer field | Type when available | Meaning |
|---|---|---|
camera.projectionMode | 'perspective' | 'orthographic' | The active projection. |
camera.viewMode | 'top' | 'other' | 'top' means the view is within one degree of straight down. |
camera.autoRotateEnabled | boolean | Idle rotation is enabled, even while it is waiting to start. |
camera.autoFitLocked | boolean | Auto-fit is locked by the integration or the authored camera. |
camera.isAnimating | boolean | A finite camera transition is running. Idle rotation and gesture inertia are excluded. Use this to indicate a transition elsewhere in your UI. |
camera.position, camera.target | { x: number; y: number; z: number } | Eye position and look-at point in Y-up scene coordinates. |
camera.zoomLevel | number | Eye-to-target distance in perspective or half the visible height in orthographic projection, in scene units. |
camera.zoomLimits | { min: number | null; max: number | null } | Limits in the same units as zoomLevel. A null endpoint is unbounded. |
status.firstRender, status.settled | boolean each | Enable camera controls once the scene has drawn and the configurator has settled. |
Fit results and edge cases
Omitted inset edges default to zero. A fit defaults to 300 ms. Pass durationMs: 0 for an immediate fit.
| Fit result | Meaning |
|---|---|
{ status: 'finished', fits: true } | The fit finished and the content fits. |
{ status: 'finished', fits: false } | The viewport, insets, or camera limits prevent full framing. |
{ status: 'interrupted' } | The fit stopped before completion, for example because another motion replaced it or the camera was disposed. |
{ status: 'skipped', reason: 'noCamera' | 'noContent' | 'locked' } | There is no interactive camera, no visible geometry to frame, or the camera is locked for auto-fit. |
- The example overlays the drawer on the right edge of the viewport. If your drawer resizes the canvas instead, calculate the inset only for the area it covers inside the resized viewport.
- Camera limits can clamp a restored pose or zoom. Use the observers to show what the camera actually accepted. An authored camera lock can prevent a pose or projection change.
- A rotation pause is temporary. To keep the camera still after the idle wait, turn auto-rotate off.
- The zoom option defaults to false. Existing zoom calls keep their current auto-rotation behavior.
See A toolbar and labels that follow items, Programmatic modular scene control, and the Observers guide.