@@ -30,6 +30,31 @@ const DEFAULT_CANVAS_OPTIONS: CanvasContextOptions = {
3030const MARSHAL_REFERENCE_PROPERTIES = [ 'fillStyle' , 'strokeStyle' ] as const ;
3131const PRINTABLE_KEYS = [ 'Enter' , 'Tab' , 'Backspace' , 'Delete' ] ;
3232
33+ const GRADIENT_METHODS = new Set ( [ 'createLinearGradient' , 'createRadialGradient' , 'createConicGradient' ] ) ;
34+
35+ const sanitizeNonFiniteNumbers = ( args : readonly unknown [ ] ) : unknown [ ] => {
36+ let out : unknown [ ] | null = null ;
37+
38+ for ( let i = 0 ; i < args . length ; i ++ ) {
39+ const v = args [ i ] ;
40+ if ( typeof v === 'number' && ! Number . isFinite ( v ) ) {
41+ out ??= Array . from ( args ) ;
42+ out [ i ] = 0 ;
43+ }
44+ }
45+
46+ return out ?? ( args as unknown [ ] ) ;
47+ } ;
48+
49+ const sanitizeNonFiniteNumbersInPlace = ( args : unknown [ ] ) : void => {
50+ for ( let i = 0 ; i < args . length ; i ++ ) {
51+ const v = args [ i ] ;
52+ if ( typeof v === 'number' && ! Number . isFinite ( v ) ) {
53+ args [ i ] = 0 ;
54+ }
55+ }
56+ } ;
57+
3358/**
3459 * Creates a new BlazorCanvas2d API instance with isolated state
3560 * @returns A frozen BlazorexAPI instance
@@ -312,7 +337,10 @@ const createBlazorexAPIImpl = (): BlazorexAPI => {
312337
313338 // Hot path: no marshal reference => no need to copy/modify args
314339 if ( ! isMarshalReference ( firstParam ) ) {
315- return typedContext [ methodName ] ( ...parameters ) ;
340+ const safeParams = GRADIENT_METHODS . has ( methodName )
341+ ? sanitizeNonFiniteNumbers ( parameters )
342+ : ( parameters as unknown [ ] ) ;
343+ return typedContext [ methodName ] ( ...safeParams ) ;
316344 }
317345
318346 // Handle C# MarshalReference objects
@@ -321,6 +349,9 @@ const createBlazorexAPIImpl = (): BlazorexAPI => {
321349 if ( ! marshalRef . isElementRef ) {
322350 // This is a marshal object reference (gradient, pattern, etc.)
323351 const args = parameters . slice ( 1 ) ;
352+ if ( GRADIENT_METHODS . has ( methodName ) ) {
353+ sanitizeNonFiniteNumbersInPlace ( args ) ;
354+ }
324355 const existingObject = marshalledObjects . get ( marshalRef . id ) as
325356 | Record < string , Function >
326357 | undefined ;
0 commit comments