canvas标签学习之文本

1.font属性

<body>
	<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
		Your browser does not support the HTML5 canvas tag.
	</canvas>
	
	<script type="text/javascript">
		var c = document.getElementById('myCanvas');
		var ctx = c.getContext('2d');
		ctx.font = "40px Arial";
		ctx.fillText('hello world',10,50);
	</script>
</body>

font 属性设置或返回画布上文本内容的当前字体属性。

属性值

描述
font-style

规定字体样式。可能的值:

  • normal
  • italic
  • oblique
font-variant

规定字体变体。可能的值:

  • normal
  • small-caps
font-weight

规定字体的粗细。可能的值:

  • normal
  • bold
  • bolder
  • lighter
  • 100
  • 200
  • 300
  • 400
  • 500
  • 600
  • 700
  • 800
  • 900
font-size / line-height规定字号和行高,以像素计。
font-family规定字体系列。
caption使用标题控件的字体(比如按钮、下拉列表等)。
icon使用用于标记图标的字体。
menu使用用于菜单中的字体(下拉列表和菜单列表)。
message-box使用用于对话框中的字体。
small-caption使用用于标记小型控件的字体。
status-bar使用用于窗口状态栏中的字体。

2.textAlign属性

<body>
	<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
		Your browser does not support the HTML5 canvas tag.
	</canvas>
	
	<script type="text/javascript">
		var c = document.getElementById('myCanvas');
		var ctx = c.getContext('2d');
		ctx.strokeStyle = 'blue';
		ctx.moveTo(150,20);
		ctx.lineTo(150,170);
		ctx.stroke();
		ctx.textAlign = 'start';
		ctx.fillText('textAlign=start',150,60);
		ctx.textAlign = 'end';
		ctx.fillText('textAlign=end',150,80);
		ctx.textAlign = 'left';
		ctx.fillText('textAlign=left',150,100);
		ctx.textAlign = 'center';
		ctx.fillText('textAlign=center',150,120);
		ctx.textAlign = 'right';
		ctx.fillText('textAlign=right',150,140);
	</script>
</body>

textAlign 属性根据锚点,设置或返回文本内容的当前对齐方式。

通常,文本会从指定位置开始,不过,如果您设置为 textAlign="right" 并将文本放置到位置 150,那么会在位置 150 结束。

默认值:start
JavaScript 语法:context.textAlign="center|end|left|right|start";

3.textBaseline属性

<body>
	<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
		Your browser does not support the HTML5 canvas tag.
	</canvas>
	
	<script type="text/javascript">
		var c = document.getElementById('myCanvas');
		var ctx = c.getContext('2d');
		ctx.strokeStyle = 'blue';
		ctx.moveTo(50,100);
		ctx.lineTo(395,100);
		ctx.stroke();
		ctx.textBaseline = 'top';
		ctx.fillText('textBaseline=top',5,100);
		ctx.textBaseline = 'bottom';
		ctx.fillText('textAlign=bottom',50,100);
		ctx.textBaseline = 'middle';
		ctx.fillText('textAlign=middle',120,100);
		ctx.textBaseline = 'alphabetic';
		ctx.fillText('textAlign=alphabetic',190,100);
		ctx.textAlign = 'hanging';
		ctx.fillText('textAlign=hanging',290,100);
	</script>
</body>

textBaseline 属性设置或返回在绘制文本时的当前文本基线。

下面的图示演示了 textBaseline 属性支持的各种基线:

文本基线图示

注释:fillText() 或 strokeText() 方法在画布上定位文本时,将使用指定的 textBaseline 值。

默认值:alphabetic
JavaScript 语法:context.textBaseline="alphabetic|top|hanging|middle|ideographic|bottom";

属性值

描述
alphabetic默认。文本基线是普通的字母基线。
top文本基线是 em 方框的顶端。。
hanging文本基线是悬挂基线。
middle文本基线是 em 方框的正中。
ideographic文本基线是表意基线。
bottom文本基线是 em 方框的底端。

4.fillText()方法

<body>
	<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
		Your browser does not support the HTML5 canvas tag.
	</canvas>
	
	<script type="text/javascript">
		var c = document.getElementById('myCanvas');
		var ctx = c.getContext('2d');
		ctx.font = '20px Georgia';
		ctx.fillText('hello world',10,50);
		
		ctx.font = '30px Verdana';
		var gra = ctx.createLinearGradient(0,0,c.width,0);
		gra.addColorStop('0','magenta');
		gra.addColorStop('0.5','blue');
		gra.addColorStop('1.0','red');
		ctx.fillStyle = gra;
		ctx.fillText('w3school.com.cn',10,90);
		
	</script>
</body>

fillText() 方法在画布上绘制填色的文本。文本的默认颜色是黑色。

提示:请使用 font 属性来定义字体和字号,并使用 fillStyle 属性以另一种颜色/渐变来渲染文本。

context.fillText(text,x,y,maxWidth);
参数描述
text规定在画布上输出的文本。
x开始绘制文本的 x 坐标位置(相对于画布)。
y开始绘制文本的 y 坐标位置(相对于画布)。
maxWidth可选。允许的最大文本宽度,以像素计。

5.strokeText()方法

strokeText() 方法在画布上绘制文本(没有填色)。文本的默认颜色是黑色。

提示:请使用 font 属性来定义字体和字号,并使用 strokeStyle 属性以另一种颜色/渐变来渲染文本

context.strokeText(text,x,y,maxWidth);
参数描述
text规定在画布上输出的文本。
x开始绘制文本的 x 坐标位置(相对于画布)。
y开始绘制文本的 y 坐标位置(相对于画布)。
maxWidth可选。允许的最大文本宽度,以像素计。

6.measureText()方法

<body>
	<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
		Your browser does not support the HTML5 canvas tag.
	</canvas>
	
	<script type="text/javascript">
		var c = document.getElementById('myCanvas');
		var ctx = c.getContext('2d');		
		ctx.font = '30px Verdana';
		var txt = 'hello world';
		ctx.fillText('width:' + ctx.measureText(txt).width,10,50);
		ctx.fillText(txt,10,100);		
	</script>
</body>

measureText() 方法返回包含一个对象,该对象包含以像素计的指定字体宽度。

提示:如果您需要在文本向画布输出之前,就了解文本的宽度,那么请使用该方法。

context.measureText(text).width;
参数描述
text要测量的文本。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值