@@ -13,7 +13,7 @@ import { CaptureMenu } from "./embeddedFrontend/captureMenu/captureMenu";
13
13
import { ResultView } from "./embeddedFrontend/resultView/resultView" ;
14
14
15
15
export interface IAvailableContext {
16
- readonly canvas : HTMLCanvasElement ;
16
+ readonly canvas : HTMLCanvasElement | OffscreenCanvas ;
17
17
readonly contextSpy : ContextSpy ;
18
18
}
19
19
@@ -22,8 +22,12 @@ export const EmbeddedFrontend = {
22
22
ResultView,
23
23
} ;
24
24
25
+ interface IAnnotatedOffscreenCanvas extends OffscreenCanvas {
26
+ __spector_context_type ?: string ;
27
+ }
28
+
25
29
export class Spector {
26
- public static getFirstAvailable3dContext ( canvas : HTMLCanvasElement ) : WebGLRenderingContexts {
30
+ public static getFirstAvailable3dContext ( canvas : HTMLCanvasElement | OffscreenCanvas ) : WebGLRenderingContexts {
27
31
// Custom detection to run in the extension.
28
32
return this . tryGetContextFromHelperField ( canvas ) ||
29
33
this . tryGetContextFromCanvas ( canvas , "webgl" ) ||
@@ -32,19 +36,25 @@ export class Spector {
32
36
this . tryGetContextFromCanvas ( canvas , "experimental-webgl2" ) ;
33
37
}
34
38
35
- private static tryGetContextFromHelperField ( canvas : HTMLCanvasElement ) : WebGLRenderingContexts {
36
- const type = canvas . getAttribute ( "__spector_context_type" ) ;
39
+ private static tryGetContextFromHelperField ( canvas : HTMLCanvasElement | OffscreenCanvas ) : WebGLRenderingContexts {
40
+ const type : string | void = canvas instanceof HTMLCanvasElement ?
41
+ canvas . getAttribute ( "__spector_context_type" ) :
42
+ ( canvas as IAnnotatedOffscreenCanvas ) . __spector_context_type ;
43
+
37
44
if ( type ) {
38
45
return this . tryGetContextFromCanvas ( canvas , type ) ;
39
46
}
40
47
41
48
return undefined ;
42
49
}
43
50
44
- private static tryGetContextFromCanvas ( canvas : HTMLCanvasElement , type : string ) : WebGLRenderingContexts {
51
+ private static tryGetContextFromCanvas ( canvas : HTMLCanvasElement | OffscreenCanvas , type : string ) : WebGLRenderingContexts {
45
52
let context : WebGLRenderingContexts ;
46
53
try {
47
- context = canvas . getContext ( type ) as WebGLRenderingContexts ;
54
+ // Cast canvas to any because lib.dom.d.ts types are not suitably
55
+ // general to allow for custom canvas context types that are
56
+ // potentially specified by __spector_context_type:
57
+ context = ( canvas as any ) . getContext ( type ) as WebGLRenderingContexts ;
48
58
}
49
59
catch ( e ) {
50
60
// Nothing to do here, canvas has not been found.;
@@ -208,7 +218,7 @@ export class Spector {
208
218
this . canvasSpy . onContextRequested . add ( this . spyContext , this ) ;
209
219
}
210
220
211
- public spyCanvas ( canvas : HTMLCanvasElement ) : void {
221
+ public spyCanvas ( canvas : HTMLCanvasElement | OffscreenCanvas ) : void {
212
222
if ( this . canvasSpy ) {
213
223
this . onErrorInternal ( "Already spying canvas." ) ;
214
224
return ;
@@ -222,7 +232,7 @@ export class Spector {
222
232
return this . getAvailableContexts ( ) ;
223
233
}
224
234
225
- public captureCanvas ( canvas : HTMLCanvasElement ,
235
+ public captureCanvas ( canvas : HTMLCanvasElement | OffscreenCanvas ,
226
236
commandCount = 0 ,
227
237
quickCapture : boolean = false ) : void {
228
238
@@ -245,7 +255,7 @@ export class Spector {
245
255
commandCount = 0 ,
246
256
quickCapture : boolean = false ) : void {
247
257
248
- let contextSpy = this . getAvailableContextSpyByCanvas ( context . canvas as HTMLCanvasElement ) ;
258
+ let contextSpy = this . getAvailableContextSpyByCanvas ( context . canvas as HTMLCanvasElement | OffscreenCanvas ) ;
249
259
250
260
if ( ! contextSpy ) {
251
261
if ( ( context as WebGL2RenderingContext ) . getIndexedParameter ) {
@@ -266,7 +276,7 @@ export class Spector {
266
276
contextSpy . onMaxCommand . add ( this . stopCapture , this ) ;
267
277
268
278
this . contexts . push ( {
269
- canvas : contextSpy . context . canvas as HTMLCanvasElement ,
279
+ canvas : contextSpy . context . canvas as HTMLCanvasElement | OffscreenCanvas ,
270
280
contextSpy,
271
281
} ) ;
272
282
}
@@ -314,26 +324,26 @@ export class Spector {
314
324
}
315
325
}
316
326
317
- public captureNextFrame ( obj : HTMLCanvasElement | WebGLRenderingContexts ,
327
+ public captureNextFrame ( obj : HTMLCanvasElement | OffscreenCanvas | WebGLRenderingContexts ,
318
328
quickCapture : boolean = false ) : void {
319
329
320
- if ( obj instanceof HTMLCanvasElement ) {
330
+ if ( obj instanceof HTMLCanvasElement || ( self . OffscreenCanvas && obj instanceof OffscreenCanvas ) ) {
321
331
this . captureCanvas ( obj , 0 , quickCapture ) ;
322
332
}
323
333
else {
324
- this . captureContext ( obj , 0 , quickCapture ) ;
334
+ this . captureContext ( obj as WebGLRenderingContexts , 0 , quickCapture ) ;
325
335
}
326
336
}
327
337
328
- public startCapture ( obj : HTMLCanvasElement | WebGLRenderingContexts ,
338
+ public startCapture ( obj : HTMLCanvasElement | OffscreenCanvas | WebGLRenderingContexts ,
329
339
commandCount : number ,
330
340
quickCapture : boolean = false ) : void {
331
341
332
- if ( obj instanceof HTMLCanvasElement ) {
342
+ if ( obj instanceof HTMLCanvasElement || ( self . OffscreenCanvas && obj instanceof OffscreenCanvas ) ) {
333
343
this . captureCanvas ( obj , commandCount , quickCapture ) ;
334
344
}
335
345
else {
336
- this . captureContext ( obj , commandCount , quickCapture ) ;
346
+ this . captureContext ( obj as WebGLRenderingContexts , commandCount , quickCapture ) ;
337
347
}
338
348
}
339
349
@@ -397,7 +407,7 @@ export class Spector {
397
407
}
398
408
399
409
private spyContext ( contextInformation : IContextInformation ) {
400
- let contextSpy = this . getAvailableContextSpyByCanvas ( contextInformation . context . canvas as HTMLCanvasElement ) ;
410
+ let contextSpy = this . getAvailableContextSpyByCanvas ( contextInformation . context . canvas as HTMLCanvasElement | OffscreenCanvas ) ;
401
411
if ( ! contextSpy ) {
402
412
contextSpy = new ContextSpy ( {
403
413
context : contextInformation . context ,
@@ -408,15 +418,15 @@ export class Spector {
408
418
contextSpy . onMaxCommand . add ( this . stopCapture , this ) ;
409
419
410
420
this . contexts . push ( {
411
- canvas : contextSpy . context . canvas as HTMLCanvasElement ,
421
+ canvas : contextSpy . context . canvas as HTMLCanvasElement | OffscreenCanvas ,
412
422
contextSpy,
413
423
} ) ;
414
424
}
415
425
416
426
contextSpy . spy ( ) ;
417
427
}
418
428
419
- private getAvailableContextSpyByCanvas ( canvas : HTMLCanvasElement ) : ContextSpy {
429
+ private getAvailableContextSpyByCanvas ( canvas : HTMLCanvasElement | OffscreenCanvas ) : ContextSpy {
420
430
for ( const availableContext of this . contexts ) {
421
431
if ( availableContext . canvas === canvas ) {
422
432
return availableContext . contextSpy ;
0 commit comments