-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasterizer.js
executable file
·110 lines (102 loc) · 2.5 KB
/
rasterizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env node
import { default as CRI } from 'chrome-remote-interface'
import fs from 'node:fs'
import yargs from 'yargs/yargs'
import path from 'node:path'
const argv = (
await yargs(process.argv.slice(2))
.option('iters', {
alias: 'i',
default: 20,
description: 'Number of iterations',
type: 'number',
})
.option('url', {
alias: 'u',
description: 'URL to load [required]',
type: 'string',
required: true,
})
.option('prefix', {
alias: 'p',
default: 'shot',
description: 'Image filename prefix',
type: 'string',
})
.option('width', {
alias: 'w',
default: 200,
description: 'Width to render',
type: 'number',
})
.option('height', {
alias: 'h',
default: 200,
description: 'Height to render',
type: 'number',
})
.option('drop', {
alias: 'd',
default: 0,
description: 'Frames to drop between captures',
type: 'number',
})
.option('verbose', {
alias: 'v',
description: 'Print additional information',
})
.help()
.showHelpOnFail(true)
.argv
)
if(!argv.url) {
throw new Error('URL is required.')
}
if(!/^\w+:\/\//.test(argv.url)) {
// @ts-ignore
argv.url = `file://${path.join(process.cwd(), argv.url)}`
}
if(argv.verbose) {
console.debug(`Args: ${JSON.stringify(argv, null, 2)}`)
}
CRI({}, async (client) => {
const { Page } = client
const {
url, iters, width, height, prefix, drop,
} = argv
const color = { r: 255, g: 255, b: 255, a: 0 }
try {
await Page.enable()
await client.Emulation.setDeviceMetricsOverride({
width, height,
// fitWindow: true,
deviceScaleFactor: 1,
mobile: false
})
if(argv.verbose) console.info(`Rendering: ${url}`)
await Page.navigate({ url })
await Page.loadEventFired()
await client.Emulation.setDefaultBackgroundColorOverride({
color
})
await Page.startScreencast({ format: 'png', everyNthFrame: 1 });
let counter = 1
while(counter <= iters) {
const { data, sessionId } = (
await Page.screencastFrame()
)
const out = `${prefix}.${(counter++).toString().padStart(4, '0')}.png`
fs.writeFileSync(
out, Buffer.from(data, 'base64')
)
if(argv.verbose) console.info(`Creating: ${out}`)
await Page.screencastFrameAck({ sessionId })
for(let i = 1; i <= drop; i++) {
const { sessionId } = await Page.screencastFrame()
await Page.screencastFrameAck({ sessionId })
}
}
} finally {
client.close()
}
})