小智 3
有很多方法可以做到这一点。
\n您可以使用 html2canvas 库。\n示例代码 - :
\nhtml2canvas(input, {\n // // dpi: 144,\n backgroundColor: "#FFFFFF",\n // allowTaint: false,\n // taintTest: false,\n })\n .then((canvas) => {\n console.log(canvas);\n canvas.style.display = 'none';\n var image = canvas.toDataURL("png")\n var a = document.createElement("a");\n a.setAttribute('download', 'myImage.png');\n a.setAttribute('href', image);\n a.click();\n }\nRun Code Online (Sandbox Code Playgroud)\n或者你也可以使用 Blob 来保存图像 -:
\ncanvas.toBlob(\nblob => {\n const anchor = document.createElement('a');\n anchor.download = `${this.state.regionName}.jpeg`; // optional, but you can give the file a name\n anchor.href = URL.createObjectURL(blob);\n\n anchor.click(); // \xe2\x9c\xa8 magic!\n\n URL.revokeObjectURL(anchor.href); // remove it from memory and save on memory! \n },\n 'image/jpeg',\n 0.9,\n);\nRun Code Online (Sandbox Code Playgroud)\n