是否有一种动态的方法可以根据高度、宽度、X、Y或矩形以及文本长度来居中文本?我正在尝试手动计算按钮文本的X/Y坐标,但困难重重。
忽略我使用roundRect方法制作圆角的事实,有什么好的方法可以使文本居中呢?
ctx.lineWidth = 4;
ctx.strokeStyle = "#000000";
ctx.fillStyle = "#abc";
roundRect(ctx, 10, 10, 100, 50, 10, true);
ctx.font="20px Georgia";
ctx.fillStyle = "#000000";
var rectHeight = 50;
var rectWidth = 100;
var rectX = 10;
var rectY = 10;
ctx.fillText("Attack!",rectX+(rectWidth/2),rectY+(rectHeight/2));
请查看代码片段:http://jsfiddle.net/vu7dZ/1/
ctx
的textAlign
属性? - Noble Mushtak