0

Have code for a rectangle with a line through it, I would like to display text under the rectangle. How do I add this into my code? Or can someone point me in the right direction?

<canvas id="main" width="300" height="300"></canvas>

 <script>
 var canvas = document.getElementById("main");
var context = canvas.getContext('2d');
context.fillStyle = "#008000";
context.rect(0,0,300,300);
context.fill();
context.beginPath();
context.lineWidth = 10;
context.strokeStyle = "blue";
context.lineCap = "round";
context.moveTo( 50, 150);
context.lineTo (250, 150);
context.stroke();
  context.closePath();
4

2 回答 2

1

您可以使用以下方法绘制文本fillText

context.fillText("Hello, world!", 50, 50);

有关详细信息,请参阅Mozilla 开发人员网络上的这篇文章

于 2012-07-08T21:01:33.550 回答
0

我相信你想要这个fillText功能:

context.font="30px Arial";
context.fillText("Hello World",10,50);
于 2012-07-08T21:01:50.490 回答