canvas节点无法导出图片_html页面、canvas导出图片

该代码段展示了一个名为Export2Image的类,用于将HTML内容或Canvas节点导出为图片。类中包含了初始化、调整尺寸、获取DataURL等方法,并提供了针对不同格式的图片处理。最后,类提供了exportPic方法来实现图片的保存。
摘要由CSDN通过智能技术生成

1 /**2 * Created by tengri on 2016-5-9.3 */

4

5 /**6 * 导出类7 * @param content 要导出的内容8 * @constructor9 */

10 functionExport2Image(content,opts){11 this.exportObj = typeof(content) == "string" ?document.getElementById(content) : content;12 if(!this.exportObj) throw new Error("导出内容对象只能传递ID和DOM对象");13 this.opts = opts ||{};14 if(this.exportObj.nodeName !="CANVAS"){15 this.exportType = "html2Image";16 this.canvas = document.createElement("canvas");17 this.canvas.style.display = "none";18 //如果没有设置宽度和高度,实际是多大就导出多大

19 this.canvas.width = this.opts.width || this.exportObj.scrollWidth + 10;20 this.canvas.height = this.opts.height || this.exportObj.scrollHeight + 10;21 }else{22 this.exportType = "canvas2Image";23 this.canvas = this.exportObj;24 }25 if(this.opts.width && this.opts.height){26 this.actualWidth = this.opts.width;27 this.actualHeight = this.opts.height;28 }29 this.type = this.opts.type || "png";30 this.fileName = (this.opts.name || new Date().getTime()) + "." + this.type;31 this.init();32 return this;33 }34

35 /**36 * 初始化37 */

38 Export2Image.prototype.init = function(){39 this._fixType();40 }41

42 Export2Image.prototype._encodeData = function(data){43 if(!window.btoa) throw "btoa undefined";44 var strDtata = "";45 if(typeof(data) !="string"){46 for(var i = 0 ; i < data.length;i++){47 strDtata +=String.fromCharCode(data[i]);48 }49 }else{50 strDtata =data;51 }52 returnwindow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值