Skip to content

Commit af11c83

Browse files
committed
fix: optimise fix for iOS
- update images
1 parent 93af917 commit af11c83

12 files changed

Lines changed: 35 additions & 11 deletions

packages/image-comparison-core/src/methods/__snapshots__/images.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ exports[`makeFullPageBase64Image > should handle screenshots with cropping posit
21372137
[
21382138
{
21392139
"h": 500,
2140-
"w": 500,
2140+
"w": 900,
21412141
"x": 100,
21422142
"y": 50,
21432143
},
@@ -2159,7 +2159,7 @@ exports[`makeFullPageBase64Image > should handle screenshots with cropping posit
21592159
[
21602160
{
21612161
"h": 500,
2162-
"w": 500,
2162+
"w": 900,
21632163
"x": 100,
21642164
"y": 50,
21652165
},

packages/image-comparison-core/src/methods/__snapshots__/screenshots.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ exports[`screenshots > getMobileFullPageNativeWebScreenshotsData > should handle
176176
},
177177
],
178178
"fullPageHeight": 637,
179-
"fullPageWidth": 1334,
179+
"fullPageWidth": 667,
180180
}
181181
`;
182182

@@ -203,7 +203,7 @@ exports[`screenshots > getMobileFullPageNativeWebScreenshotsData > should hide e
203203
},
204204
],
205205
"fullPageHeight": 2623,
206-
"fullPageWidth": 750,
206+
"fullPageWidth": 375,
207207
}
208208
`;
209209

@@ -230,7 +230,7 @@ exports[`screenshots > getMobileFullPageNativeWebScreenshotsData > should take m
230230
},
231231
],
232232
"fullPageHeight": 1289,
233-
"fullPageWidth": 375,
233+
"fullPageWidth": 187.5,
234234
}
235235
`;
236236

@@ -248,7 +248,7 @@ exports[`screenshots > getMobileFullPageNativeWebScreenshotsData > should take s
248248
},
249249
],
250250
"fullPageHeight": 637,
251-
"fullPageWidth": 750,
251+
"fullPageWidth": 375,
252252
}
253253
`;
254254

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ export async function makeFullPageBase64Image(
462462
const { height: screenshotHeight, width: screenshotWidth } = getBase64ScreenshotSize(currentScreenshot, devicePixelRatio)
463463
const isRotated = isLandscape && screenshotHeight > screenshotWidth
464464
const newBase64Image = isRotated ? await rotateBase64Image({ base64Image: currentScreenshot, degrees: 90 }) : currentScreenshot
465-
const { canvasYPosition, imageHeight, imageWidth, imageXPosition, imageYPosition } = screenshotsData.data[i]
465+
const { canvasYPosition, imageHeight, imageXPosition, imageYPosition } = screenshotsData.data[i]
466466
const image = await Jimp.read(Buffer.from(newBase64Image, 'base64'))
467467

468468
// Clamp crop dimensions to fit within the actual image bounds
@@ -471,7 +471,10 @@ export async function makeFullPageBase64Image(
471471
const actualImageHeight = image.bitmap.height
472472
const clampedCropX = Math.max(0, Math.min(imageXPosition, actualImageWidth - 1))
473473
const clampedCropY = Math.max(0, Math.min(imageYPosition, actualImageHeight - 1))
474-
const clampedCropWidth = Math.min(imageWidth, actualImageWidth - clampedCropX)
474+
// Ensure the cropped width matches the canvas width to avoid 1px gaps due to rounding
475+
// The canvas width is the target, but we must not exceed the available image bounds
476+
const maxAvailableWidth = actualImageWidth - clampedCropX
477+
const clampedCropWidth = Math.min(canvasWidth, maxAvailableWidth)
475478
const clampedCropHeight = Math.min(imageHeight, actualImageHeight - clampedCropY)
476479

477480
canvas.composite(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ describe('screenshots', () => {
9191

9292
vi.mocked(utilsModule.waitFor).mockResolvedValue(undefined)
9393
vi.mocked(utilsModule.calculateDprData).mockImplementation((data) => data)
94+
vi.mocked(utilsModule.getBase64ScreenshotSize).mockImplementation((_screenshot, dpr = 1) => {
95+
const baseWidth = 750
96+
const baseHeight = 1334
97+
return {
98+
width: Math.round(baseWidth / dpr),
99+
height: Math.round(baseHeight / dpr)
100+
}
101+
})
94102
})
95103

96104
afterEach(() => {

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export async function getMobileFullPageNativeWebScreenshotsData(browserInstance:
4848
const amountOfScrollsArray = []
4949
let scrollHeight: number | undefined
5050
let isRotated = false
51+
let actualFullPageWidth: number | undefined
5152

5253
for (let i = 0; i <= amountOfScrollsArray.length; i++) {
5354
// Determine and start scrolling
@@ -151,7 +152,7 @@ export async function getMobileFullPageNativeWebScreenshotsData(browserInstance:
151152
: scrollY
152153

153154
// Store all the screenshot data in the screenshot object
154-
viewportScreenshots.push({
155+
const screenshotData = {
155156
...calculateDprData(
156157
{
157158
canvasWidth: isRotated ? effectiveViewportHeight : viewportWidth,
@@ -164,7 +165,19 @@ export async function getMobileFullPageNativeWebScreenshotsData(browserInstance:
164165
devicePixelRatio,
165166
),
166167
screenshot,
167-
})
168+
}
169+
viewportScreenshots.push(screenshotData)
170+
171+
// Calculate the actual cropped width from the first screenshot to handle rounding differences
172+
if (i === 0 && !actualFullPageWidth) {
173+
const { height: screenshotHeightDevicePixels, width: screenshotWidthDevicePixels } = getBase64ScreenshotSize(screenshot)
174+
const screenshotIsRotated = Boolean(isLandscape && screenshotHeightDevicePixels > screenshotWidthDevicePixels)
175+
const actualScreenshotWidthDevicePixels = screenshotIsRotated ? screenshotHeightDevicePixels : screenshotWidthDevicePixels
176+
const maxAvailableWidthDevicePixels = actualScreenshotWidthDevicePixels - screenshotData.imageXPosition
177+
const actualCroppedWidthDevicePixels = Math.min(screenshotData.imageWidth, maxAvailableWidthDevicePixels)
178+
179+
actualFullPageWidth = actualCroppedWidthDevicePixels / devicePixelRatio
180+
}
168181

169182
// Show scrollbars again
170183
await browserInstance.execute(hideScrollBars, false)
@@ -184,7 +197,7 @@ export async function getMobileFullPageNativeWebScreenshotsData(browserInstance:
184197
}
185198

186199
const fullPageHeight = scrollHeight - addressBarShadowPadding - toolBarShadowPadding
187-
const fullPageWidth = isRotated ? effectiveViewportHeight : viewportWidth
200+
const fullPageWidth = actualFullPageWidth ?? (isRotated ? effectiveViewportHeight : viewportWidth)
188201

189202
return {
190203
...calculateDprData(
934 Bytes
Loading
-53 Bytes
Loading
-6.82 KB
Loading
-242 Bytes
Loading
-50 Bytes
Loading

0 commit comments

Comments
 (0)