Skip to content

Commit 4ce1264

Browse files
committed
Add option to set window size in render command
1 parent d3979f0 commit 4ce1264

4 files changed

Lines changed: 19 additions & 16 deletions

File tree

bin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ program
4747
.option('-s, --selector <selector>', 'CSS selector to target the rendered node, defaults to `css-doodle`')
4848
.option('-d, --delay <delay>', 'Delay after the image is rendered, e.g, `2s`')
4949
.option('-t, --time <time>', 'Record screen for a specific time, e.g, `10s')
50+
.option('-ws, --window-size <size>', 'The size of the rendered window, e.g, `800x600`, defaults to `1600x900`')
5051
.action(handleRender);
5152

5253
program

lib/handler.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export async function handleRender(source, options) {
2020
options.scale = Number(options.scale) || (options.time ? 1 : 2);
2121
options.selector ??= 'css-doodle';
2222

23+
options.windowWidth = 1600;
24+
options.windowHeight = 900;
25+
if (options.windowSize) {
26+
const [w, h = w] = options.windowSize.split(/[,x]/);
27+
options.windowWidth = Number(w);
28+
options.windowHeight = Number(h);
29+
}
30+
2331
let title = options.time ? 'record' : 'image';
2432
if (source) {
2533
const basename = path.basename(source);

lib/render/screencast.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { setTimeout } from 'node:timers/promises';
22

3-
const defaultWidth = 1600;
4-
const defaultHeight = 900;
5-
63
export async function screencast(page, options) {
7-
const { scale, output, selector } = options;
4+
const { scale, output, selector, windowWidth, windowHeight } = options;
85

96
await page.setViewport({
10-
width: defaultWidth,
11-
height: defaultHeight,
7+
width: windowWidth,
8+
height: windowHeight,
129
});
1310

1411
const clip = await page.evaluate(selector => {
@@ -31,8 +28,8 @@ export async function screencast(page, options) {
3128
}, selector);
3229

3330
await page.setViewport({
34-
width: Math.ceil(clip.width) || defaultWidth,
35-
height: Math.ceil(clip.height) || defaultHeight,
31+
width: Math.ceil(clip.width) || windowWidth,
32+
height: Math.ceil(clip.height) || windowHeight,
3633
});
3734

3835
const recorder = await page.screencast({

lib/render/screenshot.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
const defaultWidth = 1600;
2-
const defaultHeight = 900;
3-
41
export async function screenshot(page, options = {}) {
5-
const { scale, output, selector } = options;
2+
const { scale, output, selector, windowWidth, windowHeight } = options;
63

74
await page.setViewport({
8-
width: defaultWidth,
9-
height: defaultHeight,
5+
width: windowWidth,
6+
height: windowHeight,
107
deviceScaleFactor: scale
118
});
129

@@ -30,8 +27,8 @@ export async function screenshot(page, options = {}) {
3027
}, selector);
3128

3229
await page.setViewport({
33-
width: Math.ceil(info.width) || defaultWidth,
34-
height: Math.ceil(info.height) || defaultHeight,
30+
width: Math.ceil(info.width) || windowWidth,
31+
height: Math.ceil(info.height) || windowHeight,
3532
deviceScaleFactor: scale
3633
});
3734

0 commit comments

Comments
 (0)