A native screenshot toolkit for Node.js / Electron, built in Rust with napi-rs.
- Full-screen overlay with multi-monitor support and per-display GL context isolation
- Cross-screen drag selection, with logical-coordinate geometry throughout
- Window detection with hover highlighting (R-tree spatial index) and double-click to capture
- In-place editor: rectangle / ellipse / arrow / brush / mosaic / text, with undo / redo
- Mosaic redaction, save-to-disk and one-key clipboard copy
- Synchronous blocking
start()API designed for Electron's main process
| Platform | Status |
|---|---|
| macOS | Fully supported (CoreGraphics capture, NSStatusWindowLevel overlay) |
| Windows | Stub (WindowsBackend not yet implemented) |
| Linux/Other | UnsupportedBackend — calls return errors |
npm install electron-rust-screenshotThe package ships native binaries via
napi-rs. To build from source you'll need a stable Rust toolchain plus Node.js 20+.
git clone <this-repo>
cd electron-rust-screenshot
npm install
npm run build # produces dist/*.node and dist/index.jsconst { start } = require('electron-rust-screenshot');
const result = start({
savePath: '/tmp/shot.png',
format: 'png', // 'png' | 'jpg'
quality: 90, // 1-100
defaultColor: '#ff0000',
defaultSize: 3,
});
console.log(result);
// → { type: 'saved', path: '/tmp/shot.png', copied: false }
// → { type: 'cancelled' }
// → { type: 'error', code: '...', message: '...' }Important:
start()blocks the calling thread until the overlay closes, because macOS requires thewinit::EventLoopto live on the main thread. In Electron, call it from the main process.
node scripts/demo.js interactive # png, red pen
node scripts/demo.js jpg # jpeg, green pen
node scripts/demo.js clipboard # png, blue pen| Action | Key |
|---|---|
| Select region / draw shape | Mouse drag |
| Highlight hovered window | Mouse move |
| Capture hovered window directly | Double-click |
| Switch tool (Rect / Ellipse / Arrow / Brush / Mosaic / Text) | 1–6 |
| Undo | Cmd+Z |
| Redo | Cmd+Shift+Z |
| Copy to clipboard (in editing mode) | C |
| Save | Enter |
| Cancel | Esc |
Launches the overlay synchronously. Returns a JSON string describing the final event.
| Field | Type | Default | Description |
|---|---|---|---|
savePath |
string |
/tmp |
Output path for the saved image |
format |
'png' | 'jpg' |
'png' |
Encoding format |
quality |
number (1–100) |
90 |
JPEG quality (PNG ignores) |
mosaicBlockSize |
number |
8 |
Mosaic tile size in logical pixels |
defaultColor |
string (hex) |
#ff0000 |
Default brush / shape color |
defaultSize |
number |
3 |
Default brush / stroke width |
locale |
string |
zh-CN |
UI locale |
showDebugHud |
boolean |
false |
Toggle on-screen perf HUD |
metricsIntervalMs |
number |
500 |
Interval between metrics events |
crates/
screenshot-core/ # platform-agnostic Rust library (rlib)
src/core/ # engine, capture, editor, events, types, dpi, perf, window detector
src/overlay/ # winit + egui multi-window overlay, GL helpers, image compositor, toolbar
src/platform/ # macOS / Windows / unsupported backends
napi-bindings/ # napi-rs cdylib that wraps the core for Node
src/bridge/ # ScreenshotConfig, ScreenshotSession, JSON event serializer
dist/ # build output (.node + JS wrappers)
e2e/ # Jest + AppleScript E2E tests
scripts/ # demo.js (interactive) and test-smoke.js
For the deep-dive architecture (multi-monitor GL isolation, coordinate system, NSWindow setup, composite algorithm), see CLAUDE.md.
cargo test --workspace # Rust unit + integration tests
npm test # Node smoke test
cd e2e && npx jest --runInBand # AppleScript E2E suite
cargo fmt --all -- --check # CI lint gate
cargo clippy --workspace --all-targets -- -D warnings # CI lint gateE2E tests inject mouse events via cliclick, which requires Accessibility permission. In headless / sandboxed environments, set SCREENSHOT_TEST_MOCK_DRAG=x1,y1,x2,y2 (e.g. 300,300,500,500) and the overlay will simulate the drag internally about 5 seconds after launch.
ISC
{ "type": "saved", "path": "/tmp/shot.png", "copied": false } { "type": "cancelled" } { "type": "error", "code": "<error-code>", "message": "<reason>" }