Skip to content

Commit ad7941c

Browse files
committed
[css-nav-1] Clarify how the improve distance function works
1 parent f003a72 commit ad7941c

File tree

4 files changed

+73
-65
lines changed

4 files changed

+73
-65
lines changed

css-nav-1/Overview.bs

+73-65
Original file line numberDiff line numberDiff line change
@@ -1155,14 +1155,9 @@ run the following steps:
11551155
whose boundary goes through the geometric center of the <var>searchOrigin</var>
11561156
and is perpendicular to <var>dir</var>.
11571157
2. For each <var>candidate</var> in <var>candidates</var>,
1158-
1. If the <var>searchOrigin</var> is an element, then find the points <var>P1</var> inside the <a>boundary box</a>
1159-
of <var>searchOrigin</var> and P2 inside the boundary box of <var>candidate</var>.
1160-
2. Else if the <var>searchOrigin</var> is a point, then let the point <var>P1</var> be the <var>searchOrigin</var>
1161-
and find the point <var>P2</var> inside the <a>boundary box</a>.
1162-
3. Let <var>intersectRect</var> be an <dfn>intersecting area</dfn> between the search origin and a candidate.
1163-
4. Select the <var>P1</var> and <var>P2</var> that make the smallest <var>distance</var> between those two points by
1164-
<a>calculating the distance</a>.
1165-
5. Return the item of the <var>candidates</var> set that has the smallest <var>distance</var>.
1158+
[=find the shortest distance=] between <var>searchOrigin</var> and <var>candidate</var>
1159+
in direction <var>dir</var>.
1160+
3. Return the item of the <var>candidates</var> set that has the smallest distance.
11661161
If several have the same distance,
11671162
return the first one in document order,
11681163
unless its [=boundary box=] overlaps with the [=boundary box=] of an other item at the same distance,
@@ -1172,82 +1167,95 @@ run the following steps:
11721167

11731168
</div>
11741169

1175-
<div algorithm="to calculate the distance">
1176-
To <dfn lt="calculating the distance | to calculate the distance">calculate the <var>distance</var></dfn>, the function is defined as below:
1170+
<div algorithm="to find the shortest the distance">
1171+
To <dfn lt="find the shortest distance">to find the shortest distance</dfn>
1172+
between a <var>reference</var> and a <var>candidate</var>
1173+
in direction <var>dir</var>,
1174+
find the points <var>P1</var> and <var>P2</var>,
1175+
respectively within the boundary boxes of the <var>reference</var> and of the <var>candidate</var>,
1176+
that minimize the <var>distance</var> as defined as below:
11771177

1178-
<pre class="prod">
1178+
<pre><code>
11791179
<var>distance</var> = <var>euclidean</var> + <var>displacement</var> - <var>alignment</var> - <var>sqrt(Overlap)</var>
1180-
</pre>
1180+
</code></pre>
11811181

1182-
The meaning of each factor in the function is as follows:
1182+
The meaning of each term is as follows:
11831183

11841184
<dl dfn-type="value" dfn-for="spatial-navigation-distance-function">
11851185
<dt><var>euclidean</var>
1186-
<dd>The euclidean distance between <var>P1</var> and <var>P2</var>
1186+
<dd>
1187+
The euclidean distance between <var>P1</var> and <var>P2</var>
11871188

11881189
<dt><var>displacement</var>
1189-
<dd>The degree of displacement in <var>dir</var> between <var>searchOrigin</var> and <var>candidate</var>
1190-
<dd>Defined as <pre class="prod">(absolute distance on the axis orthogonal to <var>dir</var> + <var>orthogonalBias</var>) * <var>orthogonalWeight</var></pre>
1191-
1192-
* <var>orthogonalBias</var>
1193-
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}},
1194-
height of <var>searchOrigin</var> / 2
1195-
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}},
1196-
width of <var>searchOrigin</var> / 2
1190+
<dd>
1191+
The degree of displacement in <var>dir</var> between the <var>reference</var> and the <var>candidate</var>,
1192+
defined as
1193+
<pre><code>
1194+
<var>displacement</var> = (absolute distance on the axis orthogonal to <var>dir</var> between <var>P1</var> and <var>P2</var> +
1195+
<var>orthogonalBias</var>) *
1196+
<var>orthogonalWeight</var>
1197+
</code></pre>
1198+
1199+
<dt><var>orthogonalBias</var>:
1200+
<dd>
1201+
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}},
1202+
the height of the axis-aligned bounding box of <var>reference</var> / 2
1203+
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}},
1204+
the width of the axis-aligned bounding box of <var>reference</var> / 2
11971205

1198-
* <var>orthogonalWeight</var>:
1199-
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}}, 30
1200-
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}}, 2
1206+
<dt><var>orthogonalWeight</var>:
1207+
<dd>
1208+
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}}, 30
1209+
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}}, 2
12011210

12021211
<dt><var>alignment</var>
1203-
<dd>The degree of alignment in <var>dir</var> between <var>searchOrigin</var> and <var>candidate</var>
1204-
<dd>Defined as <pre class="prod"><var>alignBias</var> * <var>alignWeight</var></pre>
1212+
<dd>
1213+
The degree of alignment in <var>dir</var> between the <var>reference</var> and the <var>candidate</var>,
1214+
defined as:
1215+
<pre><code>
1216+
<var>alignment</var> = <var>alignBias</var> * <var>alignWeight</var>
1217+
</code></pre>
12051218

1206-
* <var>alignBias</var>
1219+
<dt><var>alignBias</var>:
1220+
<dd>
1221+
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}},
1222+
<var>projectedOverlap</var> / height of the axis-aligned bounding box of <var>candidate</var>
1223+
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}},
1224+
<var>projectedOverlap</var> / width of the axis-aligned bounding box of <var>candidate</var>
12071225

1208-
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}},
1209-
height of <var>intersectRect</var> / height of <var>candidate</var>
1210-
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}},
1211-
width of <var>intersectRect</var> / width of <var>candidate</var>
1226+
<dt><var>projectedOverlap</var>:
1227+
<dd>
1228+
* If the <var>dir</var> is {{SpatialNavigationDirection/left}} or {{SpatialNavigationDirection/right}},
1229+
the length of the overlap between the horizontal projections onto the vertical axis
1230+
of the <var>reference</var> and the <var>candidate</var>
1231+
* Else if the <var>dir</var> is {{SpatialNavigationDirection/up}} or {{SpatialNavigationDirection/down}},
1232+
the length of the overlap between the vertical projections onto the horizontal axis
1233+
of the <var>reference</var> and the <var>candidate</var>
12121234

1213-
* <var>alignWeight</var> is 5
1235+
<figure>
1236+
<img alt="" src="images/projected_overlap.png" style="width: 500px;"/>
1237+
<figcaption>projectedOverlap</figcaption>
1238+
</figure>
1239+
1240+
<dt><var>alignWeight</var>:
1241+
<dd>5
12141242

12151243
<dt><var>sqrt(Overlap)</var>
1216-
<dd>The square root of an <a>overlapping area</a> between the <a>boundary boxes</a> of <var>searchOrigin</var> and <var>candidate</var>
1217-
<dd>Let <var>overlapRect</var> be
1218-
<var>intersectRect</var> if the <a>overlapping area</a> exists,
1219-
and 0 otherwise.
1220-
<dd>Defined as <pre class="prod">sqrt(height of <var>overlapRect</var> * width of <var>overlapRect</var>)</pre>
1244+
<dd>
1245+
The square root of area of overlap between the <var>reference</var> and the <var>candidate</var>,
1246+
or 0 if they do not overlap.
12211247
</dl>
12221248

1223-
NOTE: The values of <var>alignWeight</var> and <var>orthogonalWeight</var> in the function are decided based on
1224-
the collected <a href="https://wicg.github.io/spatial-navigation/tests/ux/list.html">UX test cases</a>.
1225-
It aims to show the generally expected result in the real pages.
1226-
Everyone is welcome to contribute to the test cases!
1249+
Note: This general formula was picked from several plausible alternatives,
1250+
based on which one most often match intuition
1251+
when used to select the best candidate
1252+
in a series of <a href="https://wicg.github.io/spatial-navigation/tests/ux/list.html">UX test cases</a>.
1253+
Similarly, the values of <var>alignWeight</var> and <var>orthogonalWeight</var>
1254+
were also determined experimentatlly based on the same test cases.
1255+
The resulting forumla is somewhat complicated,
1256+
but seems to give good answers.
1257+
Suggestions on improvements or simplifications are welcome.
12271258

1228-
<div class='example'>
1229-
This example shows how the <a>intersecting area</a>(<var>intersectRect</var>) is defined.
1230-
1231-
<figure>
1232-
<img alt="" src="images/intersecting-area-1.png" style="height: 200px;"/>
1233-
<figcaption>The <a>intersecting area</a> when there is the <a>overlapping area</a>.</figcaption>
1234-
</figure>
1235-
1236-
The figure above is the simplest case to define the <a>intersecting area</a>.
1237-
The <dfn>overlapping area</dfn> is the geometric overlap region between the two elements.
1238-
In this case, the <a>intersecting area</a> is the same as the <a>overlapping area</a> without reference to the direction of the navigation.
1239-
1240-
<figure>
1241-
<img alt="" src="images/intersecting-area-2.png" style="height: 200px;"/>
1242-
<figcaption>The <a>intersecting area</a> when the elements are aligned.</figcaption>
1243-
</figure>
1244-
1245-
In the case above, the search origin element and the candidate element are aligned and the requested direction is {{SpatialNavigationDirection/right}}.
1246-
The <a>intersecting area</a> is created with
1247-
(1) The gap between two elements.
1248-
(2) The parts of the edges from each element which are overlapped
1249-
when the right edge of the search origin element and the left edge of the candidate are projected to each other.
1250-
</div>
12511259
</div>
12521260

12531261
<div class=cssapi>
-18.6 KB
Binary file not shown.
-19.4 KB
Binary file not shown.
8.52 KB
Loading

0 commit comments

Comments
 (0)