How it works

The four steps of a calculation: place, check the boundary, flip, stick.

A calculation runs in four steps. The library reads the sizes and positions, places the item, checks the boundary and, if needed, changes the placement. It never moves the element itself.

Item, target and boundary A boundary rectangle with a dashed edge. An item sits above a target and crosses the top edge of the boundary. boundary item target
The item is placed above the target. Its top edge crosses the boundary, so flip is needed.

The four steps

  1. Place. Each element has a reference point, set with itemAt and targetAt. The library moves the item so that both points meet, including the offsets. The result is moveBy.
  2. Check the boundary. The library measures the distance from the new position to each edge of the boundary. An edge that the item crosses is listed in distance.overflow.
  3. Flip. If an edge overflows and flip is set, the library calculates the flipped placement. If it has no overflow, the library uses it. Otherwise it chooses, per axis, the placement with less overflow. Flip needs a target.
  4. Stick. If stick is set, the library moves the item along the edges you chose so that it stays inside the boundary.

The result of calculate() reflects all four steps. calcVariant() stops after step 2 and tests one placement only.

What the model does not cover

The steps use the sizes and positions the library read last. They do not follow later changes by themselves. Call calculate() again after a scroll or a resize. See Placement and collisions for the rules of each step and Limits for what is not handled.