{"record":{"id":"17a1a89ef4ea3952","repo":"grafana/k6","slug":"clip-area-is-either-empty-or-outside-the-viewport","errorCode":null,"errorMessage":"clip area is either empty or outside the viewport","messagePattern":"clip area is either empty or outside the viewport","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":460,"sourceCode":"}\n\nfunc (s *screenshotter) trimClipToSize(clip *Rect, size *Size) (*Rect, error) {\n\tp1 := Position{\n\t\tX: math.Max(0, math.Min(clip.X, size.Width)),\n\t\tY: math.Max(0, math.Min(clip.Y, size.Height)),\n\t}\n\tp2 := Position{\n\t\tX: math.Max(0, math.Min(clip.X+clip.Width, size.Width)),\n\t\tY: math.Max(0, math.Min(clip.Y+clip.Height, size.Height)),\n\t}\n\tresult := Rect{\n\t\tX:      p1.X,\n\t\tY:      p1.Y,\n\t\tWidth:  p2.X - p1.X,\n\t\tHeight: p2.Y - p1.Y,\n\t}\n\tif result.Width == 0 || result.Height == 0 {\n\t\treturn nil, errors.New(\"clip area is either empty or outside the viewport\")\n\t}\n\treturn &result, nil\n}\n","sourceCodeStart":442,"sourceCodeEnd":464,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/screenshotter.go#L442-L464","documentation":"screenshotter.trimClipToSize (screenshotter.go:437-456) clamps the requested clip rectangle to the current viewport and intersects the two. If the resulting width or height is 0 — the clip is zero-sized or lies entirely beyond the viewport bounds — the screenshot is refused with this error before any capture happens.","triggerScenarios":"page.screenshot({clip: {x: 2000, y: 0, width: 100, height: 100}}) with the default 1280x720 viewport; clip with width: 0 or height: 0; negative x/y that pushes the whole rect out of bounds after clamping.","commonSituations":"Hardcoded clip coordinates captured on a larger monitor; viewport resized via browser launch options after the coordinates were chosen; responsive pages rendering narrower than expected in headless CI; clips derived from stale element bounding boxes.","solutions":["Keep the clip inside the viewport: 0 <= x, x+width <= viewport width (same for y/height)","Enlarge the viewport in browser.launch({viewport: {...}}) so it covers the clip region","Use fullPage: true instead of clip when the target content may exceed the viewport","Compute the clip at runtime from element.boundingBox() and clamp it to page.viewportSize()"],"exampleFix":"// before\nawait page.screenshot({ clip: { x: 1500, y: 10, width: 200, height: 200 } }); // viewport 1280x720\n\n// after\nawait page.screenshot({ clip: { x: 1000, y: 10, width: 200, height: 200 } });","handlingStrategy":"validation","validationCode":"// Clamp the clip to the live viewport before shooting\nconst vp = page.viewportSize();\nconst box = await el.boundingBox();\nconst clip = {\n  x: Math.max(0, Math.min(box.x, vp.width - 1)),\n  y: Math.max(0, Math.min(box.y, vp.height - 1)),\n  width: Math.max(1, Math.min(box.width, vp.width)),\n  height: Math.max(1, Math.min(box.height, vp.height)),\n};\nawait page.screenshot({ clip });","typeGuard":null,"tryCatchPattern":"try {\n  await page.screenshot({ clip });\n} catch (e) {\n  if (/clip area/.test(e.message)) {\n    await page.screenshot({ fullPage: true }); // fallback capture\n  } else throw e;\n}","preventionTips":["Derive clip coordinates from boundingBox() at runtime, never hardcode them","Set the viewport explicitly in launch options to match your coordinate assumptions","Sanity-check x+width and y+height against viewportSize() before screenshotting","Use fullPage when content size is unpredictable"],"tags":["browser","screenshot","clip","viewport"],"backgroundTag":"screenshot-clip-invalid","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}