node-screenshots
is a native node.js screenshot library that supports Mac, Windows, and Linux systems without any dependencies.
English | 简体中文
Operating System | node14 | node16 | node18 |
---|---|---|---|
Windows x64 | ✓ | ✓ | ✓ |
Windows x32 | ✓ | ✓ | ✓ |
Windows arm64 | ✓ | ✓ | ✓ |
macOS x64 | ✓ | ✓ | ✓ |
macOS arm64 | ✓ | ✓ | ✓ |
Linux x64 gnu | ✓ | ✓ | ✓ |
Linux x64 musl | ✓ | ✓ | ✓ |
const fs = require("fs");
const { Screenshots } = require("node-screenshots");
let capturer = Screenshots.fromPoint(100, 100);
console.log(capturer, capturer.id);
// Synchronous capture
let image = capturer.captureSync();
fs.writeFileSync("./a.png", image);
// Asynchronous capture
capturer.capture().then((data) => {
console.log(data);
fs.writeFileSync(`${capturer.id}.png`, data);
});
// Get all screen captures
let all = Screenshots.all() ?? [];
all.forEach((capturer) => {
console.log({
id: capturer.id,
x: capturer.x,
y: capturer.y,
width: capturer.width,
height: capturer.height,
rotation: capturer.rotation,
scaleFactor: capturer.scaleFactor,
isPrimary: capturer.isPrimary,
});
capturer.captureSync(true);
});
Screenshots.fromPoint(x, y)
: Get a screenshot from the specified coordinatesScreenshots.all()
: Get all screenshotsscreenshots.capture(copyOutputData)
: Asynchronously capture full screenscreenshots.captureSync(copyOutputData)
: Synchronously capture full screenscreenshots.captureArea(x, y, width, height, copyOutputData)
: Asynchronously capture the specified areascreenshots.captureAreaSync(x, y, width, height, copyOutputData)
: Synchronously capture the specified area
copyOutputData
: pass related parameters in electron, otherwise electron will crash, nodejs does not pass or passes false, performance will be better, for more information refer to napi-rs/napi-rs#1346
On Linux, you need to install libxcb
, libxrandr
, and dbus
.
Debian / Ubuntu:
apt-get install libxcb1 libxrandr2 libdbus-1-3
Alpine:
apk add libxcb libxrandr dbus