Canavs在文字上繪制刪除線/中划線


效果圖:

思路:

  1. 繪制文字
  2. 繪制高度為1px的長方形,並用黑色填充
  3. 長方形的寬度為文字的長度

 

具體代碼:

      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();    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM