jQuery 生成svg矢量二维码
jQuery 生成矢量svg二维码,并提供PNG,和SVG的页面下载,减轻服务端的压力。
代码如下所示:
<html> <head> <title>jQuery 生成svg矢量二维码</title> </head> <body> <script type='text/javascript' src='http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js'></script> <script type="text/javascript" src="http://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js"></script> <script type='text/javascript' src='raphael.js'></script> <script type='text/javascript' src='qrcodesvg.js'></script> <p>二维码信息:201211070014</p> <div id="qrcodeTable"></div> <p>二维码信息:gerrard</p> <div id="qrcodeCanvas"></div> <p>二维码信息:test</p> <div id="svg-wrap" class="svg-wrap"></div> <br> <a id="a" href="javascript:saveAsPng()">下载PNG</a> <a id="a" href="javascript:saveAsSvg()">下载SVG</a> <script> var qrcodesvg = new Qrcodesvg("http://www.baidu.com", "svg-wrap", 250); qrcodesvg.draw(); // qrcodesvg.createSquare(); /* //jQuery('#qrcode').qrcode("this plugin is great"); jQuery('#qrcodeTable').qrcode({ render : "table", text : "201211070014" //根据此串生成第一个二维码 }); jQuery('#qrcodeCanvas').qrcode({ render : "canvas", text : "http://www.csdn.net" //根据此串生成第二个二维码 }); */ // 下载png图片 function saveAsPng () { var svgXml = $('.svg-wrap').html(); var image = new Image(); image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgXml))); //给图片对象写入base64编码的svg流 // 把svg格式转换成canvas格式 var canvas = document.createElement('canvas'); //准备空画布 canvas.width = $('.svg-wrap svg').width(); canvas.height = $('.svg-wrap svg').height(); var context = canvas.getContext('2d'); //取得画布的2d绘图上下文 context.drawImage(image, 0, 0); var a = document.createElement('a'); a.href = canvas.toDataURL('image/png'); //将画布内的信息导出为png图片数据 a.download = mathRand(); //设定下载名称 a.click(); //点击触发下载 } // 下载svg图片 function saveAsSvg () { var svgXml = $('.svg-wrap').html(); var image = new Image(); image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgXml))); //给图片对象写入base64编码的svg流 var a = document.createElement('a'); a.href = image.src; //直接导出SVG a.download = mathRand(); //设定下载名称 a.click(); //点击触发下载 } // 随机生成数字 function mathRand() { var num = ""; for(var i = 0 ; i < 6 ; i ++) { num+=Math.floor(Math.random()*10); } return num ; } </script> </body> </html>
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17