1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > html5 canvas 显示文字居中 html5 canvas 文字居中对齐

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

时间:2019-11-13 21:21:01

相关推荐

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

> web前端 > HTML 5 > 正文

html5 canvas 文字居中对齐

-07-09

我要投稿

[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]

点击复制链接 与好友分享!回本站首页

相关TAG标签

文字

上一篇:HTML5 Canvas渐进填充与透明

下一篇:写个一个简单的canvas图表

相关文章

Sencha toucha2中设置文本框默认提示

HTML5/CSS3(PrefixFree.js) 3D文字特效

HTML5绘图之文字旋转

HTML5:标记文字

canvas 三 图片插入及文字填充

图文推荐

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。