如何在HTML5 Canvas'Text上添加边框?
-
07-07-2019 - |
题
我可以使用以下代码绘制文字,
myCanvas.fillStyle = "Red";
myCanvas.font = "30pt Arial";
myCanvas.fillText("Some Text", 10, 30);
但我想在“Some Text”周围添加一个边框,有什么想法吗?
解决方案
使用 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>
其他提示
我们可以使用 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>
怎么样?
myCanvas.style.border = "red 1px solid";
不隶属于 StackOverflow