首页 / 知识
html2canvas把div保存高清图的方法代码
2023-04-11 15:12:00
http://www.bootcdn.cn/(可以搜索html2canvans)
1.选择html2canvas版本(这个版本可以放大倍数保证图片清晰)
默认生成的canvas图片在retina设备上显示很模糊,处理成2倍图能解决这个问题:
varw=$("#code").width();
varh=$("#code").height();
//要将canvas的宽高设置成容器宽高的2倍
varcanvas=document.createElement("canvas");
canvas.width=w*2;
canvas.height=h*2;
canvas.style.width=w+"px";
canvas.style.height=h+"px";
varcontext=canvas.getContext("2d");
//然后将画布缩放,将图像放大两倍画到画布上
context.scale(2,2);
html2canvas(document.querySelector("#code"),{
canvas:canvas,
onrendered:function(canvas){
...
}
});
下载方法:
.on('click','.download',function(){
$('#mycanvas').remove();
var_height=$('.skinReport').height();
//滚到顶部
$('html,body').animate({scrollTop:0});
if(confirm('是否下载肌肤检测报告?'))
{
setTimeout(function(){
varcanvas=document.createElement("canvas"),
w=$('#skinReport').width(),
h=$('#skinReport').height();
canvas.width=w*2;
canvas.height=h*2;
canvas.style.width=w+"px";
canvas.style.height=h+"px";
varcontext=canvas.getContext("2d");
//然后将画布缩放,将图像放大两倍画到画布上
context.scale(2,2);
html2canvas(document.getElementById('skinReport'),{
allowTaint:false,
taintTest:true,
canvas:canvas,
onrendered:function(canvas){
canvas.id="mycanvas";
canvas.style.display='none';
document.body.appendChild(canvas);
//生成base64图片数据
imgData=canvas.toDataURL(type);
//varnewImg=document.createElement("img");
//newImg.src=dataUrl;
//document.body.appendChild(newImg);
//console.log(imgData);
var_fixType=function(type){
type=type.toLowerCase().replace(/jpg/i,'jpeg');
varr=type.match(/png|jpeg|bmp|gif/)[0];
return'image/'+r;
};
//加工imagedata,替换mimetype
imgData=imgData.replace(_fixType(type),'image/octet-stream');
/**
*在本地进行文件保存
*@param{String}data要保存到本地的图片数据
*@param{String}filename文件名
*/
varsaveFile=function(data,filename){
varsave_link=document.createElementNS('http://www.w3.org/1999/xhtml','a');
save_link.href=data;
save_link.download=filename;
varevent=document.createEvent('MouseEvents');
event.initMouseEvent('click',true,false,window,0,0,0,0,0,false,false,false,false,0,null);
save_link.dispatchEvent(event);
};
//下载后的问题名
varfilename=aname+'肌肤检测报告'+(newDate()).getTime()+'.'+type;
//download
saveFile(imgData,filename);
},
width:1512,
height:15000
})
},2500)
}
else
{
return;
}
})
本文转载自中文网 |
最新内容
相关内容
python如何读取列表中元素的位置?
python如何读取列表中元素的位置?,位置,数据,异常,培训,字符串,元素,索引,方法,示例,结果,python读取列表中元素位置的方法:1、使用index()方python中获取路径的三种方法
python中获取路径的三种方法,工作,代码,情况,培训,下来,路径,文件,也就是,桌面,目录,python中获取路径总结下来分为三种情况:1、获取工作目录python如何调用另一个文件夹中的内
python如何调用另一个文件夹中的内容?,系统,培训,文件,模块,内容,路径,函数,所在,前缀,语句,python中调用另外一个文件夹中的内容:1、同一文件python怎么找出所有的数字?
python怎么找出所有的数字?,数字,培训,代码,小数点,小数,字符串,整数,表达式,含义,思路,python中获取字符串中所有数字的方法:1、使用正则表达如何在python代码中指定保存的文件
如何在python代码中指定保存的文件格式,代码,培训,文件格式,格式,二进制文件,文件,后缀,以上,方法,更多,python指定保存文件格式的方法:1、保Python如何复制文件中的内容
Python如何复制文件中的内容,盘中,数据,培训,文件,内容,方法,文件夹,路径,源文件,文件名,python复制文件中内容的方法:1、使用shutil.copyfile实数是不是python的数据类型?
实数是不是python的数据类型?,数字,标准,培训,实数,数据类型,数轴,复数,有限小数,无理数,虚数,实数是python的数据类型。实数,是有理数和无理Python写完代码怎么用
Python写完代码怎么用,代码,下来,数字,概念,培训,文本,模式,文件,程序,路径,python运行有两种方式,一种是在python交互式命令行下运行;另一种python读入不定行字符的问题
python读入不定行字符的问题,培训,字符,问题,方法,头尾,示例,空格,字符串,序列,结尾,python读入不定行字符的问题解决:stdin.readline()会读取python中的int是什么意思?
python中的int是什么意思?,数字,数据,培训,字符串,参数,语法,实例,以下,方法,函数,python中的int()函数用于将一个字符串或数字转换为整型。Python库与模块的区别是什么?
Python库与模块的区别是什么?,名称,概念,标准,机构,培训,名字,代码,模块,区别,函数,python库与模块的区别是:模块、库主要区别在于他们的定义python的闭包函数是什么
python的闭包函数是什么,代码,工作,函数,培训,程序,外部,个数,变量,嵌套,赋值,python中的闭包,又称闭包函数或者闭合函数,其实和前面讲的嵌套函