@@ -163,14 +163,47 @@ class AnimatedPath
163163 * @param {* } t
164164 * @returns
165165 */
166- static tweenFill ( a , b , t )
166+ static tweenFill ( ctx , a , b , t )
167167 {
168- if ( typeof a == "string" )
168+ if ( typeof a == "string" && typeof b == "string" )
169169 {
170170 return AnimatedPath . tweenHex ( a , b , t ) ;
171171 }
172+ if ( a . type )
173+ {
174+ let graddef = AnimatedPath . tweenGradient ( a , b , t ) ;
175+ switch ( graddef . type )
176+ {
177+ case "linear" :
178+ {
179+ let grad = ctx . createLinearGradient ( ...graddef . coords ) ;
180+ for ( let i = 0 ; i < graddef . stops . length ; i ++ )
181+ {
182+ grad . addColorStop ( graddef . stops [ i ] , graddef . colours [ i ] ) ;
183+ }
184+ return grad ;
185+ }
186+ }
187+ }
172188
173189 }
190+ static tweenGradient ( a , b , t )
191+ {
192+ let finalcoords = [ ] ;
193+ let finalcolours = [ ] ;
194+ a . coords . forEach ( ( val , i ) => {
195+ finalcoords . push ( a . coords [ i ] + ( b . coords [ i ] - a . coords [ i ] ) * t ) ;
196+ } ) ;
197+ a . colours . forEach ( ( val , i ) => {
198+ finalcolours . push ( AnimatedPath . tweenHex ( a . colours [ i ] , b . colours [ i ] , t ) ) ;
199+ } ) ;
200+ return {
201+ type : a . type ,
202+ coords :finalcoords ,
203+ stops :a . stops ,
204+ colours :finalcolours
205+ } ;
206+ }
174207 /**
175208 * Tweens between two hex colours
176209 * @param {string } a first colour as #RRGGBB
@@ -184,6 +217,13 @@ class AnimatedPath
184217 {
185218 return "#000000" ;
186219 }
220+ let aa = 255 ;
221+ let ba = 255 ;
222+ if ( a . length >= 9 )
223+ {
224+ aa = parseInt ( a . substring ( 7 , 9 ) , 16 ) ;
225+ ba = parseInt ( b . substring ( 7 , 9 ) , 16 ) ;
226+ }
187227 let ar = parseInt ( a . substring ( 1 , 3 ) , 16 ) ;
188228 let ag = parseInt ( a . substring ( 3 , 5 ) , 16 ) ;
189229 let ab = parseInt ( a . substring ( 5 , 7 ) , 16 ) ;
@@ -193,18 +233,20 @@ class AnimatedPath
193233 let R = ar + ( br - ar ) * t ;
194234 let G = ag + ( bg - ag ) * t ;
195235 let B = ab + ( bb - ab ) * t ;
196- return AnimatedPath . rgbToHex ( Math . floor ( R ) , Math . floor ( G ) , Math . floor ( B ) ) ;
236+ let A = aa + ( ba - aa ) * t ;
237+ return AnimatedPath . rgbToHex ( Math . floor ( R ) , Math . floor ( G ) , Math . floor ( B ) , Math . floor ( A ) ) ;
197238 }
198239 /**
199240 * Converts 3 values into hexadecimal RGB
200241 * @param {number } r
201242 * @param {number } g
202243 * @param {number } b
244+ * @param {number } a
203245 * @returns
204246 */
205- static rgbToHex ( r , g , b )
247+ static rgbToHex ( r , g , b , a = 255 )
206248 {
207- return "#" + AnimatedPath . componentToHex ( r ) + AnimatedPath . componentToHex ( g ) + AnimatedPath . componentToHex ( b ) ;
249+ return "#" + AnimatedPath . componentToHex ( r ) + AnimatedPath . componentToHex ( g ) + AnimatedPath . componentToHex ( b ) + AnimatedPath . componentToHex ( a ) ;
208250 }
209251 /**
210252 * Converts a single value to hex.
@@ -234,7 +276,11 @@ class AnimatedPath
234276 frames . forEach ( ( f ) => {
235277 // also save the paths in parallel for processing
236278 paths . push ( f . path ) ;
237- result . frames . push ( { "time" : f . time , "fill" :f . fill , "rotation" :( f . rotation ?? 0 ) } ) ;
279+ result . frames . push ( {
280+ "time" : f . time ,
281+ "fill" :f . fill ,
282+ "rotation" :( f . rotation ?? 0 )
283+ } ) ;
238284 } ) ;
239285 let chunked = [ ] ;
240286 // chunk the paths
@@ -261,7 +307,6 @@ class AnimatedPath
261307 "values" : f0 . values
262308 } ) ;
263309 result . length = 1 ;
264- console . log ( result ) ;
265310 }
266311 return result ;
267312 }
@@ -378,7 +423,7 @@ class VectorAnimation
378423 // generate the SVG path commands from the path and the keyframe values
379424 let strpath = AnimatedPath . stitch ( path . path , values ) ;
380425 // tween fill
381- let fill = AnimatedPath . tweenFill ( frame . fa , frame . fb , frame . t ) ;
426+ let fill = AnimatedPath . tweenFill ( ctx , frame . fa , frame . fb , frame . t ) ;
382427 // render the path in its fill colour
383428 ctx . fillStyle = fill ;
384429 // rotate
0 commit comments