2014-03-27 36 views
0

我画过的代码丝带:如何在绘制的画布上书写文字?

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

+0

https://developer.mozilla.org/en-US/docs/Drawing_text_using_a_canvas –

回答

1

可能不起作用,因为th E文的颜色也是红色

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/