Skip to content

Commit 44e0ddb

Browse files
committed
chore: add some comments
1 parent b0c4080 commit 44e0ddb

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

packages/image-comparison-core/src/methods/screenshots.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,32 @@ export async function getDesktopFullPageScreenshotsData(browserInstance:Webdrive
279279

280280
const { capabilities } = browserInstance
281281
const browserName = (capabilities?.browserName || '').toLowerCase()
282+
// Safari desktop returns the browser mask with rounded corners and a drop shadow, so we need to fix this
282283
const isSafariDesktop = browserName.includes('safari') && !browserInstance.isMobile
283-
284284
const safariTopDropShadowCssPixels = isSafariDesktop ? Math.round(1 * devicePixelRatio) : 0
285285
const safariBottomCropOffsetCssPixels = isSafariDesktop ? Math.round(10 * devicePixelRatio) : 0
286-
286+
// For Safari desktop, calculate effective scroll increment
287+
// First image: scroll by 0, use full height (e.g.716px), crop 10px from bottom
288+
// Subsequent images: scroll by (actualInnerHeight - dropShadowOffset - bottomCropOffset) = 705px, crop 1px from top and 10px from bottom
287289
const effectiveScrollIncrement = isSafariDesktop
288290
? actualInnerHeight - safariTopDropShadowCssPixels - safariBottomCropOffsetCssPixels
289291
: actualInnerHeight
290-
291292
// Start with an empty array, during the scroll it will be filled because a page could also have a lazy loading
292293
const amountOfScrollsArray = []
293294
let scrollHeight: number | undefined
294295
let screenshotSize
295296

296297
for (let i = 0; i <= amountOfScrollsArray.length; i++) {
298+
// Determine and start scrolling
299+
// For Safari desktop: first image scrolls to 0, subsequent images scroll by effectiveScrollIncrement (715px)
300+
// Image 0: scrollY = 0
301+
// Image 1: scrollY = 715 (effectiveScrollIncrement)
302+
// Image 2: scrollY = 1430 (2 * effectiveScrollIncrement)
303+
// etc.
297304
const scrollY = isSafariDesktop
298305
? (i === 0 ? 0 : i * effectiveScrollIncrement)
299306
: actualInnerHeight * i
307+
300308
await browserInstance.execute(scrollToPosition, scrollY)
301309

302310
// Simply wait the amount of time specified for lazy-loading
@@ -327,55 +335,77 @@ export async function getDesktopFullPageScreenshotsData(browserInstance:Webdrive
327335

328336
scrollHeight = await browserInstance.execute(getDocumentScrollHeight)
329337

338+
// For Safari desktop, use effectiveScrollIncrement for the scroll check
330339
const scrollCheckHeight = isSafariDesktop ? effectiveScrollIncrement : actualInnerHeight
340+
331341
if (scrollHeight && (scrollY + scrollCheckHeight < scrollHeight) && screenshotSize.height === actualInnerHeight) {
332342
amountOfScrollsArray.push(amountOfScrollsArray.length)
333343
}
334-
// There is no else, Lazy load and large screenshots,
335-
// like with older drivers such as FF <= 47 and IE11, will not work
336344

345+
// The height of the image of the last 1 could be different
346+
// For Safari desktop, account for first image being full height and subsequent images being cropped
337347
const isFirstImage = i === 0
338348
const isLastImage = amountOfScrollsArray.length === i
339349
let imageHeight: number
340350
if (scrollHeight && isLastImage) {
341351
if (isSafariDesktop) {
352+
// Calculate remaining content: scrollHeight - (firstImageHeight + (numberOfPreviousImages - 1) * effectiveScrollIncrement)
342353
const numberOfPreviousImages = viewportScreenshots.length
343354
const totalPreviousHeight = numberOfPreviousImages === 0
344355
? 0
345356
: actualInnerHeight + (numberOfPreviousImages - 1) * effectiveScrollIncrement
346357
const remainingContent = scrollHeight - totalPreviousHeight
347-
358+
// For the last image, we need to be smart:
359+
// - If remainingContent >= actualInnerHeight: it's a full screenshot, treat it like a regular non-first image
360+
// (crop 1px from top, visible height = 705px, but last image doesn't crop bottom, so add 10px)
361+
// - If remainingContent < actualInnerHeight: it's a partial screenshot
362+
// For partial screenshots, we're cropping from a position that doesn't include the drop shadow at pixel 0
363+
// Last image doesn't crop bottom, so we need to add 10px to account for that
348364
imageHeight = remainingContent >= actualInnerHeight
349365
? effectiveScrollIncrement + safariBottomCropOffsetCssPixels
350366
: remainingContent + safariBottomCropOffsetCssPixels
351367
} else {
352368
imageHeight = scrollHeight - actualInnerHeight * viewportScreenshots.length
353369
}
354370
} else {
371+
// Non-last images: use full height for first, effectiveScrollIncrement for subsequent
372+
// For non-first images, effectiveScrollIncrement already accounts for top and bottom crops
355373
imageHeight = isSafariDesktop && !isFirstImage
356374
? effectiveScrollIncrement
357375
: screenshotSize.height
358376
}
359377

378+
// The starting position for cropping could be different for the last image (0 means no cropping)
379+
// For Safari desktop, crop 1px from top for all images except first
360380
if (isSafariDesktop && isFirstImage && safariBottomCropOffsetCssPixels > 0) {
361381
imageHeight -= safariBottomCropOffsetCssPixels
362382
}
363383

384+
// The starting position for cropping could be different for the last image (0 means no cropping)
385+
// For Safari desktop, crop 1px from top for all images except first
364386
let imageYPosition: number
365387
if (isSafariDesktop) {
366388
if (isLastImage && !isFirstImage) {
389+
// Last image: need to handle two cases
367390
const numberOfPreviousImages = viewportScreenshots.length
368391
const totalPreviousHeight = numberOfPreviousImages === 0
369392
? 0
370393
: actualInnerHeight + (numberOfPreviousImages - 1) * effectiveScrollIncrement
371394
const remainingContent = scrollHeight ? scrollHeight - totalPreviousHeight : 0
372395

396+
// Full screenshot: treat like regular non-first image (crop 1px from top)
397+
// Partial screenshot: we want to show the last remainingContent pixels
398+
// But we need to include the bottom 10px that we're not cropping, so start 10px higher
399+
// imageHeight = remainingContent, so we start at: 716 - remainingContent - 10px
400+
// This way we crop 10px higher to include the bottom corners
373401
imageYPosition = remainingContent >= actualInnerHeight
374402
? safariTopDropShadowCssPixels
375403
: actualInnerHeight - remainingContent - safariBottomCropOffsetCssPixels
376404
} else if (!isFirstImage) {
405+
// Non-last, non-first images: crop 1px from top
377406
imageYPosition = safariTopDropShadowCssPixels
378407
} else {
408+
// First image: no crop
379409
imageYPosition = 0
380410
}
381411
} else {
@@ -384,6 +414,8 @@ export async function getDesktopFullPageScreenshotsData(browserInstance:Webdrive
384414
: 0
385415
}
386416

417+
// Calculate based on where the previous image ends
418+
// Previous image's canvasYPosition + previous image's height
387419
let canvasYPosition: number
388420
if (isSafariDesktop && !isFirstImage) {
389421
const previousImage = viewportScreenshots[viewportScreenshots.length - 1]

0 commit comments

Comments
 (0)