首页 技术 正文
技术 2022年11月17日
0 收藏 870 点赞 3,500 浏览 2404 个字

接着上文线条样式[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>

[js高手之路] html5 canvas系列教程 – 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

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

text = ‘跟着ghostwu学习canvas’;oGc.font = ‘bold 30px 微软雅黑’;oGc.fillStyle = ‘#09f’;oGc.fillText( text, 100, 100 );oGc.fillText( text, 100, 200, 200 );

[js高手之路] html5 canvas系列教程 – 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

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>

[js高手之路] html5 canvas系列教程 – 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

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>

[js高手之路] html5 canvas系列教程 – 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

textBaseline:设置文本垂直方向的对齐方式

cxt.textBaseline = ‘属性值’

常见的属性值: alphabetic, top, middle, bottom等

跟上面的textAlign的用法差不多,这个不是很常用

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905