html2canvas是一个将html元素生成canvas的库,绘制的canvas大部分样式和CSS一致。比如截止1.0.0-alpha.12,虚线边框依然绘制为实线,border-collapse依然有问题。
这里根据github issues里的一个思路,模拟实现了dashed边框效果。
适用情况:单独边框较多,即不是完整边框,同时不考虑border-radius
1、首先,在html2canvas绘制前,找出需要绘制canvas的元素中的所有虚线边框的方向和位置
findDashedBorders = (page) =>{
const tds= page.querySelectorAll("td");
const borders=[];
tds.forEach(td=>{
const computedStyle=window.getComputedStyle(td);
const borderStyle= computedStyle.borderStyle ? computedStyle.borderStyle.split(' ') : [];
const dashedIndex= findAll(borderStyle, 'dashed');if(dashedIndex.length) {
const rect=td.getBoundingClientRect();
dashedIndex.map(index=>{
const border={
rect,
border: