Canavs在文字上绘制删除线/中划线
效果图:

思路:
- 绘制文字
- 绘制高度为1px的长方形,并用黑色填充
- 长方形的宽度为文字的长度
具体代码:
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
const text = 'hello world';
ctx.font = '26px/26px Arial';
ctx.fillStyle = '#f00';
ctx.fillText(text, 100, 100);
ctx.beginPath();
const textWidth = ctx.measureText(text).width;
ctx.rect(100, 92, textWidth, 2);
ctx.fillStyle = '#000';
ctx.fill();

浙公网安备 33010602011771号