grafana/k6 · error
trimming clip to size
Error message
trimming clip to size: %w
What it means
Raised in screenshotPage during a full-page screenshot with a user-supplied clip: the clip rectangle (opts.Clip) must be clamped to the document rect (the full page size) before capture. trimClipToSize returns an error when the clipping math is invalid — i.e. the requested clip lies entirely outside the document bounds or produces a non-positive width/height after clamping. The input at fault is the clip {x, y, width, height} passed in the screenshot options.
Solutions
- Check that clip x/y/width/height are within the actual page dimensions
- Use non-negative, non-zero width and height values for the clip
- Measure the page or element bounding box first and derive the clip from it
- Omit clip and use fullPage alone if clipping is not strictly required
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at internal/js/modules/k6/browser/common/screenshotter.go:400 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18).
Data as JSON: /api/errors/4a0412b1191b7c72.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/screenshotter.go:417
if restoreErr := s.restoreViewport(p, originalSize); restoreErr != nil {
err = errors.Join(err, fmt.Errorf("restoring viewport to %s: %w",
viewportSize, restoreErr))
}
}()
if err := p.setViewportSize(s.ctx, fullPageSize); err != nil {
return nil, fmt.Errorf("setting viewport size to %s: %w",
fullPageSize, err)
}
}
if opts.Clip != nil {
documentRect, err = s.trimClipToSize(&Rect{
X: opts.Clip.X,
Y: opts.Clip.Y,
Width: opts.Clip.Width,
Height: opts.Clip.Height,
}, &Size{Width: documentRect.Width, Height: documentRect.Height})
if err != nil {
return nil, fmt.Errorf("trimming clip to size: %w", err)
}
}
return s.screenshot(p.session, documentRect, nil, format, opts.OmitBackground, opts.Quality, opts.Path)
}
viewportRect := &Rect{
X: 0,
Y: 0,
Width: viewportSize.Width,
Height: viewportSize.Height,
}
if opts.Clip != nil {
viewportRect, err = s.trimClipToSize(&Rect{
X: opts.Clip.X,
Y: opts.Clip.Y,
Width: opts.Clip.Width,
Height: opts.Clip.Height,View on GitHub (pinned to 3fcf5388d7)