Record at 1080p, and stop recording when the tab stops drawing
Testing the recording flow turned up two faults, both of which produce a
bad file rather than an error.
The clip took whatever size the browser window happened to be. Measured
here that was 1220x900 -- a 1.36:1 video, which is not a shape any platform
expects and which each of them letterboxes differently. A recording now
locks the renderer to 1920x1080 at pixel ratio 1 and restores the previous
size when it stops. Pixel ratio matters: `captureStream` takes the drawing
buffer, so leaving it at 2 would have recorded 3840x2160 and paid four
times the pixels for a clip nobody watches above 1080.
The second is worse because it is silent. `setAnimationLoop` runs on
requestAnimationFrame, which browsers stop completely while a tab is
hidden -- measured at zero frames per second. The tour stops advancing and
the canvas stops feeding `captureStream`, but the recorder keeps writing,
so the file comes out the right length and full of one frozen image. A 60 s
tour is long enough that reaching for another window is the natural thing
to do. The recording now ends there, with a console warning saying why: a
short clip you know about beats a full-length one that is fifty seconds of
a still frame.
Adds `__probe.step(delta)` alongside the existing `renderOnce`. The file
already explained that the probe exists because requestAnimationFrame stops
in a background tab and automated tests could not get a frame; `renderOnce`
draws one but advances nothing, so it could not answer whether the showcase
gets through its beats. The animation loop's body is now a named function
that both the loop and the probe call.
Verified through that hook rather than by watching, since the browser pane
does not composite here:
tour 1201 steps = 60.1 s, ends clean, 27 distinct captions --
every model once, in order
output video/webm;codecs=vp9, decodes, duration finite at 2.489 s
for a 2.5 s capture (MediaRecorder often reports Infinity)
canvas 976x720 before, 1920x1080 during, 976x720 after
download fires as medieval-kit-60s.webm
hidden tab showcase and recorder both stop
cost 1.3 ms per frame at 1920x1080 -- ample headroom for 60 fps
27 models, 19007 triangles, all checks pass, build clean.
This commit is contained in:
parent
e34fba4f24
commit
e3db20e4eb
1 changed files with 77 additions and 4 deletions
|
|
@ -799,7 +799,19 @@ bindToggle('sky', (on) => {
|
|||
|
||||
// ResizeObserver is more reliable than the `resize` event: on pages opened with
|
||||
// the tab hidden, or whose layout settles later, the canvas does not stay 0x0.
|
||||
/**
|
||||
* Set while recording, holding what to put back afterwards.
|
||||
*
|
||||
* A recording is locked to 1920x1080 rather than taking whatever size the
|
||||
* browser window happens to be. Measured, an unlocked capture came out at
|
||||
* 1220x900 -- a 1.36:1 clip, which is not a video shape anyone expects and
|
||||
* which every platform will letterbox differently.
|
||||
*/
|
||||
let recordingSize: { ratio: number } | undefined
|
||||
|
||||
function resize(): void {
|
||||
// The observer keeps firing while recording; it must not undo the lock.
|
||||
if (recordingSize) return
|
||||
const width = canvas.clientWidth
|
||||
const height = canvas.clientHeight
|
||||
if (width === 0 || height === 0) return
|
||||
|
|
@ -835,6 +847,14 @@ if (import.meta.env.DEV) {
|
|||
Object.assign(globalThis, {
|
||||
__probe: {
|
||||
renderOnce: () => renderer.render(scene, camera),
|
||||
/** One full frame, including showcase advance. */
|
||||
step: (delta: number) => frameStep(delta),
|
||||
/** What the tour is showing right now. */
|
||||
showcase: () => ({
|
||||
running: showcase.isRunning(),
|
||||
progress: +showcase.progress().toFixed(4),
|
||||
caption: app.querySelector('[data-caption-address]')?.textContent ?? '',
|
||||
}),
|
||||
state: () => {
|
||||
const info = survey(current!.root)
|
||||
return {
|
||||
|
|
@ -971,6 +991,15 @@ const showcase = createShowcase({
|
|||
* pretending the file is ready to post.
|
||||
*/
|
||||
function startRecording(seconds: number): void {
|
||||
// Lock to 1080p at pixel ratio 1: `captureStream` takes the drawing buffer,
|
||||
// so a ratio of 2 would record 3840x2160 and cost four times the pixels for
|
||||
// a clip nobody will view above 1080.
|
||||
recordingSize = { ratio: renderer.getPixelRatio() }
|
||||
renderer.setPixelRatio(1)
|
||||
renderer.setSize(1920, 1080, false)
|
||||
camera.aspect = 1920 / 1080
|
||||
camera.updateProjectionMatrix()
|
||||
|
||||
const stream = canvas.captureStream(60)
|
||||
const types = ['video/webm;codecs=vp9', 'video/webm;codecs=vp8', 'video/webm']
|
||||
const mimeType = types.find((type) => MediaRecorder.isTypeSupported(type))
|
||||
|
|
@ -989,10 +1018,40 @@ function startRecording(seconds: number): void {
|
|||
link.click()
|
||||
setTimeout(() => URL.revokeObjectURL(url), 5000)
|
||||
recorder = undefined
|
||||
if (recordingSize) {
|
||||
renderer.setPixelRatio(recordingSize.ratio)
|
||||
recordingSize = undefined
|
||||
resize()
|
||||
}
|
||||
}
|
||||
recorder.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* A backgrounded tab draws nothing, so it must not keep recording.
|
||||
*
|
||||
* `setAnimationLoop` runs on requestAnimationFrame, which browsers stop
|
||||
* entirely while the tab is hidden -- measured here at zero frames per second.
|
||||
* The showcase therefore stops advancing AND the canvas stops producing frames
|
||||
* for `captureStream`, so what carries on being written is a frozen image with
|
||||
* a running clock. A 60 s tour is long enough that reaching for another window
|
||||
* is the natural thing to do, and the failure is silent: the file looks the
|
||||
* right length and is not.
|
||||
*
|
||||
* Ending the take is the honest answer. A short clip you know about beats a
|
||||
* full-length one that is fifty seconds of a still frame.
|
||||
*/
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (!document.hidden || !recorder) return
|
||||
console.warn(
|
||||
'medieval-kit: the tab lost focus, so the canvas stopped producing frames. '
|
||||
+ 'The recording has been ended here rather than filling with a frozen image. '
|
||||
+ 'Keep the window visible for the whole tour.',
|
||||
)
|
||||
showcase.stop()
|
||||
recorder.stop()
|
||||
})
|
||||
|
||||
function runShowcase(seconds: number): void {
|
||||
if (showcase.isRunning()) return
|
||||
showcase.start(seconds)
|
||||
|
|
@ -1027,10 +1086,17 @@ if (requested === 30 || requested === 60 || requested === 90) {
|
|||
setTimeout(() => runShowcase(requested), 400)
|
||||
}
|
||||
|
||||
renderer.setAnimationLoop(() => {
|
||||
const now = performance.now()
|
||||
const delta = Math.min((now - previous) / 1000, 0.05)
|
||||
previous = now
|
||||
/**
|
||||
* One frame, factored out so it can be stepped by hand.
|
||||
*
|
||||
* `setAnimationLoop` runs on requestAnimationFrame, which a browser stops
|
||||
* entirely while the tab is in the background -- measured at zero frames per
|
||||
* second. That makes the tour impossible to exercise from an automated test,
|
||||
* which is the same gap `renderOnce` was added for; `renderOnce` draws a frame
|
||||
* but does not advance anything, so it cannot tell you whether the showcase
|
||||
* gets through its beats.
|
||||
*/
|
||||
function frameStep(delta: number): void {
|
||||
current?.update?.(delta)
|
||||
if (showcase.isRunning()) {
|
||||
if (!showcase.update(delta)) recorder?.stop()
|
||||
|
|
@ -1038,4 +1104,11 @@ renderer.setAnimationLoop(() => {
|
|||
controls.update()
|
||||
}
|
||||
void renderer.render(scene, camera)
|
||||
}
|
||||
|
||||
renderer.setAnimationLoop(() => {
|
||||
const now = performance.now()
|
||||
const delta = Math.min((now - previous) / 1000, 0.05)
|
||||
previous = now
|
||||
frameStep(delta)
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue