@@ -87,19 +87,116 @@ function gsapNeverRendered(timeline: RuntimeTimelineLike): boolean {
8787}
8888
8989const lastRenderSeek = new WeakMap < object , number > ( ) ;
90+ const sharedPropertySpans = new WeakMap < object , { version : string ; spans : [ number , number ] [ ] } > ( ) ;
91+ const NOT_PROPERTIES = new Set ( [
92+ "duration" ,
93+ "ease" ,
94+ "delay" ,
95+ "repeat" ,
96+ "repeatDelay" ,
97+ "yoyo" ,
98+ "yoyoEase" ,
99+ "repeatRefresh" ,
100+ "stagger" ,
101+ "overwrite" ,
102+ "immediateRender" ,
103+ "lazy" ,
104+ "id" ,
105+ "paused" ,
106+ "reversed" ,
107+ "callbackScope" ,
108+ "runBackwards" ,
109+ "data" ,
110+ "inherit" ,
111+ "defaults" ,
112+ ] ) ;
113+ const EVERY_PROPERTY = new Set ( [ "keyframes" , "startAt" , "transform" ] ) ;
114+ const SAME_PROPERTY : Record < string , string > = {
115+ scaleX : "scale" ,
116+ scaleY : "scale" ,
117+ autoAlpha : "opacity" ,
118+ rotate : "rotation" ,
119+ } ;
120+
121+ type PlacedTween = GsapPlaced & {
122+ vars ?: Record < string , unknown > ;
123+ targets ?: ( ) => object [ ] ;
124+ totalDuration ?: ( ) => number ;
125+ } ;
126+ type PropertyWrite = { start : number ; end : number ; properties : Set < string > | null } ;
127+
128+ function propertiesWritten ( vars : Record < string , unknown > ) : Set < string > | null {
129+ const properties = new Set < string > ( ) ;
130+ for ( const key of Object . keys ( vars ) ) {
131+ if ( EVERY_PROPERTY . has ( key ) ) return null ;
132+ if ( ! key . startsWith ( "on" ) && ! NOT_PROPERTIES . has ( key ) )
133+ properties . add ( SAME_PROPERTY [ key ] ?? key ) ;
134+ }
135+ return properties ;
136+ }
90137
91- function renderFramesSkippedSinceLastSeek (
138+ function writeOf ( tween : PlacedTween , timeline : RuntimeTimelineLike ) : PropertyWrite | null {
139+ const start = startOnOwner ( tween , timeline ) ;
140+ if ( start === null ) return null ;
141+ let speed = 1 ;
142+ for ( let parent = tween . parent ; parent && parent !== timeline ; parent = parent . parent )
143+ speed *= Number ( parent . _ts ) ;
144+ const end = start + Number ( tween . totalDuration ?.( ) ?? 0 ) / speed ;
145+ return { start, end, properties : propertiesWritten ( tween . vars ?? { } ) } ;
146+ }
147+
148+ function spansWhereTweensShareAProperty ( timeline : RuntimeTimelineLike ) : [ number , number ] [ ] {
149+ const writes = new Map < object , PropertyWrite [ ] > ( ) ;
150+ for ( const child of timeline . getChildren ?.( true , true , false ) ?? [ ] ) {
151+ const tween = child as PlacedTween ;
152+ const write = writeOf ( tween , timeline ) ;
153+ if ( ! write ) return [ [ - Infinity , Infinity ] ] ;
154+ for ( const target of tween . targets ?.( ) ?? [ ] )
155+ writes . set ( target , [ ...( writes . get ( target ) ?? [ ] ) , write ] ) ;
156+ }
157+ return [ ...writes . values ( ) ] . flatMap ( overlapsOnOneTarget ) ;
158+ }
159+
160+ function sharesAProperty ( a : PropertyWrite , b : PropertyWrite ) : boolean {
161+ return ! a . properties || ! b . properties || [ ...a . properties ] . some ( ( p ) => b . properties ! . has ( p ) ) ;
162+ }
163+
164+ function overlapsOnOneTarget ( writes : PropertyWrite [ ] ) : [ number , number ] [ ] {
165+ const spans : [ number , number ] [ ] = [ ] ;
166+ writes . sort ( ( x , y ) => x . start - y . start ) ;
167+ writes . forEach ( ( first , i ) => {
168+ for ( const second of writes . slice ( i + 1 ) ) {
169+ if ( second . start >= first . end ) break ;
170+ if ( sharesAProperty ( first , second ) )
171+ spans . push ( [ second . start , Math . min ( first . end , second . end ) ] ) ;
172+ }
173+ } ) ;
174+ return spans ;
175+ }
176+
177+ function framesSkippedSinceLastSeek (
92178 timeline : RuntimeTimelineLike ,
93179 t : number ,
94- suppressEvents : boolean ,
95180 fps : number ,
96- ) : void {
181+ version = "" ,
182+ ) : number [ ] {
97183 const last = lastRenderSeek . get ( timeline ) ;
98184 lastRenderSeek . set ( timeline , t ) ;
99- const frames = last === undefined ? Math . floor ( t * fps + 1e-6 ) + 1 : ( t - last ) * fps ;
100- const skipped = Math . round ( frames ) - 1 ;
101- if ( skipped < 1 || Math . abs ( frames - Math . round ( frames ) ) > 1e-6 ) return ;
102- for ( let back = skipped ; back >= 1 ; back -- ) timeline . totalTime ( t - back / fps , suppressEvents ) ;
185+ const skipped =
186+ last === undefined ? Math . floor ( t * fps + 1e-6 ) : Math . ceil ( ( t - last ) * fps - 1e-6 ) - 1 ;
187+ if ( skipped < 1 ) return [ ] ;
188+ let cached = sharedPropertySpans . get ( timeline ) ;
189+ if ( cached ?. version !== version ) {
190+ cached = { version, spans : spansWhereTweensShareAProperty ( timeline ) } ;
191+ sharedPropertySpans . set ( timeline , cached ) ;
192+ }
193+ const replay = new Set < number > ( ) ;
194+ for ( const [ from , to ] of cached . spans ) {
195+ const nearest = Math . max ( 1 , Math . ceil ( ( t - to ) * fps - 1e-6 ) - 1 ) ;
196+ const farthest = Math . min ( skipped , Math . floor ( ( t - from ) * fps + 1e-6 ) + 1 ) ;
197+ for ( let back = nearest ; back <= farthest ; back ++ ) replay . add ( back ) ;
198+ }
199+ return [ ...replay ] . sort ( ( x , y ) => y - x ) . map ( ( back ) => t - back / fps ) ;
103200}
104201
105202export function seekGsapTimeline (
@@ -113,7 +210,11 @@ export function seekGsapTimeline(
113210 if ( typeof timeline . seek === "function" ) timeline . seek ( t , suppressEvents ) ;
114211 return false ;
115212 }
116- if ( renderFps ) renderFramesSkippedSinceLastSeek ( timeline , t , suppressEvents , renderFps ) ;
213+ if ( renderFps ) {
214+ for ( const time of framesSkippedSinceLastSeek ( timeline , t , renderFps , structure ?. version ) ) {
215+ timeline . totalTime ( time , suppressEvents ) ;
216+ }
217+ }
117218 const from = Number ( timeline . totalTime ( ) ) ;
118219 timeline . totalTime ( t , suppressEvents ) ;
119220 if ( t < from && structure ) reapplyStepsInAuthoredOrder ( timeline , structure , t ) ;
0 commit comments