Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.4 — Framing an Object
3D Web · Example 2 of 2

frameObject(): one function for every shape

The bike here is two simple shapes. Drag to orbit: the limits came from the object's size. The last two lines are the guard for empty and scale-0 objects.

To fit an object in the picture, compute the camera distance from the field of view: d = (size / 2) / tan(fov / 2), using whichever of height and width is tighter. Then set near, far and the controls' limits from the same size, and do it again when the window changes shape.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 3 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

A shelf 2.4 m wide and 0.4 m tall is seen by a camera with fov 40° and aspect 2. How far must the camera be, with no margin?

02

Write the guard at the top of frameObject that stops it from framing an empty object, in one if.

Explain it without notes

01

Derive the distance at which an object h tall just fits, for a camera with vertical field of view fov.

02

Why does the same bike need a different camera distance on a phone held upright?

frameObject(): one function for every shape
Press Run to see the page.
idle

Console

Nothing logged yet.

Expected console output

16:9 screen            distance 2.65 m, limits 0.53 to 7.96 m
phone upright          distance 5.28 m, limits 0.53 to 15.83 m
phone turned sideways  distance 2.65 m, limits 0.53 to 7.96 m
nothing to frame: the object is empty or has scale 0
nothing to frame: the object is empty or has scale 0