[js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)
接着上文线条样式[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)继续.
canvas提供两种输出文本的方式:
strokeText:描边文本
fillText:填充文本
fillStyle配合fillText使用,strokeStyle配合strokeText使用
strokeText用法:
cxt.strokeText( text, x, y, [maxwidth] )
text:需要输出的文本内容
x:最左边的文本输出的横坐标位置
y:最左边的文本的 左下角的纵坐标
maxWidth:这个是可选参数,意思就是文本最多能占用maxWidth这么宽,如果文本的实际宽度比maxWidth宽,就会有一种压缩(被挤扁)的效果。
<meta charset='utf-8' />
<style>
body {
background: #000;
}
#canvas{
background:white;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ), text = '跟着ghostwu学习canvas';
oGc.font = 'bold 30px 微软雅黑';
oGc.strokeStyle = '#09f';
oGc.strokeText( text, 100, 100 );
oGc.strokeText( text, 100, 200, 200 );
}
</script>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
</body>

fillText:填充文本,参数跟strokeText一样

measureText:获取文本的宽度(长度),它返回的是一个对象,对象有一个属性width,就是文本的长度了.
cxt.measureText( text ).width
输出一段水平居中的文本
<meta charset='utf-8' />
<style>
body {
background: #000;
}
#canvas{
background:white;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
width = oCanvas.width,
text = '跟着ghostwu学习canvas'; oGc.font = 'bold 30px 微软雅黑';
oGc.fillStyle = '#09f';
oGc.fillText( text, ( width - oGc.measureText( text ).width ) / 2, 100 );
}
</script>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
</body>

font属性跟css是一样的用法
cxt.font = "font-style font-weight font-size/line-height font-family"
textAlign:文本水平对齐方式
cxt.textAlign = 'start/end/left/right/center';
start跟left差不多,end跟right差不多.
<meta charset='utf-8' />
<style>
body {
background: #000;
}
#canvas{
background:white;
}
</style>
<script>
window.onload = function(){
var oCanvas = document.querySelector( "#canvas" ),
oGc = oCanvas.getContext( '2d' ),
width = oCanvas.width,
height = oCanvas.height,
text = '跟着ghostwu学习canvas'; oGc.font = 'bold 16px 微软雅黑';
oGc.fillStyle = '#09f'; var xPos = ( width ) / 2;
oGc.moveTo( xPos, 0 );
oGc.lineTo( xPos, height );
oGc.stroke(); oGc.textAlign = 'start';
oGc.fillText( text, 300, 30 );
oGc.textAlign = 'left';
oGc.fillText( text, 300, 60 );
oGc.textAlign = 'right';
oGc.fillText( text, 300, 90 );
oGc.textAlign = 'end';
oGc.fillText( text, 300, 120 );
oGc.textAlign = 'center';
oGc.fillText( text, 300, 150 );
}
</script>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
</body>

textBaseline:设置文本垂直方向的对齐方式
cxt.textBaseline = '属性值'
常见的属性值: alphabetic, top, middle, bottom等
跟上面的textAlign的用法差不多,这个不是很常用
[js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)的更多相关文章
- [js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)
上文,写完弧度与贝塞尔曲线[js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具),本文主要是关于线条的样式设置 lineWidth: 设置线条的宽 ...
- [js高手之路] html5 canvas系列教程 - 图片操作(drawImage,clip,createPattern)
接着上文[js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)继续,本文介绍的 ...
- [js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API
我们接着上文[js高手之路] html5 canvase系列教程 - 认识canvas以及基本使用方法继续. 一.直线的绘制 cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点 ...
- [js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)
之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, ...
- [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)
绘制曲线,经常会用到路径的知识,如果你对路径有疑问,可以参考我的这篇文章[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解. arc:画 ...
- [js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)
接着上文[js高手之路] html5 canvas系列教程 - 状态详解(save与restore),相信大家都应该玩过美颜功能,而我们今天要讲的就是canvas强大的像素处理能力,通过像素处理,实现 ...
- [js高手之路] html5 canvas系列教程 - 状态详解(save与restore)
本文内容与路径([js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解)是canvas中比较重要的概念.掌握理解他们是做出复杂canvas动 ...
- [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置
接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...
- [js高手之路] html5 canvas动画教程 - 实时获取鼠标的当前坐标
有了前面的canvas基础之后,现在开始就精彩了,后面写的canvas教程都是属于综合应用,前面已经写了常用的canvas基础知识,参考链接如下: [js高手之路] html5 canvas系列教程 ...
随机推荐
- 关于CSS属性clip在手机浏览器端的兼容性问题
由于自己的6坏了拿去修了所以这两天使用了同事一只山寨安卓机和自己早年的小4,在今天的页面测试中,对于img中进行clip操作在这两台机子中均不能实现,后借用同事的6发现clip能正常展现,其中安卓版本 ...
- Apache与Tomcat的关系和区别 -个人比较
我们经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料 ...
- Ubuntu 环境 samba的安装&配置
一.samba的安装: sudo apt-get install samba sudo apt-get install smbfs sudo apt-get install cifs-utils (新 ...
- redis 安装及启动关闭
1.redis下载 方式1:直接去官网下载 https://redis.io/download 方式2:通过命令下载 wget http://download.redis.io/releases/re ...
- webSocket浏览器握手不成功(解决)
websocket与服务端握手会报握手不成功的错误解决方法: 首先是服务端首次收到请求要回报给客户端的报文要做处理多的不说,方法敬上: /// <summary> /// 打包请求连接数据 ...
- throws Exception方法异常处理机制
public class T4 { private String sex; public String getSex() { return sex; } public void setSex(Stri ...
- appium python api收集
1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话中的上下文,使用后可以识别H5页面的 ...
- C++中static关键字作用总结
1.先来介绍它的第一条也是最重要的一条:隐藏.(static函数,static变量均可) 当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.举例来说明.同时编译两个源文件 ...
- Centos 6启动流程详解
author:JevonWei 版权声明:原创作品 Centos6 启动流程 POST开机自检 当按下电源键后,会启动ROM芯片中的CMOS程序检查CPU.内存等硬件设备是否正常运行,CMOS中的程序 ...
- Python Requests: Invalid Header Name 解决方法
这几天在练习python,并且用到了Requests,不得不说真的比urllib 方便了很多啊,简直有点事半功倍的感觉 言归正传,(好像上面的话也没多歪啦~~~~~) 简单叙述下我的script 流程 ...