-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-camera.html
More file actions
187 lines (170 loc) · 7.5 KB
/
Copy pathdebug-camera.html
File metadata and controls
187 lines (170 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, maximum-scale=1, user-scalable=no">
<title>Camera geometry probe</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<style>
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; background: #123; }
#app { position: relative; height: 100vh; height: 100dvh; width: 100%; }
#stage { position: absolute; inset: 0; background: #000; overflow: hidden; }
video {
position: absolute; top: 0; left: 0;
width: 100%; height: 100%;
min-width: 100%; min-height: 100%;
object-fit: cover;
background: #000;
}
video.modeA { transform: translateZ(0); backface-visibility: hidden; }
video.modeC { object-fit: fill; width: auto; height: auto;
min-width: 0; min-height: 0; transform-origin: top left; }
/* Gate variants start hidden and are revealed by script. */
video.gated { opacity: 0; visibility: hidden; transition: opacity 280ms ease; }
video.gated.ready { opacity: 1; visibility: visible; }
#out { position: absolute; left: 0; right: 0; top: 0; max-height: 72%;
overflow: auto; font: 10px/1.3 ui-monospace, Menlo, monospace;
color: #0f0; background: rgba(0,0,0,0.85); padding: 6px 8px;
white-space: pre; z-index: 5; }
#bar { position: absolute; left: 0; right: 0; bottom: 0; z-index: 6;
display: flex; flex-wrap: wrap; gap: 6px; padding: 10px;
background: rgba(0,0,0,0.8); }
#bar a { flex: 1 1 28%; text-align: center; padding: 12px 6px;
font: 600 14px -apple-system, sans-serif; text-decoration: none;
border-radius: 10px; background: #2866bd; color: #fff; }
#bar a.on { background: #0a0; }
.edge { position: fixed; background: magenta; z-index: 4; }
</style>
</head>
<body>
<div id="app">
<div id="stage"><video id="v" playsinline muted autoplay></video></div>
<pre id="out">starting…</pre>
<div id="bar"></div>
</div>
<div class="edge" style="left:0;top:0;right:0;height:2px"></div>
<div class="edge" style="left:0;bottom:0;right:0;height:2px"></div>
<script>
// Recording starts at the first script tick, before the camera is requested,
// so the whole load-time window is captured rather than sampled by hand.
const T0 = performance.now();
const q = new URLSearchParams(location.search);
const MODE = (q.get('mode') || 'A').toUpperCase();
const GATE = q.get('gate') || 'none';
const v = document.getElementById('v');
const out = document.getElementById('out');
const marks = [];
const rows = [];
let lastKey = '';
function mark(label) { marks.push(`${(performance.now() - T0).toFixed(0)}ms ${label}`); }
function sample() {
const r = v.getBoundingClientRect();
const cs = getComputedStyle(v);
const key = [v.videoWidth, v.videoHeight, Math.round(r.width), Math.round(r.height),
Math.round(r.left), Math.round(r.top), v.readyState, cs.visibility,
cs.objectFit].join('|');
if (key !== lastKey) {
lastKey = key;
rows.push({ t: Math.round(performance.now() - T0), vw: v.videoWidth, vh: v.videoHeight,
w: Math.round(r.width), h: Math.round(r.height),
x: Math.round(r.left), y: Math.round(r.top),
rs: v.readyState, vis: cs.visibility.slice(0,3) });
}
requestAnimationFrame(sample);
}
function probeUnit(u) {
const d = document.createElement('div');
d.style.cssText = `position:fixed;height:100${u};top:-99999px;`;
document.body.appendChild(d);
const px = d.getBoundingClientRect().height; d.remove(); return px;
}
function probeInset(side) {
const d = document.createElement('div');
d.style.cssText = `position:fixed;height:env(safe-area-inset-${side},0px);`;
document.body.appendChild(d);
const px = d.getBoundingClientRect().height; d.remove(); return px;
}
function render() {
const app = document.getElementById('app').getBoundingClientRect();
const t = v.srcObject && v.srcObject.getVideoTracks()[0];
const s = t && t.getSettings ? t.getSettings() : {};
const head = [
`mode=${MODE} gate=${GATE} standalone=${matchMedia('(display-mode: standalone)').matches}`,
`dpr=${devicePixelRatio} screen=${screen.width}x${screen.height} inner=${innerWidth}x${innerHeight}`,
`100vh=${probeUnit('vh').toFixed(0)} 100dvh=${probeUnit('dvh').toFixed(0)} safeT=${probeInset('top').toFixed(0)} safeB=${probeInset('bottom').toFixed(0)}`,
`#app=${Math.round(app.width)}x${Math.round(app.height)}@${Math.round(app.top)} track=${s.width}x${s.height}`,
'',
'EVENTS:', ...marks, '',
'GEOMETRY CHANGES (t, intrinsic, rect wxh @x,y, readyState, vis):',
];
const body = rows.map(r =>
`${String(r.t).padStart(5)} ${String(r.vw).padStart(4)}x${String(r.vh).padEnd(4)} ` +
`${String(r.w).padStart(4)}x${String(r.h).padEnd(4)} @${r.x},${r.y} rs=${r.rs} ${r.vis}`);
out.textContent = head.concat(body).join('\n');
}
const bar = document.getElementById('bar');
for (const [label, href] of [
['A cold', '?mode=A'], ['B cold', '?mode=B'], ['C cold', '?mode=C'],
['A+gate', '?mode=A&gate=app'], ['A+gate+', '?mode=A&gate=settle'], ['reload', location.search || '?mode=A'],
]) {
const a = document.createElement('a');
a.textContent = label; a.href = href;
if (location.search === href) a.className = 'on';
bar.appendChild(a);
}
function sizeToCover() {
if (MODE !== 'C') return;
const stage = document.getElementById('stage').getBoundingClientRect();
const vw = v.videoWidth, vh = v.videoHeight;
if (!vw || !vh) return;
const scale = Math.max(stage.width / vw, stage.height / vh);
const w = vw * scale, h = vh * scale;
v.style.width = w + 'px'; v.style.height = h + 'px';
v.style.transform = `translate(${(stage.width - w) / 2}px, ${(stage.height - h) / 2}px)`;
}
v.className = 'mode' + MODE + (GATE === 'none' ? '' : ' gated');
(async () => {
requestAnimationFrame(sample);
setInterval(render, 200);
mark('requesting camera');
let stream;
try {
stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: { ideal: 'environment' }, width: { ideal: 1280 }, height: { ideal: 720 } },
audio: false });
} catch (e) { mark('getUserMedia FAILED ' + e.name); return; }
mark('stream acquired');
v.srcObject = stream;
v.addEventListener('loadedmetadata', () => mark(`loadedmetadata ${v.videoWidth}x${v.videoHeight}`));
v.addEventListener('resize', () => { mark(`video resize ${v.videoWidth}x${v.videoHeight}`); sizeToCover(); });
await v.play().catch(e => mark('play FAILED ' + e.name));
mark('play() resolved');
sizeToCover();
if (GATE === 'none') return;
// Wait for real intrinsic dimensions.
await new Promise(res => {
const check = () => (v.videoWidth && v.videoHeight && v.readyState >= 2) ? res() : requestAnimationFrame(check);
check();
});
mark('dimensions known');
if ('requestVideoFrameCallback' in v) {
await new Promise(res => v.requestVideoFrameCallback(() => res()));
mark('first painted frame');
}
if (GATE === 'settle') {
// Hold for several consecutive frames so WebKit has laid the media layer
// out at least once more after the first frame.
for (let i = 0; i < 8; i++) await new Promise(r => requestAnimationFrame(r));
mark('settle frames elapsed');
} else {
await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)));
}
v.classList.add('ready');
mark('REVEALED');
})();
</script>
</body>
</html>