https://github.com/nashaofu/node-screenshots
Zero-dependent. A native nodejs screenshots library for Mac、Windows、Linux.
https://github.com/nashaofu/node-screenshots
linux macos native nodejs screenshot windows zero-dependencies
Last synced: about 1 month ago
JSON representation
Zero-dependent. A native nodejs screenshots library for Mac、Windows、Linux.
- Host: GitHub
- URL: https://github.com/nashaofu/node-screenshots
- Owner: nashaofu
- License: mit
- Created: 2022-04-16T08:13:45.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2026-01-30T09:37:12.000Z (about 1 month ago)
- Last Synced: 2026-01-31T01:19:11.007Z (about 1 month ago)
- Topics: linux, macos, native, nodejs, screenshot, windows, zero-dependencies
- Language: Rust
- Homepage:
- Size: 2.57 MB
- Stars: 360
- Watchers: 2
- Forks: 23
- Open Issues: 8
-
Metadata Files:
- Readme: README-zh_CN.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- my-awesome-list - node-screenshots - dependent. A native nodejs screenshots library for Mac、Windows、Linux. | nashaofu | 369 | (Rust)
README
# 📸 node-screenshots
`node-screenshots` 是一个基于[XCap](https://github.com/nashaofu/xcap)的原生的 node.js 截图库,支持 Mac、Windows 和 Linux 系统,且无需任何依赖。 支持截图与视频录制(待实现)。
[English](README.md) | 简体中文
## 支持矩阵
### 操作系统
| 操作系统 | node16 | node18 | node20 |
| ----------------- | ------ | ------ | ------ |
| Operating System | node16 | node18 | node20 |
| ----------------- | ------ | ------ | ------ |
| Windows x64 | ✓ | ✓ | ✓ |
| Windows x32 | ✓ | ✓ | ✓ |
| Windows arm64 | ✓ | ✓ | ✓ |
| macOS x64 | ✓ | ✓ | ✓ |
| macOS arm64 | ✓ | ✓ | ✓ |
| Linux x64 gnu | ✓ | ✓ | ✓ |
| Linux x64 musl | ✓ | ✓ | ✓ |
| Linux arm64 gnu | ✓ | ✓ | ✓ |
| Linux loongarch64 | ✓ | ✓ | ✓ |
## 示例
### Monitor
```ts
const fs = require('fs')
const { Monitor } = require('node-screenshots')
let monitor = Monitor.fromPoint(100, 100)
console.log(monitor, monitor.id())
let image = monitor.captureImageSync()
fs.writeFileSync(`${monitor.id()}-sync.png`, image.toPngSync())
monitor.captureImage().then((data) => {
console.log(data)
fs.writeFileSync(`${monitor.id()}.jpeg`, data.toJpegSync())
})
const monitors = Monitor.all()
monitors.forEach((item) => {
console.log(
'Monitor:',
item.id(),
item.name(),
[item.x(), item.y(), item.width(), item.height()],
item.rotation(),
item.scaleFactor(),
item.frequency(),
item.isPrimary(),
)
})
```
### Window
```ts
const fs = require('fs')
const { Window } = require('node-screenshots')
let windows = Window.all()
windows.forEach((item) => {
console.log({
id: item.id(),
x: item.x(),
y: item.y(),
y: item.z(),
width: item.width(),
height: item.height(),
rotation: item.rotation(),
scaleFactor: item.scaleFactor(),
isPrimary: item.isPrimary(),
})
let image = item.captureImageSync()
fs.writeFileSync(`${item.id()}-sync.bmp`, image.toBmpSync())
item.captureImage().then(async (data) => {
console.log(data)
let newImage = await data.crop(10, 10, 10, 10)
fs.writeFileSync(`${item.id()}.png`, await newImage.toPng())
})
})
```
## API
完整的 TypeScript 类型定义:[index.d.ts](./index.d.ts)
### Monitor
- `static all(): Array`:获取所有监视器
- `static fromPoint(x: number, y: number): Monitor | null`:根据指定的坐标获取监视器
- `captureImageSync(): Image`:同步捕获图像
- `captureImage(): Promise`:异步捕获图像
### Window
- `static all(): Array`:获取所有窗口
- `captureImageSync(): Image`:同步捕获图像
- `captureImage(): Promise`:异步捕获图像
### Image
- `cropSync(x: number, y: number, width: number, height: number): Image`:同步裁剪图像
- `crop(x: number, y: number, width: number, height: number): Promise`:异步裁剪图像
- `toPngSync(copyOutputData?: boolean | undefined | null): Buffer`:同步转换为 PNG 格式
- `toPng(copyOutputData?: boolean | undefined | null): Promise`:异步转换为 PNG 格式
- `toJpegSync(copyOutputData?: boolean | undefined | null): Buffer`:同步转换为 JPEG 格式
- `toJpeg(copyOutputData?: boolean | undefined | null): Promise`:异步转换为 JPEG 格式
- `toBmpSync(copyOutputData?: boolean | undefined | null): Buffer`:同步转换为 BMP 格式
- `toBmp(copyOutputData?: boolean | undefined | null): Promise`:异步转换为 BMP 格式
- `toRawSync(copyOutputData?: boolean | undefined | null): Buffer`:同步转换为原始格式(RGBA 数据)
- `toRaw(copyOutputData?: boolean | undefined | null): Promise`:异步转换为原始格式(RGBA 数据)
`copyOutputData`: electron 中传递传递相关参数,否则 electron 会崩溃,nodejs 不传或者传递 false,性能会更好,详细信息参考 https://github.com/napi-rs/napi-rs/issues/1346
## Linux 系统需求
在 Linux 上,你需要安装 `libxcb`、`libxrandr` 和 `dbus`。
Debian / Ubuntu:
```sh
apt-get install libxcb1 libxrandr2 libdbus-1-3
```
Alpine:
```sh
apk add libxcb libxrandr dbus
```
## 相关仓库
- [xcap](https://github.com/nashaofu/xcap) - XCap 是一个使用 Rust 编写的跨平台的屏幕捕获库