Resolving Image Saving Issues with canvas.toDataURL()
When attempting to utilize canvas.toDataURL() to save a canvas as an image, you may encounter difficulties. Here's how to approach the situation:
问题与解决方案
问题:
以下是用于保存画布图像的代码,但它不起作用:
// Canvas named "canvasSignature" JavaScript: function putImage() { var canvas1 = document.getElementById("canvasSignature"); if (canvas1.getContext) { var ctx = canvas1.getContext("2d"); var myImage = canvas1.toDataURL("image/png"); } var imageElement = document.getElementById("MyPix"); imageElement.src = myImage; } HTML5:
解决方案:
代码中缺少将图像转换为二进制流的步骤。修改代码为:
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); window.location.href = image; // Save locally
通过将图像转换为二进制流并将其视为文件,代码可以本地保存该图像。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3