canvas绘制文本

  1. 属性和方法

    font = value                设置字体
    textAlign = value           设置字体对齐方式 start, end, left, right, center
    textBaseline = value        设置基线对齐方式 top, hanging, middle, alphabetic, ideographic, bottom
    direction = value           设置文字书写方向 ltr, rtl, inherit
    
    fillText(text, x, y [, maxWidth])       实心文字
    strokeText(text, x, y [, maxWidth])     空心文字
    
  2. 绘制文字

    canvas绘制文本

    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    
    ctx.font = '48px serif';
    ctx.textBaseline = 'hanging';
    ctx.fillText('Hello world', 10, 50);
    ctx.strokeText('Hello world', 10, 100);
    
  3. 获取文字宽度

    var text = ctx.measureText('Hello'); 
    console.log(text.width);
    

相关文章:

  • 2022-01-11
  • 2021-06-14
  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-10-14
  • 2021-04-27
  • 2022-12-23
相关资源
相似解决方案