@@ -12,6 +12,7 @@ import {
1212} from "react" ;
1313import { assertNever } from "@argos/util/assertNever" ;
1414import { invariant } from "@argos/util/invariant" ;
15+ import { captureException } from "@sentry/react" ;
1516import { clsx } from "clsx" ;
1617import { useAtomValue } from "jotai/react" ;
1718import {
@@ -286,19 +287,16 @@ async function fetchBlob(url: string) {
286287}
287288
288289async function loadImageElement ( url : string ) {
289- const blob = await fetchBlob ( url ) ;
290- const objectUrl = URL . createObjectURL ( blob ) ;
291290 return await new Promise < HTMLImageElement > ( ( resolve , reject ) => {
292291 const image = new Image ( ) ;
292+ image . crossOrigin = "anonymous" ;
293293 image . onload = ( ) => {
294- URL . revokeObjectURL ( objectUrl ) ;
295294 resolve ( image ) ;
296295 } ;
297296 image . onerror = ( ) => {
298- URL . revokeObjectURL ( objectUrl ) ;
299297 reject ( new Error ( `Failed to load image: ${ url } ` ) ) ;
300298 } ;
301- image . src = objectUrl ;
299+ image . src = url ;
302300 } ) ;
303301}
304302
@@ -325,30 +323,58 @@ async function createMaskedCompareBlob(props: {
325323 loadImageElement ( props . maskUrl ) ,
326324 ] ) ;
327325
328- const width = compareImage . naturalWidth || compareImage . width ;
329- const height = compareImage . naturalHeight || compareImage . height ;
326+ const compareDimensions = {
327+ width : compareImage . naturalWidth ,
328+ height : compareImage . naturalHeight ,
329+ } ;
330+ const maskDimensions = {
331+ width : maskImage . naturalWidth ,
332+ height : maskImage . naturalHeight ,
333+ } ;
334+ const resultDimensions = {
335+ width : Math . max ( compareDimensions . width , maskDimensions . width ) ,
336+ height : Math . max ( compareDimensions . height , maskDimensions . height ) ,
337+ } ;
330338 const canvas = document . createElement ( "canvas" ) ;
331- canvas . width = width ;
332- canvas . height = height ;
339+ canvas . width = resultDimensions . width ;
340+ canvas . height = resultDimensions . height ;
333341 const context = canvas . getContext ( "2d" ) ;
334342 invariant ( context , "Expected canvas to have a 2d context" ) ;
335343
336- context . drawImage ( compareImage , 0 , 0 , width , height ) ;
344+ context . drawImage (
345+ compareImage ,
346+ 0 ,
347+ 0 ,
348+ compareDimensions . width ,
349+ compareDimensions . height ,
350+ ) ;
337351
338352 const overlayCanvas = document . createElement ( "canvas" ) ;
339- overlayCanvas . width = width ;
340- overlayCanvas . height = height ;
353+ overlayCanvas . width = maskDimensions . width ;
354+ overlayCanvas . height = maskDimensions . height ;
341355 const overlayContext = overlayCanvas . getContext ( "2d" ) ;
342356 invariant ( overlayContext , "Expected overlay canvas to have a 2d context" ) ;
343357
344358 overlayContext . fillStyle = props . color ;
345359 overlayContext . globalAlpha = props . opacity ;
346- overlayContext . fillRect ( 0 , 0 , width , height ) ;
360+ overlayContext . fillRect ( 0 , 0 , maskDimensions . width , maskDimensions . height ) ;
347361 overlayContext . globalAlpha = 1 ;
348362 overlayContext . globalCompositeOperation = "destination-in" ;
349- overlayContext . drawImage ( maskImage , 0 , 0 , width , height ) ;
363+ overlayContext . drawImage (
364+ maskImage ,
365+ 0 ,
366+ 0 ,
367+ maskDimensions . width ,
368+ maskDimensions . height ,
369+ ) ;
350370
351- context . drawImage ( overlayCanvas , 0 , 0 , width , height ) ;
371+ context . drawImage (
372+ overlayCanvas ,
373+ 0 ,
374+ 0 ,
375+ maskDimensions . width ,
376+ maskDimensions . height ,
377+ ) ;
352378
353379 return canvasToBlob ( canvas ) ;
354380}
@@ -703,7 +729,11 @@ function DownloadCompareScreenshotButton({
703729 toast . promise ( promise , {
704730 loading : "Downloading image…" ,
705731 success : "Image downloaded" ,
706- error : ( data ) => getErrorMessage ( data ) ,
732+ error : ( data ) => {
733+ console . error ( data ) ;
734+ captureException ( data ) ;
735+ return getErrorMessage ( data ) ;
736+ } ,
707737 } ) ;
708738 } ;
709739
0 commit comments