【发布时间】:2012-05-02 20:41:45
【问题描述】:
我想弄清楚如何将文本添加到画布形状 例如这里是我的代码:
var text ="5"; // text to display over the circle
context.fillStyle = "red";
context.beginPath();
context.arc(50,70, 10, 0, Math.PI * 2);
context.closePath();
context.fill();
如果有人能帮我将文本添加到形状中,我会非常感激 提前致谢。
编辑 我发现我需要在画布上再写一次,所以这就是我到目前为止得到的……但是 文本不对齐圆心:
context.fillStyle = "red";
context.beginPath();
var radius = 10; // for example
context.arc(200, 200, radius, 0, Math.PI * 2);
context.closePath();
context.fill();
context.fillStyle = "black"; // font color to write the text with
var font = "bold " + radius +"px serif";
context.font = font;
context.textBaseline = "top";
context.fillText(text, 200-radius/4 ,200-radius/2);
【问题讨论】: