ScreenCapturer
ScreenCapturer
media_projection.ScreenCapturer
Live screen capture session created by requestScreenCapture. Yields successive Image frames from the virtual display.
Inheritance
EventEmitter↳
ScreenCapturer
ScreenCapturer extends EventEmitter, so you can use on / once / off etc.
Table of contents
Methods
Events
Events
image_available
▸ on(event, listener): this
Fires whenever a new frame is available (same moment you could read via latestImageOrNull).
Example
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
capturer.on("image_available", () => {
console.log("New frame available");
const img = capturer.latestImageOrNull();
if (img) {
console.log("Size:", img.width, "x", img.height);
}
});
$autojs.keepRunning();
}
main();Parameters
| Name | Type | Description |
|---|---|---|
event | "image_available" | Fixed event name. |
listener | () => void | Called when a new frame is ready. |
Returns
this
The capturer instance (chainable).
Methods
latestImage
▸ latestImage(): Image
Returns the most recent frame. Throws ImageUnavailableError if none exists yet.
Example
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
await capturer.awaitForImageAvailable();
const img = capturer.latestImage();
console.log("Size:", img.width, "x", img.height);
capturer.stop();
}
main();Returns
Latest frame.
Throws
ImageUnavailableError when no frame is available.
latestImageOrNull
▸ latestImageOrNull(): Image | null
Same as latestImage, but returns null instead of throwing when no frame exists.
Example
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
const img = capturer.latestImageOrNull();
if (img) {
console.log("Size:", img.width, "x", img.height);
} else {
console.log("No frame yet");
}
capturer.stop();
}
main();Returns
Image | null
Latest frame, or null.
nextImage
▸ nextImage(): Promise<Image>
Waits until the next frame is produced and resolves with it.
Example
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
const img = await capturer.nextImage();
console.log("First frame:", img.width, "x", img.height);
const img2 = await capturer.nextImage();
console.log("Second frame:", img2.width, "x", img2.height);
capturer.stop();
}
main();Returns
Promise<Image>
Promise that resolves with the next frame.
awaitForImageAvailable
▸ awaitForImageAvailable(): Promise<void>
Resolves when at least one frame exists. Right after permission is granted, the pipeline may need a moment before the first frame arrives; this helper waits for that. If a frame is already available, it resolves immediately.
Example
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
await capturer.awaitForImageAvailable();
console.log("First frame ready");
const img = capturer.latestImage();
console.log("Size:", img.width, "x", img.height);
capturer.stop();
}
main();Returns
Promise<void>
stop
▸ stop(): void
Tear down capture and stop emitting frames.
Example
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
for (let i = 0; i < 5; i++) {
const img = await capturer.nextImage();
console.log(`Frame ${i + 1}`);
}
capturer.stop();
console.log("Capture stopped");
}
main();Returns
void
