grafana/k6 · error

clip area is either empty or outside the viewport

Error message

clip area is either empty or outside the viewport

What it means

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.

Solutions

  1. Keep the clip inside the viewport: 0 <= x, x+width <= viewport width (same for y/height)
  2. Enlarge the viewport in browser.launch({viewport: {...}}) so it covers the clip region
  3. Use fullPage: true instead of clip when the target content may exceed the viewport
  4. Compute the clip at runtime from element.boundingBox() and clamp it to page.viewportSize()

Example fix

// before
await page.screenshot({ clip: { x: 1500, y: 10, width: 200, height: 200 } }); // viewport 1280x720

// after
await page.screenshot({ clip: { x: 1000, y: 10, width: 200, height: 200 } });
Defensive patterns

Strategy: validation

Validate before calling

// Clamp the clip to the live viewport before shooting
const vp = page.viewportSize();
const box = await el.boundingBox();
const clip = {
  x: Math.max(0, Math.min(box.x, vp.width - 1)),
  y: Math.max(0, Math.min(box.y, vp.height - 1)),
  width: Math.max(1, Math.min(box.width, vp.width)),
  height: Math.max(1, Math.min(box.height, vp.height)),
};
await page.screenshot({ clip });

Try / catch

try {
  await page.screenshot({ clip });
} catch (e) {
  if (/clip area/.test(e.message)) {
    await page.screenshot({ fullPage: true }); // fallback capture
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/17a1a89ef4ea3952. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/screenshotter.go:460

}

func (s *screenshotter) trimClipToSize(clip *Rect, size *Size) (*Rect, error) {
	p1 := Position{
		X: math.Max(0, math.Min(clip.X, size.Width)),
		Y: math.Max(0, math.Min(clip.Y, size.Height)),
	}
	p2 := Position{
		X: math.Max(0, math.Min(clip.X+clip.Width, size.Width)),
		Y: math.Max(0, math.Min(clip.Y+clip.Height, size.Height)),
	}
	result := Rect{
		X:      p1.X,
		Y:      p1.Y,
		Width:  p2.X - p1.X,
		Height: p2.Y - p1.Y,
	}
	if result.Width == 0 || result.Height == 0 {
		return nil, errors.New("clip area is either empty or outside the viewport")
	}
	return &result, nil
}

View on GitHub (pinned to 3fcf5388d7)