html5 canvas 显示文字居中,html5 canvas 文字居中对齐

[color=eight:25px]html部门

[color=eight:25px]

[color=eight:25px]css部门

[color=eight:25px]canvas{width:1000px;height:700px;}

[color=eight:25px]js部门

初初的根本格局:

[color=eight:25px]window.οnlοad=function(){var canvas=document.getElementById('myCanvas');

var context=canvas.getContext('2d');

//前面书写画绘号令

context.beginPath();//开端画绘的声明

context.moveTo(x,y);//界说出发点,能够理解为将绘笔挪动迪苹个位置

context.lineTo(x,y);//界说一个起点

context.lineWidth=5;//界说线条宽度

context.strokeStyle='blue';//界说线条颜色

context.lineCap='round';//界说线帽(露圆角、尖角、斜角)

context.stroke();//给线条上色,即停止画造

}

//正在此先举一个绘线的小例子

效果图:

[color=eight:25px]

代码以下:

< html>

< head>

< meta charset=utf-8 />

< title>canvas绘线

< /head>

< body>

< /body>

< script type="text/JAVAscript">

var can=document.getElementById('test');

var draw=can.getContext('2d');

draw.beginPath();

draw.moveTo(50,100);

draw.lineTo(300,100);

draw.lineWidth=50;

draw.strokeStyle='#0f0';

draw.lineCap='round';

draw.stroke();

< /script>

< /html>尔后的代码只交换beginPath()后的内容

接下去,再引见寂?属性

context.font="文字特殊款式 字体巨细 字体"//此中文字特殊款式可选,假如没有挖写,默许为normal,借可使用italic暗示斜体等等

context.fillStyle='#f00';//文字颜色

context.strokeText('利用canvas创立的文字——有边框',x,y);//创立文字,控造文件的肇端位置

context.fillText('利用canvas创立的文字——有添补',x,y);//创立文字,控造文件的肇端位置

context.textAlign='center';//文本程度对齐方法

context.textBaseline='middle';//文本垂曲标的目的,基线位置

[color=eight:25px]

[color=eight:25px]举个例子。先看效果

[color=eight:25px]

代码:

draw.font="normal 30px arial"

draw.fillStyle='#00f';

draw.fillText('利用canvas创立的文字——有添补',50,50);

draw.font="italic 40px arial"

draw.strokeStyle='#f00';

draw.strokeText('利用canvas创立的文字——无添补',50,120);

接下去再用程度对齐,基线对齐两个属性,吭哟效果

[color=eight:25px]1)对上面的'利用canvas创立的文字——无添补'加减:程度居中对齐

[color=eight:25px]

draw.strokeStyle='#f00';

draw.textAlign='center';//加减的一止代码

draw.strokeText('利用canvas创立的文字——无添补',50,120);

效果以下:

[color=eight:25px]

假如改成draw.textAlign='right',则效果以下

可睹,所谓的left、right、center指的是定位参考面的位置

right暗示,以整段文字的最左侧做为定位面,然后那个面的位置位于上里所界说的120px(横背)位置

那里很容易弄治,请留意!

最初再引见一个属性,获与天生的文本的少度

draw.measureText(文本或变量名称)

举个例子

代码以下:

var can=document.getElementById('test');

var draw=can.getContext('2d');

var text='利用canvas创立的文字——有添补'

draw.beginPath();

draw.font="normal 30px arial"

draw.fillStyle='#00f';

draw.fillText(text,50,50);

var a=draw.measureText(text);

var width=a.width;

draw.font="normal 30px arial"

draw.fillStyle='#f00';

draw.fillText('文字少度为'+width+'px',50,100);假使我改动width=a.width的位置

[color=eight:25px]会发生别的的效果(那是代码的从上到下剖析所酿成的)

[color=eight:25px]代码以下:

[color=eight:25px]

var can=document.getElementById('test'); var draw=can.getContext('2d'); var text='利用canvas创立的文字——有添补'; var a=draw.measureText(text); var width=a.width; draw.beginPath(); draw.font="normal 30px arial" draw.fillStyle='#00f'; draw.fillText(text,50,50); draw.font="normal 30px arial" draw.fillStyle='#f00'; draw.fillText('文字少度为'+width+'px',50,100); 效果如图:

[color=eight:25px]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值