Skip to content

Instantly share code, notes, and snippets.

@NaniteFactory
Created May 17, 2020 07:13
Show Gist options
  • Save NaniteFactory/b181532bdde21a7401f12a0cfcffb421 to your computer and use it in GitHub Desktop.
Save NaniteFactory/b181532bdde21a7401f12a0cfcffb421 to your computer and use it in GitHub Desktop.
chromedp full screen shot
func FullScreenshot(quality int64, result *[]byte) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) error {
// get layout metrics
_, _, contentSize, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
width, height := int64(math.Ceil(contentSize.Width)), int64(math.Ceil(contentSize.Height))
// force viewport emulation
err = emulation.SetDeviceMetricsOverride(width, height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).
Do(ctx)
if err != nil {
return err
}
// capture screenshot
*result, err = page.CaptureScreenshot().
WithQuality(quality).
WithClip(&page.Viewport{
X: contentSize.X,
Y: contentSize.Y,
Width: contentSize.Width,
Height: contentSize.Height,
Scale: 1,
}).Do(ctx)
if err != nil {
return err
}
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment