a标签下载canvas生成的图片

<a :href="downloadImg" download="code.jpg"><el-button @click="loadImg">下载二维码</el-button></a>

 

rqcode.js生成二维码

canvas: qrcode._oDrawing._elCanvas

img: qrcode._oDrawing._elImage

 

this.qrcode = new QRCode(document.getElementById("qrcode"), {

                text: this.publishPath,

               width: 128,

               height: 128,

               colorDark : "#000000",

               colorLight : "#ffffff",

               correctLevel : QRCode.CorrectLevel.H

            });

 

href = qrcode._oDrawing._elImage.src


 

downloadImg(){

           var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");

            save_link.href = this.qrcode._oDrawing._elImage.src;

            save_link.download = "code.jpg";

            var event = document.createEvent('MouseEvents');

            event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

            save_link.dispatchEvent(event);

        }