html2canvas边框线,html2canvas 实现dashed虚线边框

1cd74d6224fffc88eefaaa919c14fb51.png

16334daeea3ebf22c2dab4ead4ff817f.png

2477c2605fc8deb46157ba96362d04ba.png

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: dashedBorder[index]

}

borders.push(border);

td.style[`border${dashedBorder[index]}Color`]= 'transparent';

});

}

});returnborders;

}

page是需要绘制canvas的元素,我这里有dashed边框的都是td元素,所以查找所有td元素,使用getComputedStyle()方法查找它的borderStyle,如果它有dashed边框,那么这个属性的值的形式为"dashed dashed none none",所以根据findAll()这个自定义方法找到所有的dashed的方向(比如"dashed dashed none none"将返回[0, 1]),其中dashedBorder数组如下:

const dashedBorder = ["Top", "Right", "Bottom", "Left"];

找到方位后同时获取它的位置,将方向和位置信息存入borders数组,同时将这条边框设为透明,使html2canvas不绘制这条框,我们之后会单独处理。(注意:这个页面的虚线边框都会透明掉,这里并没有考虑绘制完成后将透明边框变回原来的颜色)

2、使用html2canvas得到绘制后的canvas

pages.forEach((page, idx) =>{

const borders= this.findDashedBorders(page);

const parentRect=page.getBoundingClientRect();

html2canvas(page, {scale:2, logging: false, useCORS: true}).then((canvas) =>{this.drawDashedBorder(canvas, borders, parentRect);

......

})

})

pages是需要绘制canvas的所有元素,我们在获取每个page的虚线边框时,同时获取这个page的位置,以便我们绘制dashed边框时得到边框相对于这个页面的位置。待html2canvas绘制canvas后,我们通过drawDashedBorder()方法进一步绘制出dashed边框,下面实现这个方法。

3、drawDashedBorder()在得到canvas后进一步绘制虚线。

drawDashedBorder = (canvas, borders, parentRect) =>{var ctx = canvas.getContext("2d");

ctx.setLineDash([6, 3]);

ctx.strokeStyle= '#3089c7';

ctx.lineWidth= 1;

ctx.globalAlpha= 1;

borders.forEach(border=>{var left = (border.rect.left + 0.5 - parentRect.left)*2;var right = (border.rect.right - 0.5 - parentRect.left)*2;var top = (border.rect.top + 0.5 - parentRect.top)*2;var bottom = (border.rect.bottom - 0.5 - parentRect.top)*2;switch(border.border) {case 'Top':

ctx.beginPath();

ctx.moveTo(left, top);

ctx.lineTo(right, top);

ctx.stroke();break;case 'Right':

ctx.beginPath();

ctx.moveTo(right, top);

ctx.lineTo(right, bottom);

ctx.stroke();break;case 'Bottom':

ctx.beginPath();

ctx.moveTo(left, bottom);

ctx.lineTo(right, bottom);

ctx.stroke();break;case 'Left':

ctx.beginPath();

ctx.moveTo(left, top);

ctx.lineTo(left, bottom);

ctx.stroke();break;default:break;

}

})

}

意思就是根据borders里dashed边框的方向和位置信息,在canvas里获取2d上下文后使用setLineDash方法把虚线绘制出来。位置都*2是因为我们之前canvas使用了2倍大小。

4、这个方法目前只在chrome测试可用,firefox无效,因为firefox下getComputedStyle返回的信息和chrome不同。

由于对canvas理解不深无法pr,只能期待html2canvas的官方实现了。

内容来源于网络如有侵权请私信删除

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值