DNB*_*ims 16 html javascript html5 canvas
我可以使用下面的代码绘制文字,
myCanvas.fillStyle = "Red";
myCanvas.font = "30pt Arial";
myCanvas.fillText("Some Text", 10, 30);
Run Code Online (Sandbox Code Playgroud)
但我想在"Some Text"周围添加一个边框,有什么想法吗?
小智 32
使用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();
Run Code Online (Sandbox Code Playgroud)
<canvas id="myc"></canvas>
Run Code Online (Sandbox Code Playgroud)
我们可以使用StrokeStyle方法在文本或轮廓周围绘制边框,并且我们可以使用lineWidth方法来定义描边线的宽度。
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
Run Code Online (Sandbox Code Playgroud)
<canvas id="Canvas01" width="400" height="400" style="border:2px solid #bbb; margin-left:10px; margin-top:10px;"></canvas>
Run Code Online (Sandbox Code Playgroud)