gpt4 book ai didi

javascript - 将 Canvas 转换为 pdf : black background

转载 作者:行者123 更新时间:2023-11-28 06:01:24 28 4
gpt4 key购买 nike

我想将我的 html Canvas 导出为 pdf 并下载它,我做了一些搜索,以下脚本对我来说效果很好:

<script language = "JAVASCRIPT">
var canvas = document.getElementById('mycanvas');

var context = canvas.getContext('2d');

// draw a blue cloud
context.beginPath();
context.moveTo(170, 80);
context.bezierCurveTo(130, 100, 130, 150, 230, 150);
context.bezierCurveTo(250, 180, 320, 180, 340, 150);
context.bezierCurveTo(420, 150, 420, 120, 390, 100);
context.bezierCurveTo(430, 40, 370, 30, 340, 50);
context.bezierCurveTo(320, 5, 250, 20, 250, 50);
context.bezierCurveTo(200, 5, 150, 20, 170, 80);
context.closePath();
context.lineWidth = 5;
context.fillStyle = '#8ED6FF';
context.fill();
context.strokeStyle = '#0000ff';
context.stroke();

download.addEventListener("click", function() {
// only jpeg is supported by jsPDF
var imgData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new jsPDF();

pdf.addImage(imgData, 'JPEG', 0, 0);
var download = document.getElementById('download');

pdf.save("download.pdf");
}, false);
</script>

但是我的 Canvas 是黑色背景的!

我尝试过其他代码和在互联网上找到的许多解决方案,我尝试更改 Canvas 标签中的背景颜色,但没有任何效果..

请问有什么帮助吗?

最佳答案

不要下载为 JPEG,而是尝试下载为 PNG:

download.addEventListener("click", function() {
// only jpeg is supported by jsPDF
var imgData = canvas.toDataURL("image/png", 1.0);
var pdf = new jsPDF();

pdf.addImage(imgData, 'PNG', 0, 0);
var download = document.getElementById('download');

pdf.save("download.pdf");
}, false);

关于javascript - 将 Canvas 转换为 pdf : black background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37244994/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com