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: <div id="createPNGButton"> <button onclick="putImage()">Save as Image</button> </div>
解决方案:
代码中缺少将图像转换为二进制流的步骤。修改代码为:
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); window.location.href = image; // Save locally
通过将图像转换为二进制流并将其视为文件,代码可以本地保存该图像。
以上是为什么我的 canvas.toDataURL() 没有保存我的图像?的详细内容。更多信息请关注PHP中文网其他相关文章!