@@ -494,8 +494,6 @@ export class Html5Player extends Player {
494494 const frameBytes = Math . trunc ( W * scale ) * Math . trunc ( H * scale ) * 4 ;
495495 const framesInFlight = captureFramesInFlight ( navigator . hardwareConcurrency , frameBytes ) ;
496496 const { inFlight } = Html5Player ;
497- const hasRoom = ( ) : boolean =>
498- inFlight . frames === 0 || ( inFlight . frames < framesInFlight && inFlight . bytes + frameBytes <= CAPTURE_MEMORY_BUDGET_BYTES ) ;
499497 // The playhead's frame goes first, so the clip under it plays soonest.
500498 const segments = passSegments (
501499 Array . from ( { length : frameCount } , ( _ , i ) => i ) ,
@@ -510,16 +508,32 @@ export class Html5Player extends Player {
510508 this . bakingFrames = frames ;
511509 this . bakingScale = scale ;
512510 }
513- const pending : { idx : number ; drawn : Promise < Blob > } [ ] = [ ] ;
511+ const pending : { idx : number ; drawn : Promise < Blob > ; settled ?: Blob } [ ] = [ ] ;
512+ const hasRoom = ( ) : boolean =>
513+ pending . length < framesInFlight &&
514+ ( inFlight . frames === 0 || ( inFlight . frames < framesInFlight && inFlight . bytes + frameBytes <= CAPTURE_MEMORY_BUDGET_BYTES ) ) ;
514515 // The SVG is a frame's whole input, so a frame that repeats a recent one draws identical bytes and shares its Blob.
515516 const recent = new Map < string , Promise < Blob > > ( ) ;
516517 let recentStyles = "" ;
517518 let issued = 0 ;
518519 let published = 0 ;
519520 let progressStep = 0 ;
520521 let sliceStart = - Infinity ;
522+ const publish = ( idx : number , frame : Blob ) : void => {
523+ frames [ idx ] = frame ;
524+ published += 1 ;
525+ this . captureFramesDone += 1 ;
526+ // Steps, not frames: enough for a ring to fill smoothly, and bounded whatever the frame count.
527+ const step = Math . floor ( ( this . captureFramesDone / frameCount ) * CAPTURE_PROGRESS_STEPS ) ;
528+ if ( step > progressStep ) {
529+ progressStep = step ;
530+ this . emitCaptureProgress ( ) ;
531+ }
532+ } ;
521533 // Frames publish in the order they were issued, so the oldest in flight gates the window.
522534 while ( published < frameCount ) {
535+ // Draws finish while the bake yields, so finished frames publish before the next one is drawn.
536+ for ( let head = pending [ 0 ] ?. settled ; head ; head = pending [ 0 ] ?. settled ) publish ( pending . shift ( ) ! . idx , head ) ;
523537 if ( issued < frameCount && hasRoom ( ) ) {
524538 const idx = order [ issued ] ;
525539 this . bakeNextIndex = idx ;
@@ -558,22 +572,21 @@ export class Html5Player extends Player {
558572 if ( recent . size >= REPEAT_WINDOW_FRAMES ) recent . delete ( recent . keys ( ) . next ( ) . value ! ) ;
559573 recent . set ( body , drawn ) ;
560574 }
561- pending . push ( { idx, drawn } ) ;
575+ const entry : ( typeof pending ) [ number ] = { idx, drawn } ;
576+ drawn . then (
577+ blob => {
578+ entry . settled = blob ;
579+ } ,
580+ ( ) => undefined
581+ ) ;
582+ pending . push ( entry ) ;
562583 issued += 1 ;
563584 } else if ( pending . length > 0 ) {
564585 const { idx, drawn } = pending . shift ( ) ! ;
565586 const frame = await drawn ;
566587 if ( stale ( ) ) return null ;
567- frames [ idx ] = frame ;
568- published += 1 ;
569- this . captureFramesDone += 1 ;
570- // Steps, not frames: enough for a ring to fill smoothly, and bounded whatever the frame count.
571- const step = Math . floor ( ( this . captureFramesDone / frameCount ) * CAPTURE_PROGRESS_STEPS ) ;
572- if ( step > progressStep ) {
573- progressStep = step ;
574- this . emitCaptureProgress ( ) ;
575- }
576- } else {
588+ publish ( idx , frame ) ;
589+ } else if ( issued < frameCount ) {
577590 // Other bakes' frames fill the window, and they finish without their bakes' turns.
578591 await nextFrame ( ) ;
579592 if ( stale ( ) ) return null ;
0 commit comments