简体   繁体   English

使用 html2Canvas 创建下载 png 时出现以下错误

[英]I am getting following error while creating download png using html2Canvas

While creating a website I was using html2canvas which is going to convert text to an image.在创建网站时,我使用的是html2canvas ,它将文本转换为图像。 Conversion is done successfully but while trying to download on button click, I got the following error: Error Screenshot转换已成功完成,但在尝试单击按钮下载时,出现以下错误:错误屏幕截图

Can anyone help me out with this please?任何人都可以帮我解决这个问题吗?

PS: I am completely new in web designing PS:我在 web 设计方面是全新的

html2canvas(document.getElementById("myname"), {
  onrendered: function (canvas) {
    var screenshot = canvas.toDataURL("image/png");
    document.getElementById("textScreenshot").setAttribute("src", screenshot);
  },
});

btnDownload.addEventListener("click", function () {
  if (window.navigator.msSaveBlob) {
    window.navigator.msSaveBlob(textScreenshot.msToBlob(), "sg.png");
  } else {
    const a = document.createElement("a");
    document.body.appendChild(a);
    a.href = screenshot.toDataURL();
    a.download = "sg.png";
    a.click();
    document.body.removeChild(a);
  }
});

Error:texttoimg.html:99 Uncaught ReferenceError: screenshot is not defined at HTMLButtonElement.错误:texttoimg.html:99 Uncaught ReferenceError:屏幕截图未在 HTMLButtonElement 中定义。

This is happening because your screenshot variable is locally scoped to your onrendered function.发生这种情况是因为您的screenshot变量在本地范围内是您的onrendered function。 You need to take it out and store in a global variable to be able to access it in the other function.您需要将其取出并存储在一个全局变量中,以便能够在另一个 function 中访问它。

let screenshot; // making it global 
window.onload = function(){
    html2canvas(document.getElementById("myname"), {
      onrendered: function (canvas) {
        screenshot = canvas.toDataURL("image/png");
        document.getElementById("textScreenshot").setAttribute("src", screenshot);
      },
    });
 }

btnDownload.addEventListener("click", function () {
  if (window.navigator.msSaveBlob) {
    window.navigator.msSaveBlob(textScreenshot.msToBlob(), "sg.png");
  } else {
    const a = document.createElement("a");
    document.body.appendChild(a);
    a.href = screenshot; // remote toDataURL from here
    a.download = "sg.png";
    a.click();
    document.body.removeChild(a);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用html2canvas将png保存到服务器 - save png to server using html2canvas 如果我第二次下载打印屏幕,则出现 html2canvas 错误 - html2canvas error if I download printscreen second time 使用 react 创建登录页面时出现以下错误:错误:超出最大更新深度 - I am getting the following error while creating a signin page using react : Error: Maximum update depth exceeded 使用 HTML2Canvas 下载图像 - Download Image Using HTML2Canvas 在angular2 Typescript应用程序中使用html2canvas出现错误 - using html2canvas in angular2 Typescript application getting error 使用具有png扩展名的html2canvas捕获网页的屏幕截图 - Capture the screenshot of a webpage using html2canvas with png extension html2canvas无法下载 - html2canvas unable to download 创建快照时,Chrome 版本 87 不支持 HTML2CANVAS 的 function canvas.toDataURL("image/png") - Chrome version 87 not supporting function canvas.toDataURL("image/png") of HTML2CANVAS when creating snapshot 如何在dowllaods文件夹中而不是在浏览器中使用html2canvas和FileSaver下载div的内容? - How can I download content of div using html2canvas and FileSaver in the dowllaods folder instead of in the browser? 如何使用html2Canvas自动下载截图 - How to download the screenshot automatically by using html2Canvas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM