node-screenshots

A native Node.js screenshots library for Mac, Windows, Linux

README

📸 node-screenshots


node-screenshots is a native node.js screenshot library based on XCap, It supports Mac, Windows, and Linux systems without any dependencies.node-screenshots supports screenshot and video recording (to be implemented).

Support Matrix


Operating System


Operatingnode16node18node20
----------------------------------
Windows
Windows
Windows
macOS
macOS
Linux
Linux

Example


Monitor


  1. ```ts
  2. const fs = require("fs");
  3. const { Monitor } = require("node-screenshots");

  4. let monitor = Monitor.fromPoint(100, 100);

  5. console.log(monitor, monitor.id);

  6. let image = monitor.captureImageSync();
  7. fs.writeFileSync(`${monitor.id}-sync.png`, image.toPngSync());

  8. monitor.captureImage().then((data) => {
  9.   console.log(data);
  10.   fs.writeFileSync(`${monitor.id}.jpeg`, data.toJpegSync());
  11. });

  12. let monitors = Monitor.all();

  13. monitors.forEach((capturer) => {
  14.   console.log({
  15.     id: capturer.id,
  16.     x: capturer.x,
  17.     y: capturer.y,
  18.     width: capturer.width,
  19.     height: capturer.height,
  20.     rotation: capturer.rotation,
  21.     scaleFactor: capturer.scaleFactor,
  22.     isPrimary: capturer.isPrimary,
  23.   });
  24. });
  25. ```

Window


  1. ```ts
  2. const fs = require("fs");
  3. const { Window } = require("node-screenshots");

  4. let windows = Window.all();

  5. windows.forEach((item) => {
  6.   console.log({
  7.     id: item.id,
  8.     x: item.x,
  9.     y: item.y,
  10.     width: item.width,
  11.     height: item.height,
  12.     rotation: item.rotation,
  13.     scaleFactor: item.scaleFactor,
  14.     isPrimary: item.isPrimary,
  15.   });

  16.   let image = item.captureImageSync();
  17.   fs.writeFileSync(`${item.id}-sync.bmp`, image.toBmpSync());

  18.   item.captureImage().then(async (data) => {
  19.     console.log(data);
  20.     let newImage = await data.crop(10, 10, 10, 10);
  21.     fs.writeFileSync(`${item.id}.png`, await newImage.toPng());
  22.   });
  23. });
  24. ```

API


Full typeScript type definition: index.d.ts

Monitor


- `static all(): Array`: Get all monitor
- static fromPoint(x: number, y: number): Monitor | null: Get a monitor from the specified coordinates
- captureImageSync(): Image: Synchronously capture image
- `captureImage(): Promise`: Asynchronously capture image

Window


- `static all(): Array`: Get all window
- captureImageSync(): Image: Synchronously capture image
- `captureImage(): Promise`: Asynchronously capture image

Image


- cropSync(x: number, y: number, width: number, height: number): Image: Synchronously crop image
- `crop(x: number, y: number, width: number, height: number): Promise`: Asynchronously crop image
- toPngSync(copyOutputData?: boolean | undefined | null): Buffer:Synchronously Convert to png
- `toPng(copyOutputData?: boolean | undefined | null): Promise`: Asynchronously Convert to png
- toJpegSync(copyOutputData?: boolean | undefined | null): Buffer: Synchronously Convert to jpeg
- `toJpeg(copyOutputData?: boolean | undefined | null): Promise`: Asynchronously Convert to jpeg
- toBmpSync(copyOutputData?: boolean | undefined | null): Buffer: Synchronously Convert to bmp
- `toBmp(copyOutputData?: boolean | undefined | null): Promise`: Asynchronously Convert to bmp
- toRawSync(copyOutputData?: boolean | undefined | null): Buffer: Synchronously Convert to raw(RGBA data)
- `toRaw(copyOutputData?: boolean | undefined | null): Promise`: Asynchronously Convert to raw(RGBA data)

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 https://github.com/napi-rs/napi-rs/issues/1346

Linux System Requirements


On Linux, you need to install libxcb, libxrandr, and dbus.

Debian / Ubuntu:

  1. ```sh
  2. apt-get install libxcb1 libxrandr2 libdbus-1-3
  3. ```

Alpine:

  1. ```sh
  2. apk add libxcb libxrandr dbus
  3. ```