@@ -17,6 +17,7 @@ import {
1717} from "@shotstack/shotstack-canvas" ;
1818import * as pixi from "pixi.js" ;
1919
20+ import { passSegments } from "./bake-order" ;
2021import { BakeQueue } from "./bake-queue" ;
2122import { computeHtml5CacheKey , html5CacheGet , html5CachePut } from "./html5-cache" ;
2223import { createCaptureLoadingGraphic , createPlaceholderGraphic } from "./placeholder-graphic" ;
@@ -423,6 +424,11 @@ export class Html5Player extends Player {
423424 return start + ( idx + 1 ) / this . captureFps ;
424425 }
425426
427+ // The frame on screen at the playhead, or past the last frame once the playhead has left the clip.
428+ private bakeAnchor ( frameCount : number ) : number {
429+ return Math . min ( Math . max ( 0 , Math . floor ( ( this . edit . playbackTime - this . getStart ( ) ) * this . captureFps ) ) , frameCount ) ;
430+ }
431+
426432 // After a tick, an active clip's global transform is the size the canvas shows it at. An inactive clip's own
427433 // transform isn't applied, so its bake reads only the viewport's zoom and may re-bake once the clip is active.
428434 private async bakeScale ( minScale : number ) : Promise < number > {
@@ -480,24 +486,35 @@ export class Html5Player extends Player {
480486 this . captureFramesDone = 0 ;
481487
482488 const framesInFlight = captureFramesInFlight ( navigator . hardwareConcurrency , Math . trunc ( W * scale ) * Math . trunc ( H * scale ) * 4 ) ;
483- const blobs : Blob [ ] = [ ] ;
484- const pending : Promise < Blob > [ ] = [ ] ;
489+ // The playhead's frame goes first, so the clip under it plays soonest.
490+ const segments = passSegments (
491+ Array . from ( { length : frameCount } , ( _ , i ) => i ) ,
492+ this . bakeAnchor ( frameCount )
493+ ) ;
494+ const order = segments . flat ( ) ;
495+ // Where each segment after the first starts in the issue order.
496+ const freshPageAt = new Set ( segments . slice ( 0 , - 1 ) . map ( ( _ , i ) => segments . slice ( 0 , i + 1 ) . flat ( ) . length ) ) ;
497+ const frames : Array < Blob | undefined > = new Array ( frameCount ) ;
498+ const pending : { idx : number ; drawn : Promise < Blob > } [ ] = [ ] ;
485499 // The SVG is a frame's whole input, so a frame that repeats a recent one draws identical bytes and shares its Blob.
486500 const recent = new Map < string , Promise < Blob > > ( ) ;
487501 let recentStyles = "" ;
488502 let issued = 0 ;
489- this . bakeNextIndex = 0 ;
503+ let published = 0 ;
490504 let progressStep = 0 ;
491505 let sliceStart = - Infinity ;
492- // Frames publish in order, so the oldest in flight gates the window.
493- while ( blobs . length < frameCount ) {
506+ // Frames publish in the order they were issued , so the oldest in flight gates the window.
507+ while ( published < frameCount ) {
494508 if ( issued < frameCount && pending . length < framesInFlight ) {
509+ const idx = order [ issued ] ;
510+ this . bakeNextIndex = idx ;
511+ if ( freshPageAt . has ( issued ) && ! ( await this . loadFreshPage ( this . iframe , stale , scale ) ) ) return null ;
495512 if ( performance . now ( ) - sliceStart >= CAPTURE_SLICE_MS ) {
496513 await nextFrame ( ) ;
497514 if ( stale ( ) ) return null ;
498515 sliceStart = performance . now ( ) ;
499516 }
500- this . seekHarness ( issued / fps ) ;
517+ this . seekHarness ( idx / fps ) ;
501518 forceLayout ( this . iframe . contentDocument . body ) ;
502519 const { styles, body } = this . serialiseIframe ( W , H ) ;
503520 if ( styles !== recentStyles ) {
@@ -515,13 +532,14 @@ export class Html5Player extends Player {
515532 if ( recent . size >= REPEAT_WINDOW_FRAMES ) recent . delete ( recent . keys ( ) . next ( ) . value ! ) ;
516533 recent . set ( body , drawn ) ;
517534 }
518- pending . push ( drawn ) ;
535+ pending . push ( { idx , drawn } ) ;
519536 issued += 1 ;
520- this . bakeNextIndex = issued ;
521537 } else {
522- const frame = await pending . shift ( ) ! ;
538+ const { idx, drawn } = pending . shift ( ) ! ;
539+ const frame = await drawn ;
523540 if ( stale ( ) ) return null ;
524- blobs . push ( frame ) ;
541+ frames [ idx ] = frame ;
542+ published += 1 ;
525543 this . captureFramesDone += 1 ;
526544 // Steps, not frames: enough for a ring to fill smoothly, and bounded whatever the frame count.
527545 const step = Math . floor ( ( this . captureFramesDone / frameCount ) * CAPTURE_PROGRESS_STEPS ) ;
@@ -532,6 +550,8 @@ export class Html5Player extends Player {
532550 }
533551 }
534552
553+ // Every index has published by now.
554+ const blobs = frames as Blob [ ] ;
535555 if ( ! ( await this . adoptBake ( blobs , cacheKey , scale , stale ) ) ) return null ;
536556 this . emitCaptureCompleted ( frameCount ) ;
537557
0 commit comments