是否有一种方法可以使用: getDisplayMedia获取HTML元素及其所有子元素的屏幕截图,并将其保存/下载为单个png映像?
我不想流内容,我只是有一个非常复杂的SVG /画布/ HTML设置,需要拍摄整个根DIV的截图。在Chrome中尝试了许多图像快照技术,但是它们中没有一个能捕获所有的东西,所以我想也许MediaRecorder > png会有用吗?
谢谢你
发布于 2022-11-28 07:48:56
如果您的元素完全符合视窗,我会推荐最近的区域捕获API登陆Chrome。
有关更多详细信息,请参阅https://developer.chrome.com/docs/web-platform/region-capture/。
const mainContentArea = document.querySelector("#mainContentArea");
const cropTarget = await CropTarget.fromElement(mainContentArea);
// Prompt user to share the tab's content.
const stream = await navigator.mediaDevices.getDisplayMedia({ preferCurrentTab: true });
const [track] = stream.getVideoTracks();
// Start cropping the self-capture video track using the CropTarget <-- Magic!
await track.cropTo(cropTarget);
const canvas = drawToCanvas(stream);
canvas.toBlob((blob) => {
// See https://web.dev/patterns/files/save-a-file/ to save this blob locally.
});
/* Utils */
function drawToCanvas(stream) {
const canvas = document.createElement("canvas");
const video = document.createElement("video");
video.srcObject = stream;
// Play it.
await video.play();
// Draw one video frame to canvas.
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext("2d").drawImage(video, 0, 0);
return canvas;
}
将来,您可能可以使用元素捕获Web。见当前建议:https://github.com/WICG/proposals/issues/73
https://stackoverflow.com/questions/74275776
复制相似问题