@@ -427,25 +427,31 @@ function snapshotScript(...targetIds: (string | undefined)[]) {
427
427
for ( const canvas of canvasElements ) {
428
428
const context = canvas . getContext ( '2d' ) ! ;
429
429
430
- const boundingRect = canvas . getBoundingClientRect ( ) ;
431
- const xStart = boundingRect . left / window . innerWidth ;
432
- const yStart = boundingRect . top / window . innerHeight ;
433
- const xEnd = boundingRect . right / window . innerWidth ;
434
- const yEnd = boundingRect . bottom / window . innerHeight ;
435
-
436
- const partiallyUncaptured = xEnd > 1 || yEnd > 1 ;
437
- const fullyUncaptured = xStart > 1 || yStart > 1 ;
430
+ const boundingRectAttribute = canvas . getAttribute ( '__playwright_bounding_rect__' ) ;
431
+ canvas . removeAttribute ( '__playwright_bounding_rect__' ) ;
432
+ if ( ! boundingRectAttribute )
433
+ continue ;
434
+
435
+ let boundingRect : { left : number , top : number , right : number , bottom : number } ;
436
+ try {
437
+ boundingRect = JSON . parse ( boundingRectAttribute ) ;
438
+ } catch ( e ) {
439
+ continue ;
440
+ }
441
+
442
+ const partiallyUncaptured = boundingRect . right > 1 || boundingRect . bottom > 1 ;
443
+ const fullyUncaptured = boundingRect . left > 1 || boundingRect . top > 1 ;
438
444
if ( fullyUncaptured ) {
439
445
canvas . title = `Playwright couldn't capture canvas contents because it's located outside the viewport.` ;
440
446
continue ;
441
447
}
442
448
443
449
drawCheckerboard ( context , canvas ) ;
444
450
445
- context . drawImage ( img , xStart * img . width , yStart * img . height , ( xEnd - xStart ) * img . width , ( yEnd - yStart ) * img . height , 0 , 0 , canvas . width , canvas . height ) ;
451
+ context . drawImage ( img , boundingRect . left * img . width , boundingRect . top * img . height , ( boundingRect . right - boundingRect . left ) * img . width , ( boundingRect . bottom - boundingRect . top ) * img . height , 0 , 0 , canvas . width , canvas . height ) ;
446
452
if ( isUnderTest )
447
453
// eslint-disable-next-line no-console
448
- console . log ( `canvas drawn:` , JSON . stringify ( [ xStart , yStart , xEnd , yEnd ] . map ( v => Math . floor ( v * 100 ) ) ) ) ;
454
+ console . log ( `canvas drawn:` , JSON . stringify ( [ boundingRect . left , boundingRect . top , ( boundingRect . right - boundingRect . left ) , ( boundingRect . bottom - boundingRect . top ) ] . map ( v => Math . floor ( v * 100 ) ) ) ) ;
449
455
450
456
if ( partiallyUncaptured )
451
457
canvas . title = `Playwright couldn't capture full canvas contents because it's located partially outside the viewport.` ;
0 commit comments