@@ -18,10 +18,12 @@ export type DrawTextProps = {
1818 fillColor ?: string
1919 durationFrames ?: number
2020 delayFrames ?: number
21+ lagRatio ?: number
2122 fillDurationFrames ?: number
2223 fillDelayFrames ?: number
2324 outStartFrames ?: number
2425 outDurationFrames ?: number
26+ outLagRatio ?: number
2527 lineHeight ?: number
2628 align ?: "left" | "center" | "right"
2729 style ?: CSSProperties
@@ -40,10 +42,12 @@ export type DrawTexProps = {
4042 fillColor ?: string
4143 durationFrames ?: number
4244 delayFrames ?: number
45+ lagRatio ?: number
4346 fillDurationFrames ?: number
4447 fillDelayFrames ?: number
4548 outStartFrames ?: number
4649 outDurationFrames ?: number
50+ outLagRatio ?: number
4751 displayMode ?: boolean
4852 style ?: CSSProperties
4953}
@@ -55,6 +59,7 @@ type GlyphPath = {
5559}
5660
5761const clamp01 = ( value : number ) => Math . min ( 1 , Math . max ( 0 , value ) )
62+ const DEFAULT_LAG_RATIO = 0.6
5863const fontCache = new Map < string , Promise < Font > > ( )
5964const DRAW_TEXT_TRACKER_KEY = "__frameScript_DrawTextTracker"
6065
@@ -179,10 +184,12 @@ const renderGlyphSvg = (params: {
179184 resolvedFillColor : string
180185 durationFrames : number
181186 delayFrames : number
187+ lagRatio ?: number
182188 fillDurationFrames : number
183189 fillDelayFrames : number
184190 outStartFrames ?: number
185191 outDurationFrames ?: number
192+ outLagRatio ?: number
186193 style ?: CSSProperties
187194} ) => {
188195 const {
@@ -197,30 +204,42 @@ const renderGlyphSvg = (params: {
197204 resolvedFillColor,
198205 durationFrames,
199206 delayFrames,
207+ lagRatio,
200208 fillDurationFrames,
201209 fillDelayFrames,
202210 outStartFrames,
203211 outDurationFrames,
212+ outLagRatio,
204213 style,
205214 } = params
206215
207216 const drawCount = glyphs . reduce (
208217 ( count , glyph , index ) => count + ( ! glyph . isGap && ( glyphLengths [ index ] ?? 0 ) > 0 ? 1 : 0 ) ,
209218 0 ,
210219 )
211- const perGlyphFrames = drawCount > 0 ? Math . max ( 1 , Math . round ( durationFrames / drawCount ) ) : 0
220+ const safeLagRatio = Number . isFinite ( lagRatio ) ? Math . max ( 0 , lagRatio ?? 0 ) : DEFAULT_LAG_RATIO
221+ const safeOutLagRatio = Number . isFinite ( outLagRatio )
222+ ? Math . max ( 0 , outLagRatio ?? 0 )
223+ : safeLagRatio
224+ const inDenom = 1 + safeLagRatio * Math . max ( 0 , drawCount - 1 )
225+ const perGlyphFrames =
226+ drawCount > 0 ? Math . max ( 1 , Math . round ( durationFrames / Math . max ( 1 , inDenom ) ) ) : 0
227+ const stepFrames = drawCount > 1 ? Math . max ( 1 , Math . round ( perGlyphFrames * safeLagRatio ) ) : perGlyphFrames
228+ const outDenom = 1 + safeOutLagRatio * Math . max ( 0 , drawCount - 1 )
212229 const outPerGlyphFrames =
213230 outDurationFrames && outDurationFrames > 0 && drawCount > 0
214- ? Math . max ( 1 , Math . round ( outDurationFrames / drawCount ) )
231+ ? Math . max ( 1 , Math . round ( outDurationFrames / Math . max ( 1 , outDenom ) ) )
215232 : 0
233+ const outStepFrames =
234+ drawCount > 1 ? Math . max ( 1 , Math . round ( outPerGlyphFrames * safeOutLagRatio ) ) : outPerGlyphFrames
216235
217236 let cursor = delayFrames
218237 const glyphTimings = glyphs . map ( ( glyph , index ) => {
219238 if ( glyph . isGap || ( glyphLengths [ index ] ?? 0 ) <= 0 ) {
220239 return { start : cursor , duration : 0 }
221240 }
222241 const start = cursor
223- cursor += perGlyphFrames
242+ cursor += stepFrames
224243 return { start, duration : perGlyphFrames }
225244 } )
226245
@@ -236,7 +255,7 @@ const renderGlyphSvg = (params: {
236255 for ( let i = drawable . length - 1 ; i >= 0 ; i -= 1 ) {
237256 const index = drawable [ i ]
238257 outTimings [ index ] = { start : outCursor , duration : outPerGlyphFrames }
239- outCursor += outPerGlyphFrames
258+ outCursor += outStepFrames
240259 }
241260 }
242261
@@ -603,10 +622,12 @@ export const DrawText = ({
603622 fillColor,
604623 durationFrames = 180 ,
605624 delayFrames = 0 ,
625+ lagRatio = 0.5 ,
606626 fillDurationFrames = 18 ,
607627 fillDelayFrames = 0 ,
608628 outStartFrames,
609629 outDurationFrames,
630+ outLagRatio = 0.5 ,
610631 lineHeight = 1.2 ,
611632 align = "left" ,
612633 style,
@@ -725,10 +746,12 @@ export const DrawText = ({
725746 resolvedFillColor,
726747 durationFrames,
727748 delayFrames,
749+ lagRatio,
728750 fillDurationFrames,
729751 fillDelayFrames,
730752 outStartFrames,
731753 outDurationFrames,
754+ outLagRatio,
732755 style,
733756 } )
734757}
@@ -751,10 +774,12 @@ export const DrawTex = ({
751774 fillColor,
752775 durationFrames = 180 ,
753776 delayFrames = 0 ,
777+ lagRatio = 0.5 ,
754778 fillDurationFrames = 18 ,
755779 fillDelayFrames = 0 ,
756780 outStartFrames,
757781 outDurationFrames,
782+ outLagRatio = 0.5 ,
758783 displayMode = false ,
759784 style,
760785} : DrawTexProps ) => {
@@ -864,10 +889,12 @@ export const DrawTex = ({
864889 resolvedFillColor,
865890 durationFrames,
866891 delayFrames,
892+ lagRatio,
867893 fillDurationFrames,
868894 fillDelayFrames,
869895 outStartFrames,
870896 outDurationFrames,
897+ outLagRatio,
871898 style,
872899 } )
873900}
0 commit comments