Sweet Alert Pop up Prompt Before Downloading Canvas HTML Image to PNG(在将 Canvas HTML 图像下载为 PNG 之前弹出甜蜜警报提示)
问题描述
在我的 Canvas 绘图应用程序中,我有一个下载到 png 按钮,我想制作它,以便只有当用户在我的甜蜜警报弹出提示上单击是的保存它"时,画布上的图像才会下载.现在它仍在自动下载.谢谢您的帮助.(另外,如果有人有更好的方式通过 Javascript 下载也有帮助,它正在下载 png,但它已损坏,我无法打开它)
in my Canvas drawing app I have a download to png button, I want to make it so the image off the canvas only downloads when the user clicks "yes save it" on my sweet alert pop up prompt. Right now it's still downloading automatically. Thank you for your help. (also if someone had a better way to download via Javascript that would help too, it's downloading the png but it is corrupt and I can't open it)
$('#download').click(function(){
swal({
title: "Are you finished your creation?",
text: "click yes to save",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#f8c1D9",
confirmButtonText: "Yes, save it!",
closeOnConfirm: true
}, function (isConfirm) {
if (isConfirm) {
swal("Saving!");
var base64 = document.getElementById("canvas")
.toDataURL("image/png")
.replace(/^data:image/[^;]/, 'data:application/octet-stream');
document.getElementById("download-png").href = base64
} else {
}
return false;
});
});
html
<div id="download">
<a href="#" id="download-png" download="image.png"><img src="./assets/imgs/tools/save.png" /></a>
</div>
推荐答案
你应该包含 canvas-toBlob.js 和 FileSaver.js 进入您的页面,然后:
You should include canvas-toBlob.js and FileSaver.js into your page and then:
function (isConfirm) {
if (isConfirm) {
swal("Saving!");
var canvas = document.getElementById("canvas")
// get the canvas as a blob
canvas.toBlob(function(blob){
// Save the file...
saveAs(blob, 'my-image.png')
}, "image/png", 0.95); // PNG at 95% quality
} else {
// user cancel
}
return false;
};
这篇关于在将 Canvas HTML 图像下载为 PNG 之前弹出甜蜜警报提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在将 Canvas HTML 图像下载为 PNG 之前弹出甜蜜警报提示
基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
