首页 > 其他 > 详细

canvas绘制文本

时间:2019-02-09 16:12:58      阅读:146      评论:0      收藏:0      [点我收藏+]

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. 绘制文字

    技术分享图片

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

canvas绘制文本

原文:https://www.cnblogs.com/ye-hcj/p/10357552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!