如何在绘制的画布上书写文字?

时间:2014-03-27 02:30:58

标签: javascript html html5 canvas

我已经用代码绘制了一个功能区:

var canvas = document.getElementById('draw-ribbon');
ctx = canvas.getContext("2d");
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(300, 0);
ctx.lineTo(260, 75);
ctx.lineTo(300, 150);
ctx.lineTo(0, 150);
ctx.closePath();
ctx.fill();

得到了这个:

enter image description here

在此之后我尝试了filltext()但是没有工作我想实现这个目标:

enter image description here

1 个答案:

答案 0 :(得分:1)

可能无效,因为文字颜色也是红色

var canvas = document.getElementById('myCanvas');
ctx = canvas.getContext("2d");
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(300, 0);
ctx.lineTo(260, 75);
ctx.lineTo(300, 150);
ctx.lineTo(0, 150);
ctx.closePath();
ctx.fill();
ctx.font="30px Verdana";
ctx.fillStyle = 'white';
ctx.fillText("Text",50,80);

http://jsfiddle.net/dvdyakonov/zFg5q/