[英]Canvas drawing not loading in puppeteer
I need to generate snapshots for seo. 我需要为seo生成快照。 I am using puppeteer(headless chrome) for this purpose.
为此,我正在使用木偶(无头铬)。 On main page i have a canvas, on which i start to draw once the component has mounted (my main site is in react).
在主页面上我有一个画布,一旦组件安装就开始画画(我的主站点在反应中)。
Issue is that when i get the html from puppeteer, the drawing on the canvas is not there. 问题是,当我从puppeteer获取html时,画布上的绘图不存在。
In puppeteer code i wait till the content is not loaded. 在puppeteer代码中我等到内容未加载。
html = await page.content()
How can i make puppeteer wait till the point canvas is not painted. 我怎样才能使木偶操纵者等到画布没有涂上。
page.content
will only return the HTML representation of the DOM. page.content
只返回DOM的HTML表示形式。 To get the actual image of a canvas inside the DOM, you can use the function toDataURL
. 要获取DOM内部画布的实际图像,可以使用函数
toDataURL
。 This will return the image that is shown in a base64-encoded string. 这将返回以base64编码的字符串显示的图像。
Code sample 代码示例
const dataUrl = await page.evaluate(() => {
const canvas = document.querySelector("#canvas-selector");
return canvas.toDataURL();
});
// dataUrl looks like this: "data:image/png;base64,iVBORw..."
const base64String = dataUrl.substr(dataUrl.indexOf(',') + 1); // get everything after the comma
const imgBuffer = Buffer.from(base64String, 'base64'); //
fs.writeFileSync('image.png', imgBuffer);
The evaluate
call will return the base64 encoded buffer of the image. evaluate
调用将返回图像的base64编码缓冲区。 You need to first remove the "data:...,"
from that and then you can put that into a buffer. 您需要先从中删除
"data:...,"
,然后将其放入缓冲区。 The buffer can then be saved (or handled in any other way). 然后可以保存缓冲区(或以任何其他方式处理)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.