Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 50 ยท 2D Transformations

Window-to-Viewport Mapping

Learn the window-to-viewport transformation: the mapping formula, scale factors, aspect ratio distortion, and how zoom and pan work, with live demos.

When you describe a scene, you use the numbers that suit the scene. A map might use kilometers, a graph of a sound wave might use seconds and volts, and a game level might use meters. The screen, however, has only pixels. Somewhere, a rule must say: "the part of the world between these coordinates appears in this rectangle of the display." That rule is the window-to-viewport transformation.

It is one of the oldest ideas in computer graphics, and it still sits inside every plotting library, map viewer, CAD tool and game camera. It is also an easy way to practice the transformations of this chapter, because it is nothing more than a translate, a scale and another translate.

Window and viewport

  • The window is a rectangle in world coordinates: the part of the scene you want to see. It is defined by xwmin, xwmax, ywmin, ywmax.
  • The viewport is a rectangle in device (screen) coordinates: the region of the display where that part will be drawn. It is defined by xvmin, xvmax, yvmin, yvmax.

The word "window" here has nothing to do with the operating-system window. Think of it as a window onto the world: a frame through which you look. Changing the window changes what you see; changing the viewport changes where and how big it appears on screen.

flowchart LR
  subgraph WORLD["World coordinates"]
    W1["Window<br/>xwmin..xwmax<br/>ywmin..ywmax"]
  end
  subgraph DEVICE["Screen coordinates"]
    V1["Viewport<br/>xvmin..xvmax<br/>yvmin..yvmax"]
  end
  W1 -->|"translate, scale, translate"| V1

Every point inside the window lands at the corresponding place inside the viewport.

The mapping formula

The idea is proportionality. If a point is 30% of the way across the window, it should be 30% of the way across the viewport. In symbols, for the x-direction:

TEXT
(xv - xvmin) / (xvmax - xvmin) = (xw - xwmin) / (xwmax - xwmin)

Solve for xv:

TEXT
sx = (xvmax - xvmin) / (xwmax - xwmin)
xv = xvmin + (xw - xwmin) * sx

and the same for y with sy = (yvmax - yvmin) / (ywmax - ywmin). Read the formula as three steps, which you will recognize from the last lessons:

  1. Subtract xwmin: translate the window corner to the origin.
  2. Multiply by sx: scale the window to the viewport's size.
  3. Add xvmin: translate to the viewport's position.

As a matrix this is T(xvmin, yvmin) * S(sx, sy) * T(-xwmin, -ywmin), a classic composition in homogeneous coordinates. Because it is a matrix, you can multiply it with the other transformations of a scene and send everything through the pipeline in one step.

The y-flip

The formula as written assumes that both rectangles have y growing in the same direction. World coordinates normally have y up, but screen coordinates have y down (see coordinate systems). If the viewport rectangle is given in screen pixels, with yvmin the top row and yvmax the bottom row, the correct mapping flips the direction:

TEXT
yv = yvmax - (yw - ywmin) * sy      where sy = (yvmax - yvmin) / (ywmax - ywmin)

The bottom of the window (yw = ywmin) lands on the bottom of the viewport, and the top of the window lands on yvmin, the top. Forgetting this flip gives upside-down plots, one of the most frequent beginner bugs.

A worked example

Window: x from 0 to 10, y from 0 to 5. Viewport: x from 100 to 300 and y from 50 to 150 (screen pixels, y down).

TEXT
sx = (300 - 100) / (10 - 0) = 20
sy = (150 -  50) / ( 5 - 0) = 20

Point (5, 2):   xv = 100 + (5 - 0) * 20 = 200
                yv = 150 - (2 - 0) * 20 = 110

The point (5, 2) is halfway across the window and 40% of the way up, and pixel (200, 110) is halfway across the viewport and 40% of the way up from the bottom edge. The scale factors are equal here (20 and 20), so shapes keep their proportions. That is not always true.

Zoom and pan

The window-to-viewport idea gives you zoom and pan for free. You never change the picture; you change the window.

  • Pan by shifting the window: add dx to both xwmin and xwmax to see more to the right. The scene seems to slide left.
  • Zoom in by making the window smaller around a center. The same viewport now shows a smaller piece of the world, so everything looks larger. Halving the window width doubles every apparent size.
  • Zoom out by making the window bigger.

To zoom around a center (cx, cy) by a factor k (with k > 1 meaning zoom in), the new window half-sizes are the old ones divided by k:

TEXT
halfW = (xwmax - xwmin) / 2 / k
xwmin = cx - halfW,   xwmax = cx + halfW

(and the same for y). In an interactive map you usually zoom about the mouse position instead of the center. Then you pick the world point under the mouse, zoom, and shift the window so the same world point stays under the mouse.

The first demo plots a sine wave, a circle and a grid. Use the buttons to zoom and pan: only the four window numbers change. The viewport (the framed rectangle on the canvas) never moves. The drawing is clipped to the viewport with ctx.clip() so parts of the world outside the window do not appear.

Zoom and pan with a window

Notice the log: zooming in raises the "pixels per world unit" number. That number is exactly the scale factor s in the formula. Panning leaves it unchanged.

Aspect ratio and distortion

The aspect ratio of a rectangle is its width divided by its height. The scale factors sx and sy are independent. If the window and the viewport have the same aspect ratio, then sx = sy, and shapes keep their proportions. If they differ, sx โ‰  sy, and the picture is stretched: circles become ellipses, squares become rectangles. This is the non-uniform scaling from translation, scaling and rotation, applied to the whole scene.

Sometimes stretching is wanted, for example when a chart should fill any available space. For geometry (a map, a game, a drawing) it is almost always a bug. There are two common ways to fix it:

  1. Adapt the window. When the viewport size changes, recompute the window so it has the same aspect ratio, for example height = width * viewportHeight / viewportWidth. The world shown grows or shrinks, but is never distorted. This is what most cameras do.
  2. Fit with a uniform scale. Use s = min(sx, sy) for both axes and center the result inside the viewport. The unused space on two sides is called a letterbox (bars at the top and bottom) or a pillarbox (bars at the sides), just like a film shown on a screen of a different shape.
flowchart TD
  A["Window aspect โ‰  viewport aspect"] --> B{"What should happen?"}
  B -->|"fill the viewport"| C["sx โ‰  sy: distorted picture"]
  B -->|"keep shapes round"| D["s = min(sx, sy): letterbox"]
  B -->|"keep shapes round and fill"| E["change the window's size to match"]

Choose between stretching, letterboxing and changing the window.

The second demo shows a window of 8 by 6 units containing a circle of radius 2 and a square. It is drawn into two square viewports of 140 by 140 pixels. The left one stretches the window to fill the viewport. The right one uses the uniform scale min(sx, sy) and centers the result.

Stretching versus a uniform scale

In the left viewport the circle is clearly taller than it is wide because sy is larger than sx (23.33 against 17.5). On the right both scales are 17.5, so the circle is round, and the window rectangle (the light blue area) is shorter than the viewport, leaving bars at the top and bottom.

Where you meet this idea again

  • The last step of the 3D pipeline, the viewport transform, is a window-to-viewport mapping from normalized device coordinates (-1 to 1) to pixels. We pick it up again in the rasterization pipeline.
  • Canvas and SVG have the same concept: SVG's viewBox is the window and the element's size is the viewport; preserveAspectRatio chooses between stretching and a uniform fit.
  • Clipping is naturally done against the window, before mapping: there is no point in transforming things you cannot see. The next chapter, starting with Cohen-Sutherland line clipping, shows how.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the window define?

    The window selects a rectangle in world coordinates; the viewport says where on the screen it appears.
  2. 2. Window: x 0..10. Viewport: x 100..300. Where does xw = 5 land?

    sx = 200 / 10 = 20, so xv = 100 + 5 * 20 = 200: halfway across the viewport.
  3. 3. How do you zoom in with a window-to-viewport setup?

    A smaller window shows a smaller part of the world in the same viewport, so everything looks larger.
  4. 4. A 4:3 window is mapped to a square viewport with different scales for x and y. What happens to a circle in the world?

    Different scale factors for x and y stretch the picture non-uniformly, turning circles into ellipses.
  5. 5. Why does the y mapping often use yv = yvmax - (yw - ywmin) * sy?

    Screen rows are counted from the top, so the bottom of the window must map to the larger pixel row.

Summary

  • The window is the part of the world to show; the viewport is the screen rectangle that shows it.
  • The mapping is a translate, a scale and another translate: xv = xvmin + (xw - xwmin) * sx, with sx the ratio of viewport to window width. For screen output flip y.
  • Zoom and pan change only the window, never the scene.
  • Different aspect ratios cause stretching; fix it by adapting the window or by using one uniform scale min(sx, sy) with a letterbox.
  • The final viewport transform of the 3D pipeline is the same idea.

Next, we discard what is outside the window: Cohen-Sutherland Line Clipping.