2009-09-14 2023 views

回答

31

使用strokeText()strokeStyle.如:

canvas = document.getElementById("myc"); 
 
context = canvas.getContext('2d'); 
 

 
context.fillStyle = 'red'; 
 
context.strokeStyle = 'black'; 
 

 
context.font = '20pt Verdana'; 
 
context.fillText('Some text', 50, 50); 
 
context.strokeText('Some text', 50, 50); 
 

 
context.fill(); 
 
context.stroke();
<canvas id="myc"></canvas>

+0

wao!我从未在SO中见过这样的“可执行”答案。这是一个新功能吗? – Samiron 2014-11-28 15:59:12

-1

什么

myCanvas.style.border = "red 1px solid"; 
+15

这增加边框到画布 - 而不是在画布上绘制内的​​文本;我假设op想要以字母本身的形状创建边框。 – dionyziz 2012-03-24 23:20:54

4

我们可以使用的StrokeStyle方法来绘制文本周围或外框,和我们可以使用lineWidth方法来定义笔划线的宽度。

<script> 
    var canvas = document.getElementById('Canvas01'); 
    var ctx = canvas.getContext('2d'); 

    ctx.strokeStyle= "red"; //set the color of the stroke line 
    ctx.lineWidth = 3; //define the width of the stroke line 
    ctx.font = "italic bold 35pt Tahoma"; //set the font name and font size 
    ctx.strokeText("StackOverFlow",30,80); //draw the text 

</script> 
</body>