记录canvas学习3

1.文本绘制

1.1绘制方法

strokeText(text,x,y) 描边文本
fillText(text,x,y) 填充文本

    context.strokeStyle='#00f'
    context.fillStyle='#f00'
    context.strokeText('helloWorld',60,60)
    context.fillText('helloWorld',60,100)
image.png

1.2文本样式

可以通过设置文本的font属性来设置文本的样式
可按顺序设置一系列font的样式,如style,weight,size,fontFamily等
至少要设置size和fontFamily

    context.strokeStyle='#00f'
    context.fillStyle='#f00'
    context.font='700 30px Arail'
    context.strokeText('helloWorld',60,60)
    context.fillText('helloWorld',60,100)
image.png

1.3文本垂直对齐方式

首先以绘制文本的起始点(60,60)的纵坐标为基点画一条直线

    context.moveTo(10,60)
    context.lineTo(390,60)
    context.stroke()
    context.strokeStyle='#00f'
    context.fillStyle='#f00'
    context.font='700 30px Arail'
    context.strokeText('helloWorld',60,60)
image.png

由上图可以发现,文本在垂直方向是以基线的顶端对齐的。
可通过修改textBaseLine属性来调整垂直对齐方式。

top:基线位于文本顶部
hanging:悬挂基线
middle:居中基线
ideogeophic:表意基线
bottom:底部基线


Collage_Fotor_Fotor.jpg

1.4文本水平对齐方式

可以通过设置textAlign属性来设置文本的对齐方式

常用属性如
left 左对齐
right 右对齐
center 居中
start 以基线起始x坐标左对齐
end 以基线起始x坐标右对齐

1.5文本投影

文本投影需要的常用属性
shadowColor:设置投影的颜色
shadowBlur:设置模糊的阶数,默认为0不模糊
shadowOffsetX:设置阴影x轴偏移量
shadowOffsetY:设置阴影y轴偏移量

    context.fillStyle='#f00'
    context.font='700 30px Arail'
    context.shadowColor='#0f0'
    context.shadowBlur=10
    context.shadowOffsetX=10
    context.shadowOffsetY=10
    context.fillText('helloWorld',60,60)
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、canvas简介 1.1 什么是canvas?(了解) 是HTML5提供的一种新标签 Canvas是一个矩形区...
    Looog阅读 3,953评论 3 40
  • 一:canvas简介 1.1什么是canvas? ①:canvas是HTML5提供的一种新标签 ②:HTML5 ...
    GreenHand1阅读 4,707评论 2 32
  • 一、canvas简介 1.1 什么是canvas?(了解) 是HTML5提供的一种新标签 Canvas是一个矩形区...
    J_L_L阅读 1,552评论 0 4
  • 转载请声明 原文链接 关注公众号获取更多资讯 这篇文章主要总结H5的一些新增的功能以及一些基础归纳,这里只是一个提...
    前端进阶之旅阅读 9,099评论 22 225
  • “让我的爱伴着你直到永远,你有没有感觉到我为你担心,在相对的视线里才发现什么是缘,你是否也在等待有一个知心爱人?”...
    大咖口才秀阅读 633评论 0 0