首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用边框系统下载画布

用边框系统下载画布
EN

Stack Overflow用户
提问于 2022-05-09 06:42:10
回答 2查看 123关注 0票数 0

我想下载带有边框的画布。在屏幕上画布上有边框,但是当我下载画布时,它的边框不会出现。

代码语言:javascript
复制
let canvas=qrRef.current.querySelector("canvas")
canvas.style.border="black 30px solid"
 if(type=="PNG"){
    let image=canvas.toDataURL("image/png")
    let anchor=document.createElement("a");
    anchor.href=image;
    anchor.download=`qr-pollective.png`
    document.body.appendChild(anchor);
    anchor.click()
    document.body.removeChild(anchor)
}

Console.log(画布)

代码语言:javascript
复制
<canvas height="400" width="400" style="border: 30px solid black; border-radius: 5px; border-collapse: collapse;">
EN

回答 2

Stack Overflow用户

发布于 2022-05-09 10:04:03

你能做的就是创造一个更大的画布,它有黑色边框的空间。

然后将您创建的画布复制到这个更大的画布上,由30 to抵消,为边框腾出空间。

然后绘制构成边框的4个黑色矩形。这个更大的画布就是你想要下载的。

代码语言:javascript
复制
const canvas = document.querySelector('canvas');
const biggerCanvas = document.createElement('canvas');
const w = canvas.width + 60;
const h = canvas.height + 60;
biggerCanvas.width = w;
biggerCanvas.height = h;
const ctx = biggerCanvas.getContext('2d');
ctx.drawImage(canvas, 30, 30);
ctx.beginPath();
ctx.rect(0, 0, w, 30);
ctx.fillStyle = "black";
ctx.fill();
ctx.rect(0, 0, 30, h);
ctx.fill();
ctx.rect(w - 30, 0, w - 30, h);
ctx.fill();
ctx.rect(0, h - 30, w, h);
ctx.fill();
document.body.append(biggerCanvas); //just to show it has drawn the border
代码语言:javascript
复制
<canvas height="400" width="400" style="border: 30px solid black; border-radius: 5px; border-collapse: collapse;">

票数 0
EN

Stack Overflow用户

发布于 2022-05-09 10:38:32

我用html2canvas解决了我的问题这里是我的代码

代码语言:javascript
复制
html2canvas(canvas,{backgroundColor: "transparent"}).then(function (canvas) {
           var myImage = canvas.toDataURL();
           downloadURI(myImage, `qr-pollective.png`);
});

function downloadURI(uri, name) {
      var link = document.createElement("a");
   
      link.download = name;
      link.href = uri;
      document.body.appendChild(link);
      link.click();   

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72167865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档