Skip to content

Commit e5bb4b6

Browse files
committed
fix(Wheel): a tap on the drum finds the row drawn under it, through the perspective
rowAtPoint inverted the cylinder orthographically, but the rows above and below the band are drawn through the picker's 900 px perspective, which draws them nearer the band: the fourth row from the band spans 84.7-89.3 px, and rowAtPoint answered the third row for 89% of it, middle included. rowExtent now gives where CSS draws a row (laid out at the band's centre, turned by rowTransform about rowOrigin's origin, seen through the perspective from the band's centre), and rowAtPoint answers the row drawn under the point: inside the band, the band's flat strip; outside it, the drum row whose drawn extent holds the point, the nearer one in the sliver between two rows, and nothing past the outermost row. The tests compose the CSS transforms from the strings the drum hands CSS and sweep every 0.25 px of the column at two offsets; without the fix the fourth row's middle answered row 34 instead of 35. In Chromium, painting the rows as the drum tells CSS to, every painted device pixel down the drum answers its row (2,866 of 2,869 at DPR 4, the other three on the seams between two rows), against 392 misses before.
1 parent 7aff638 commit e5bb4b6

2 files changed

Lines changed: 205 additions & 15 deletions

File tree

‎packages/core/src/DateInput/wheelDrum.test.ts‎

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
* @file wheelDrum.test.ts
55
* @input wheelDrum.ts and wheelPhysics.ts, and a UIPickerView's cell
66
* transforms read at rest
7-
* @output Proves the drum puts rows where UIKit's cylinder does, and that a
8-
* baked motion stays within tolerance, never sweeps a parked row across the
9-
* front, and leaves rows that never show without animations
7+
* @output Proves the drum puts rows where UIKit's cylinder does, that a tap
8+
* finds the row drawn under it through the perspective, and that a baked
9+
* motion stays within tolerance, never sweeps a parked row across the front,
10+
* and leaves rows that never show without animations
1011
* @position Unit coverage for DateInput/wheelDrum.ts
1112
*/
1213

@@ -19,6 +20,7 @@ import {
1920
rowAngle,
2021
rowAtPoint,
2122
rowDistance,
23+
rowExtent,
2224
rowKeyframes,
2325
rowOrigin,
2426
rowTransform,
@@ -100,7 +102,116 @@ describe('the drum at rest', () => {
100102
});
101103
});
102104

105+
type Matrix = number[][];
106+
107+
function multiply(a: Matrix, b: Matrix): Matrix {
108+
return a.map(row =>
109+
b[0].map((_, j) => row.reduce((sum, value, k) => sum + value * b[k][j], 0)),
110+
);
111+
}
112+
113+
const translateZ = (z: number): Matrix => [
114+
[1, 0, 0, 0],
115+
[0, 1, 0, 0],
116+
[0, 0, 1, z],
117+
[0, 0, 0, 1],
118+
];
119+
120+
/**
121+
* Where CSS draws a row's edges along y, from the band's centre: the row laid
122+
* out there with `transform: rowTransform(angle)` and `transform-origin:
123+
* rowOrigin(geometry)`, its parent's `perspective` seen from the band's
124+
* centre. Composed as CSS Transforms 2 does, from the strings the drum hands
125+
* CSS, not from `rowExtent`'s algebra.
126+
*/
127+
function cssExtent(angle: number) {
128+
const turn = Number(
129+
/rotateX\((-?[\d.e-]+)rad\)/.exec(rowTransform(angle))?.[1],
130+
);
131+
const originZ = Number(/(-?[\d.]+)px$/.exec(rowOrigin(GEOMETRY))?.[1]);
132+
const rotateX: Matrix = [
133+
[1, 0, 0, 0],
134+
[0, Math.cos(turn), -Math.sin(turn), 0],
135+
[0, Math.sin(turn), Math.cos(turn), 0],
136+
[0, 0, 0, 1],
137+
];
138+
const perspective: Matrix = [
139+
[1, 0, 0, 0],
140+
[0, 1, 0, 0],
141+
[0, 0, 1, 0],
142+
[0, 0, -1 / GEOMETRY.perspective, 1],
143+
];
144+
const drawn = multiply(
145+
perspective,
146+
multiply(translateZ(originZ), multiply(rotateX, translateZ(-originZ))),
147+
);
148+
const at = (y: number) => {
149+
const [, screenY, , w] = drawn.map(row => row[1] * y + row[3]);
150+
return screenY / w;
151+
};
152+
return {top: at(-GEOMETRY.rowHeight / 2), bottom: at(GEOMETRY.rowHeight / 2)};
153+
}
154+
103155
describe('which row is under a point', () => {
156+
it('draws each row where CSS draws it, nearer the band than the cylinder alone near the edge', () => {
157+
for (let row = -4; row <= 4; row++) {
158+
const angle = rowAngle(row * 32, GEOMETRY);
159+
const css = cssExtent(angle);
160+
const extent = rowExtent(angle, GEOMETRY);
161+
// The CSS strings round the turn to 1e-5 rad and the origin to 1e-3 px,
162+
// which moves an edge by under 1e-3 px.
163+
expect(extent.top).toBeCloseTo(css.top, 3);
164+
expect(extent.bottom).toBeCloseTo(css.bottom, 3);
165+
}
166+
// The fourth row below the band spans 84.7–89.3 px, where the cylinder
167+
// without its perspective puts the third row.
168+
const fourth = rowExtent(rowAngle(4 * 32, GEOMETRY), GEOMETRY);
169+
expect(fourth.top).toBeCloseTo(84.67, 2);
170+
expect(fourth.bottom).toBeCloseTo(89.34, 2);
171+
});
172+
173+
it('finds the row CSS draws under every point of the column', () => {
174+
// A tap in the middle of the fourth row below or above the band turns
175+
// to that row, not the third.
176+
const fourth = rowExtent(rowAngle(4 * 32, GEOMETRY), GEOMETRY);
177+
const middle = (fourth.top + fourth.bottom) / 2;
178+
expect(rowAtPoint(middle, 31 * 32, MINUTES, GEOMETRY)).toBe(35);
179+
expect(rowAtPoint(-middle, 31 * 32, MINUTES, GEOMETRY)).toBe(27);
180+
for (const offset of [31 * 32, 31 * 32 + 11.5]) {
181+
// Each drawn row by its position along the wheel.
182+
const drawn = Array.from({length: 13}, (_, k) => {
183+
const position = Math.round(offset / 32) - 6 + k;
184+
const angle = rowAngle(position * 32 - offset, GEOMETRY);
185+
return {position, visible: isRowVisible(angle), ...cssExtent(angle)};
186+
}).filter(row => row.visible);
187+
const half = GEOMETRY.height / 2;
188+
for (let dy = -half; dy <= half; dy += 0.25) {
189+
let expected: number | null;
190+
if (Math.abs(dy) < GEOMETRY.bandHeight / 2) {
191+
// The band's flat strip, the drum cut away behind it.
192+
expected = Math.round((offset + dy) / 32);
193+
} else {
194+
const under = drawn.find(row => dy >= row.top && dy <= row.bottom);
195+
const above = drawn.filter(row => row.bottom < dy);
196+
const below = drawn.filter(row => row.top > dy);
197+
if (under != null) {
198+
expected = under.position;
199+
} else if (above.length > 0 && below.length > 0) {
200+
const a = above.reduce((x, y) => (y.bottom > x.bottom ? y : x));
201+
const b = below.reduce((x, y) => (y.top < x.top ? y : x));
202+
expected = dy - a.bottom <= b.top - dy ? a.position : b.position;
203+
} else {
204+
expected = null;
205+
}
206+
}
207+
expect(
208+
rowAtPoint(dy, offset, MINUTES, GEOMETRY),
209+
`dy ${dy} at ${offset}`,
210+
).toBe(expected);
211+
}
212+
}
213+
});
214+
104215
it('inverts the cylinder', () => {
105216
const y = GEOMETRY.radius * Math.sin(32 / GEOMETRY.radius);
106217
expect(rowAtPoint(y, 31 * 32, MINUTES, GEOMETRY)).toBe(32);

‎packages/core/src/DateInput/wheelDrum.ts‎

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* @input A wheel's rows, its offset or a curve of it (wheelPhysics.ts), and
66
* the UIKit picker geometry (motion/uikit/picker.ts)
77
* @output Exports the drum's geometry: each row's angle on the cylinder and
8-
* its CSS transform, the band copy's translation, which row is under a
9-
* point, and the curve baked into keyframes per row so a motion runs on the
10-
* compositor
8+
* its CSS transform, where a row is drawn through the perspective, the band
9+
* copy's translation, which row is drawn under a point, and the curve baked
10+
* into keyframes per row so a motion runs on the compositor
1111
* @position Rendering math of the touch picker wheel; pure, no DOM
1212
*
1313
* UIPickerView draws each row on a cylinder: a row whose centre is d px from
@@ -123,26 +123,105 @@ export function stripTranslation(offset: number, layout: WheelLayout): number {
123123
return -(folded + cycle);
124124
}
125125

126+
/** Where a row is drawn along y, from the band's centre, px. */
127+
export interface RowExtent {
128+
readonly top: number;
129+
readonly bottom: number;
130+
}
131+
132+
/**
133+
* Where a row at `angle` is drawn: laid out at the band's centre, turned by
134+
* `rowTransform(angle)` about the origin `rowOrigin` puts R behind it, and
135+
* seen through the picker's perspective from the band's centre. Near the
136+
* drum's edge the perspective draws a row several px nearer the band than
137+
* the cylinder alone would (7 px for the fourth row).
138+
*/
139+
export function rowExtent(angle: number, geometry: DrumGeometry): RowExtent {
140+
const {radius, perspective, rowHeight} = geometry;
141+
const cos = Math.cos(angle);
142+
const sin = Math.sin(angle);
143+
// An edge `y` from the row's centre, R in front of the origin, turned
144+
// about x by −angle; then its depth from the layout plane sets its scale.
145+
const drawn = (y: number) => {
146+
const turnedY = y * cos + radius * sin;
147+
const depth = radius * cos - y * sin - radius;
148+
return (turnedY * perspective) / (perspective - depth);
149+
};
150+
return {top: drawn(-rowHeight / 2), bottom: drawn(rowHeight / 2)};
151+
}
152+
126153
/**
127-
* The row whose cylinder position is under a point `dy` px below the band's
128-
* centre, or null past the drum's top or bottom. Orthographic: the
129-
* perspective moves rows by under a pixel along y.
154+
* The row drawn under a point `dy` px below the band's centre, or null where
155+
* none is. Inside the band its flat copy shows (the drum is cut away there),
156+
* so the band strip's row. Outside it, the drum's rows where `rowExtent`
157+
* draws them; a point in the sliver between two of them takes the nearer,
158+
* and a point past the outermost one finds nothing.
130159
*/
131160
export function rowAtPoint(
132161
dy: number,
133162
offset: number,
134163
layout: WheelLayout,
135164
geometry: DrumGeometry,
136165
): number | null {
137-
if (Math.abs(dy) >= geometry.radius) {
138-
return null;
166+
const {rowHeight} = layout;
167+
if (Math.abs(dy) < geometry.bandHeight / 2) {
168+
return rowAtPosition(Math.round((offset + dy) / rowHeight), layout);
139169
}
140-
const distance = geometry.radius * Math.asin(dy / geometry.radius);
141-
const index = Math.round((offset + distance) / layout.rowHeight);
170+
// Rows within a quarter turn face the viewer; the rest are not drawn.
171+
const reach = Math.ceil((geometry.radius * Math.PI) / 2 / rowHeight) + 1;
172+
const centre = Math.round(offset / rowHeight);
173+
let nearest: number | null = null;
174+
let nearestGap = Infinity;
175+
let drawnAbove = false;
176+
let drawnBelow = false;
177+
for (let position = centre - reach; position <= centre + reach; position++) {
178+
const distance = position * rowHeight - offset;
179+
if (!isDrawnAt(position, distance, layout)) {
180+
continue;
181+
}
182+
const angle = rowAngle(distance, geometry);
183+
if (!isRowVisible(angle)) {
184+
continue;
185+
}
186+
const {top, bottom} = rowExtent(angle, geometry);
187+
if (dy >= top && dy <= bottom) {
188+
return rowAtPosition(position, layout);
189+
}
190+
drawnAbove ||= bottom < dy;
191+
drawnBelow ||= top > dy;
192+
const gap = dy < top ? top - dy : dy - bottom;
193+
if (gap < nearestGap) {
194+
nearestGap = gap;
195+
nearest = position;
196+
}
197+
}
198+
return drawnAbove && drawnBelow && nearest != null
199+
? rowAtPosition(nearest, layout)
200+
: null;
201+
}
202+
203+
/**
204+
* Whether the drum draws a row at `position` (`distance` px from the band):
205+
* a list's own rows, and on a looping wheel the copy nearest the band.
206+
*/
207+
function isDrawnAt(
208+
position: number,
209+
distance: number,
210+
layout: WheelLayout,
211+
): boolean {
212+
if (!layout.looping) {
213+
return position >= 0 && position < layout.rowCount;
214+
}
215+
const cycle = layout.rowCount * layout.rowHeight;
216+
return distance >= -cycle / 2 && distance < cycle / 2;
217+
}
218+
219+
/** The option at `position` along the wheel, or null past a list's ends. */
220+
function rowAtPosition(position: number, layout: WheelLayout): number | null {
142221
if (layout.looping) {
143-
return wrapIndex(index, layout.rowCount);
222+
return wrapIndex(position, layout.rowCount);
144223
}
145-
return index >= 0 && index < layout.rowCount ? index : null;
224+
return position >= 0 && position < layout.rowCount ? position : null;
146225
}
147226

148227
/** One baked sample of a curve. */

0 commit comments

Comments
 (0)