omwteam / html2canvas Goto Github PK
View Code? Open in Web Editor NEW优化版的html2canvas 0.5.0-beta4 ,html2canvas html截图插件图片放大清晰度解决方案,支持任意放大倍数,解决原插件图片偏移问题
优化版的html2canvas 0.5.0-beta4 ,html2canvas html截图插件图片放大清晰度解决方案,支持任意放大倍数,解决原插件图片偏移问题
场景:echarts 词云
chart宽高有限制,词比较多,通过设置letter-spacing也不能很好解决掉
页面展示并不重叠,截图后就会出现重叠
主要是为了解决在vue中打印某个dom的问题,我先生成图片在使用默认配置比较模糊,使用网上高清的方法以后,打印预览都乱了,整幅大,但是内容反而缩小了。不知道打印机读的是读的哪个长宽?
用到了printJS
goPrint(){
var cntElem = this.$refs.printContent;
var shareContent = cntElem;//需要截图的包裹的(原生的)DOM 对象
var width = shareContent.offsetWidth; //获取dom 宽度
var height = shareContent.offsetHeight; //获取dom 高度
var canvas = document.createElement("canvas"); //创建一个canvas节点
var scale = 2; //定义任意放大倍数 支持小数
canvas.width = width * scale; //定义canvas 宽度 * 缩放
canvas.height = height * scale; //定义canvas高度 *缩放
canvas.getContext("2d").scale(scale, scale); //获取context,设置scale
var opts = {
canvas: canvas, //自定义 canvas
width: width, //dom 原始宽度
height: height
};
html2canvas(shareContent, opts).then(function (canvas) {
var context = canvas.getContext('2d');
// 【重要】关闭抗锯齿
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
printJs(canvas.toDataURL("image/png"), 'image');
document.body.appendChild(canvas);
});
}
根本就不能用嘛
代码如下
`
private handleCreate = () => {
const {
mainDom: { current: mainDom },
} = this.state;
if (mainDom === null) {
return;
}
let isCreacting = true;
mainDom.style.display = 'block';
// @ts-ignore
import('@/utils/html2canvas.min.js')
.then(({ default: html2canvas }) =>
html2canvas(mainDom, {
allowTaint: true,
tainttest: true,
height: mainDom.clientHeight,
width: mainDom.clientWidth,
backgroundColor: '#00263c',
scale: 2,
letterRendering: true,
logging: true,
}),
)
.then(canvas => {
setTimeout(() => {
if (isCreacting) {
isCreacting = false;
alert('您的手机版本过低,请升级至最新版本后重新打开。');
}
}, 2000);
const resultSrc = canvas.toDataURL('image/png');
this.setState({ resultSrc }, () => {
mainDom.style.display = 'none';
isCreacting = false;
});
});
};
// less
.result {
width: 100%;
height: 100vh;
background: #00263c;
display: flex;
flex-direction: column;
font-variant: normal;
}
`
IOS显示
内容过多canvas.toDataURL("image/jpeg", 1.0) 生成的字符串为data
在一个div中(overflow:hidden)放入一个img,然后使用transform进行旋转,img有一部分超出了div的宽度或高度,此时使用html2canvas保存图片后发现hidden部分也显示在保存后的图片中,这种如何解决呢
经过各种截图发现,只能截画面中的一部分。。。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.