How To Save Canvas As An Image With canvas.toDataURL()?(如何使用 canvas.toDataURL() 将 Canvas 保存为图像?)
问题描述
我目前正在构建一个 HTML5 网络应用程序/Phonegap 原生应用程序,但我似乎不知道如何使用 canvas.toDataURL() 将我的画布保存为图像.有人可以帮帮我吗?
I'm currently building a HTML5 web app/Phonegap native app and I can't seem to figure out how to save my canvas as an image with canvas.toDataURL(). Can somebody help me out?
这是代码,有什么问题?
Here's the code, what's wrong with it?
//我的画布被命名为canvasSignature"
//My canvas was named "canvasSignature"
JavaScript:
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>
推荐答案
这里有一些代码.没有任何错误.
Here is some code. without any error.
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); // here is the most important part because if you dont replace you will get a DOM 18 exception.
window.location.href=image; // it will save locally
这篇关于如何使用 canvas.toDataURL() 将 Canvas 保存为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 canvas.toDataURL() 将 Canvas 保存为图像?
基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
