Skip to content

Feat: screenshot apis#2670

Merged
abose merged 5 commits intomainfrom
z
Feb 13, 2026
Merged

Feat: screenshot apis#2670
abose merged 5 commits intomainfrom
z

Conversation

@abose
Copy link
Member

@abose abose commented Feb 13, 2026

based on native platform api: phcode-dev/phoenix-desktop#685

📸 Screenshot Utilities

The Screenshot Utilities provide flexible APIs to capture screenshots of the current page or a specific region and return them in different formats for performance and convenience.

All methods accept the same capture target:

  • Rect object: { x, y, width, height }
  • DOM element (its bounding box is captured)
  • Selector string (must resolve to exactly one element)
  • Omitted to capture the full page

screenShotBinary(rectOrNodeOrSelector)

Captures a screenshot and returns raw PNG bytes.

Parameters

Name Type Description
rectOrNodeOrSelector Object | HTMLElement | string (optional) Area to capture (rect, DOM element, selector, or full page)

Returns

Promise<Uint8Array> — PNG image bytes

Examples

Capture a specific rectangle

const bytes = await screenShotBinary({
  x: 100,
  y: 100,
  width: 400,
  height: 300
});

Capture a DOM element

const element = document.getElementById("preview");
const bytes = await screenShotBinary(element);

screenShotToBlob(rectOrNodeOrSelector)

Captures a screenshot and returns a PNG Blob.

Parameters

Name Type Description
rectOrNodeOrSelector Object | HTMLElement | string (optional) Area to capture

Returns

Promise<Blob> — PNG Blob (image/png)

Examples

Display in an image element

const blob = await screenShotToBlob("#preview");

const url = URL.createObjectURL(blob);
document.getElementById("imgOutput").src = url;

Draw to a canvas

const blob = await screenShotToBlob();
const bitmap = await createImageBitmap(blob);
ctx.drawImage(bitmap, 0, 0);

screenShotToPNGFile(filePathToSave, rectOrNodeOrSelector)

Captures a screenshot and saves it directly as a PNG file.

Parameters

Name Type Description
filePathToSave string VFS path to write the PNG file to
rectOrNodeOrSelector Object | HTMLElement | string (optional) Area to capture

Returns

Promise<void>

Throws

  • Error if filePathToSave is not a non-empty string

Examples

Save the full page

await screenShotToPNGFile("/project/output/screenshot.png"); // any VFS path. use /tauri if you neeed native paths

Save a specific element

await screenShotToPNGFile(
  "/project/output/preview.png",
  "#preview"
);

⚡ Performance Notes

Use Case Recommended API
IPC / processing screenShotBinary
UI rendering screenShotToBlob
Exporting files screenShotToPNGFile
  • No base64 encoding is used
  • Blob conversion is zero-copy
  • Optimized for repeated captures (live preview workflows)

Add _resolveRect() helper to convert DOM elements and jQuery selector
strings to capture rects, with zoom factor compensation. Update
_capturePageBinary bounds validation to account for webview scale factor.
Add tests for DOM element, selector, and error case inputs.
Use Math.ceil on zoom-adjusted dimensions to prevent false rejections
when innerWidth/Height * zoomFactor produces fractional values.
@sonarqubecloud
Copy link

@abose abose merged commit fc2f55e into main Feb 13, 2026
20 of 21 checks passed
@abose abose deleted the z branch February 13, 2026 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant