@@ -32,6 +32,10 @@ export class CanvasManager {
3232 private pendingCanvasMutations : pendingCanvasMutationsMap = new Map ( ) ;
3333 private rafStamps : RafStamps = { latestId : 0 , invokeId : null } ;
3434 private mirror : Mirror ;
35+ private logger ?: {
36+ debug : ( ...args : Parameters < typeof console . debug > ) => void ;
37+ warn : ( ...args : Parameters < typeof console . warn > ) => void ;
38+ } ;
3539
3640 private mutationCb : canvasMutationCallback ;
3741 private resetObservers ?: listenerHandler ;
@@ -71,6 +75,10 @@ export class CanvasManager {
7175 resizeQuality ?: 'pixelated' | 'low' | 'medium' | 'high' ;
7276 resizeFactor ?: number ;
7377 maxSnapshotDimension ?: number ;
78+ logger ?: {
79+ debug : ( ...args : Parameters < typeof console . debug > ) => void ;
80+ warn : ( ...args : Parameters < typeof console . warn > ) => void ;
81+ } ;
7482 } ) {
7583 const {
7684 sampling = 'all' ,
@@ -82,6 +90,7 @@ export class CanvasManager {
8290 } = options ;
8391 this . mutationCb = options . mutationCb ;
8492 this . mirror = options . mirror ;
93+ this . logger = options . logger ;
8594
8695 if ( recordCanvas && sampling === 'all' )
8796 this . initCanvasMutationObserver ( win , blockClass , blockSelector ) ;
@@ -100,6 +109,18 @@ export class CanvasManager {
100109 ) ;
101110 }
102111
112+ private debug (
113+ canvas ?: HTMLCanvasElement ,
114+ ...args : Parameters < typeof console . log >
115+ ) {
116+ if ( ! this . logger ) return ;
117+ let prefix = '[highlight-canvas]' ;
118+ if ( canvas ) {
119+ prefix += ` [ctx:${ ( canvas as ICanvas ) . __context } ]` ;
120+ }
121+ this . logger . debug ( prefix , canvas , ...args ) ;
122+ }
123+
103124 private processMutation : canvasManagerMutationCallback = (
104125 target ,
105126 mutation ,
@@ -184,6 +205,7 @@ export class CanvasManager {
184205 const matchedCanvas : HTMLCanvasElement [ ] = [ ] ;
185206 win . document . querySelectorAll ( 'canvas' ) . forEach ( ( canvas ) => {
186207 if ( ! isBlocked ( canvas , blockClass , blockSelector , true ) ) {
208+ this . debug ( canvas , 'discovered canvas' ) ;
187209 matchedCanvas . push ( canvas ) ;
188210 }
189211 } ) ;
@@ -200,12 +222,15 @@ export class CanvasManager {
200222 }
201223 lastSnapshotTime = timestamp ;
202224
203- getCanvas ( )
204- // eslint-disable-next-line @typescript-eslint/no-misused-promises
205- . forEach ( async ( canvas : HTMLCanvasElement ) => {
206- const id = this . mirror . getId ( canvas ) ;
207- if ( snapshotInProgressMap . get ( id ) ) return ;
208- snapshotInProgressMap . set ( id , true ) ;
225+ getCanvas ( ) . forEach ( async ( canvas : HTMLCanvasElement ) => {
226+ this . debug ( canvas , 'starting snapshotting' ) ;
227+ const id = this . mirror . getId ( canvas ) ;
228+ if ( snapshotInProgressMap . get ( id ) ) {
229+ this . debug ( canvas , 'snapshotting already in progress for' , id ) ;
230+ return ;
231+ }
232+ snapshotInProgressMap . set ( id , true ) ;
233+ try {
209234 if ( [ 'webgl' , 'webgl2' ] . includes ( ( canvas as ICanvas ) . __context ) ) {
210235 // if the canvas hasn't been modified recently,
211236 // its contents won't be in memory and `createImageBitmap`
@@ -231,6 +256,10 @@ export class CanvasManager {
231256 // canvas is not yet ready... this retry on the next sampling iteration.
232257 // we don't want to crash the worker if the canvas is not yet rendered.
233258 if ( canvas . width === 0 || canvas . height === 0 ) {
259+ this . debug ( canvas , 'not yet ready' , {
260+ width : canvas . width ,
261+ height : canvas . height ,
262+ } ) ;
234263 return ;
235264 }
236265 let scale = resizeFactor || 1 ;
@@ -241,11 +270,18 @@ export class CanvasManager {
241270 const width = canvas . width * scale ;
242271 const height = canvas . height * scale ;
243272
273+ window . performance . mark ( `canvas-${ canvas . id } -snapshot` ) ;
244274 const bitmap = await createImageBitmap ( canvas , {
245275 resizeQuality : resizeQuality || 'low' ,
246276 resizeWidth : width ,
247277 resizeHeight : height ,
248278 } ) ;
279+ this . debug (
280+ canvas ,
281+ 'took a snapshot in' ,
282+ window . performance . measure ( `canvas-snapshot` ) ,
283+ ) ;
284+ window . performance . mark ( `canvas-postMessage` ) ;
249285 worker . postMessage (
250286 {
251287 id,
@@ -258,7 +294,15 @@ export class CanvasManager {
258294 } ,
259295 [ bitmap ] ,
260296 ) ;
261- } ) ;
297+ this . debug (
298+ canvas ,
299+ 'send message in' ,
300+ window . performance . measure ( `canvas-postMessage` ) ,
301+ ) ;
302+ } finally {
303+ snapshotInProgressMap . set ( id , false ) ;
304+ }
305+ } ) ;
262306 rafId = requestAnimationFrame ( takeCanvasSnapshots ) ;
263307 } ;
264308
0 commit comments