笔划的默认颜色为黑色,其粗细为一个像素。但是,您可以使用strokeStyleandlineWidth属性来设置笔画的颜色和宽度。
以下示例将绘制一条宽度为5像素的橙色线。
例试试这个代码»
<script>
window.onload = function() {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.lineWidth = 5;
context.strokeStyle = "orange";
context.moveTo(50, 150);
context.lineTo(250, 50);
context.stroke();
};
</script>
您也可以使用lineCap属性设置线条的线帽样式。线帽有三种样式-对接,圆形和正方形。这是一个例子:
例试试这个代码»
<script>
window.onload = function() {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.lineWidth = 10;
context.strokeStyle = "orange";
context.lineCap = "round";
context.arc(150, 150, 80, 1.2 * Math.PI, 1.8 * Math.PI, false);
context.stroke();
};
</script>
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h60949.shtml