请帮我看看为什么水平居中和垂直居中会有问题

来源:6-2 编程练习

qq_上帝之手_3

2017-12-30 21:36:06

// canvas中的文字

var str='前端路径课程';


//设置文本样式,比如大小,字体

ctx.font="30px microsoft yahei";

//水平对齐设置,left center right

ctx.textAlign='center';

//垂直对齐设置 top middle bottom

ctx.textBaseline="center";

//填充文本

ctx.fillStyle="blue";

ctx.fillText(str,0,0);

//获取文本宽度

var width=ctx.measureText(str).width+"px";


ctx.beginPath();

var str1 = '字体的大小是:'+width;

ctx.fillText(str1,0, 100);

console.log(width);


写回答

1回答

怎么都被占用了呢

2017-12-31

文本居中的方式是先将坐标定位在画布的中间位置,然后在设置居中对齐。因为canvas画布的大小是:800*800 ,所以:

http://img.mukewang.com/climg/5a48c82a00015ab906300526.jpg

视频中讲的很详细的哦

0

0 学习 · 626 问题

查看课程