Skip to content

Commit ea211d0

Browse files
committed
feat: add overlay support
1 parent 7d55003 commit ea211d0

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,21 +375,22 @@ Scroll to the DOM element matching the given [CSS selector](https://developer.mo
375375

376376
![](https://i.imgur.com/vR0M3qt.png)
377377

378-
type: `boolean` | `object`
378+
type: `object`
379379

380-
After the screenshot has been taken, this option allows you to place the screenshot with an overlay.
380+
After the screenshot has been taken, this option allows you to place the screenshot into a fancy overlay
381381

382382
You can configure the overlay specifying:
383383

384-
- **path**: The image path to use to put on top of the screenshot.
385-
- **color**: The hexadecimal background color to use (default is `'transparent'`).
384+
- **browser**: It sets the browser image overlay to use. Supported values: `safari-light`,` safari-dark`.
385+
- **background**: It sets the background color to use. You can pass a hexadecimal/rgb/rgba or a CSS gradient.
386386

387387
```js
388388
;(async () => {
389389
const buffer = await browserless.screenshot(url.toString(), {
390390
hideElements: ['.crisp-client', '#cookies-policy'],
391391
overlay: {
392-
color: '#F76698'
392+
browser: 'safari-dark',
393+
background: 'linear-gradient(45deg, rgba(255,18,223,1) 0%, rgba(69,59,128,1) 66%, rgba(69,59,128,1) 100%)'
393394
}
394395
})
395396
})()

packages/examples/src/screenshot.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ require('./main')(async url => {
88
const buffer = await browserless.screenshot(url.toString(), {
99
hideElements: ['.crisp-client', '#cookies-policy'],
1010
overlay: {
11-
browser: 'dark',
12-
color: '#6791B6'
11+
browser: 'safari-dark',
12+
background:
13+
'linear-gradient(45deg, rgba(255,18,223,1) 0%, rgba(69,59,128,1) 66%, rgba(69,59,128,1) 100%)' // '#f00'
1314
}
1415
})
1516

packages/screenshot/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
],
2929
"dependencies": {
3030
"@browserless/goto": "^5.5.5",
31-
"hex-rgb": "~4.1.0",
3231
"is-url-http": "~1.2.0",
33-
"sharp": "~0.22.1"
32+
"sharp": "~0.22.1",
33+
"svg-gradient": "~1.0.0"
3434
},
3535
"engines": {
3636
"node": ">= 8"

packages/screenshot/src/index.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3+
const svgGradient = require('svg-gradient')
34
const goto = require('@browserless/goto')
45
const isUrl = require('is-url-http')
5-
const hexRgb = require('hex-rgb')
66
const sharp = require('sharp')
77
const path = require('path')
88

9-
const browserOverlay = ['light', 'dark'].reduce(
9+
const browserOverlay = ['safari-light', 'safari-dark'].reduce(
1010
(acc, key) => ({
1111
...acc,
1212
[key]: path.resolve(__dirname, `browser/${key}.png`)
@@ -101,19 +101,7 @@ const doRemoveElements = elements => {
101101
}
102102
}
103103

104-
const getOverlayColors = color => {
105-
let r = 0
106-
let g = 0
107-
let b = 0
108-
109-
try {
110-
;({ red: r, green: g, blue: b } = hexRgb(color))
111-
} catch (e) {
112-
color = 'transparent'
113-
}
114-
115-
return [r, g, b]
116-
}
104+
const createBackground = css => Buffer.from(svgGradient(css, { width: '1388px', height: '955px' }))
117105

118106
const getInjectKey = (ext, value) =>
119107
isUrl(value) ? 'url' : value.endsWith(`.${ext}`) ? 'path' : 'content'
@@ -195,15 +183,22 @@ module.exports = page => async (url, opts = {}) => {
195183
const screenshot = await page.screenshot({ type, ...args })
196184
if (!overlay) return screenshot
197185

198-
const { browser: overlayBrowser = 'light', color: overlayColor = 'transparent' } = overlay
186+
let { browserTheme, background = 'transparent' } = overlay
199187

200-
let image = await sharp(browserOverlay[overlayBrowser]).composite([{ input: screenshot }])
188+
if (!background.includes('gradient')) {
189+
background = `linear-gradient(45deg, ${background} 0%, ${background} 100%)`
190+
}
191+
192+
let image = await sharp(Buffer.from(createBackground(background)))
193+
let inputs = [{ input: screenshot }]
201194

202-
if (overlayColor !== 'transparent') {
203-
const [r, g, b] = getOverlayColors(overlayColor)
204-
image = await image.flatten({ background: { r, g, b, alpha: 1 } })
195+
if (browserTheme) {
196+
const input = browserOverlay[browserTheme]
197+
if (input) inputs = [{ input }].concat(inputs)
205198
}
206199

200+
image = await image.composite(inputs)
201+
207202
const buffer = await image.toBuffer()
208203
return buffer
209204
}

0 commit comments

Comments
 (0)