@@ -37,10 +37,7 @@ export function mergeVibrations(...patterns: Vibration[]): number[] {
3737 currentTime - startTime + interval
3838 ) ;
3939
40- // Mark the vibration period in the timeline
41- for ( let i = fromIndex ; i < toIndex ; i ++ ) {
42- timeline [ i ] = true ;
43- }
40+ timeline . fill ( true , fromIndex , toIndex ) ;
4441 }
4542 currentTime += interval ;
4643 isVibrating = ! isVibrating ;
@@ -49,12 +46,13 @@ export function mergeVibrations(...patterns: Vibration[]): number[] {
4946
5047 // Convert timeline back to intervals
5148 const result : number [ ] = [ ] ;
49+ const length = timeline . length ;
5250 let currentState = timeline [ 0 ] ;
5351 let currentCount = 0 ;
5452
5553 // Process the timeline including the final state transition
56- for ( let i = 0 ; i <= timeline . length ; i ++ ) {
57- const newState = i < timeline . length ? timeline [ i ] : ! currentState ;
54+ for ( let i = 0 ; i <= length ; i ++ ) {
55+ const newState = i < length ? timeline [ i ] : ! currentState ;
5856
5957 if ( newState !== currentState ) {
6058 if ( currentCount > 0 ) {
@@ -77,3 +75,36 @@ export function mergeVibrations(...patterns: Vibration[]): number[] {
7775
7876 return result ;
7977}
78+
79+ export function trimVibrations ( amount : number , patterns : number [ ] ) : number [ ] {
80+ // Initialize result array
81+ const result : number [ ] = [ ] ;
82+
83+ // Apply remaining amount to the first element
84+ let remainingAmount = amount ;
85+
86+ // Process each vibration in the pattern
87+ for ( let i = 0 ; i < patterns . length ; i ++ ) {
88+ const currentVibration = patterns [ i ] ;
89+
90+ // If we still have amount to trim
91+ if ( remainingAmount > 0 ) {
92+ // Calculate what's left after trimming
93+ const remaining = currentVibration - remainingAmount ;
94+
95+ // If there's duration left, add it to the result
96+ if ( remaining > 0 ) {
97+ result . push ( remaining ) ;
98+ remainingAmount = 0 ; // Used all the trim amount
99+ } else {
100+ // This vibration was completely consumed
101+ remainingAmount = Math . abs ( remaining ) ; // Carry over the remaining amount
102+ }
103+ } else {
104+ // No more trimming needed, add the vibration as is
105+ result . push ( currentVibration ) ;
106+ }
107+ }
108+
109+ return result ;
110+ }
0 commit comments