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