canvas08:lineCap属性与绘制线两端样式

lineCap:用于绘制线条两端的样式。属性值有“butt”、“round”、“square”。 

属性值作用
butt默认,线条的两端为平行的边缘
round向线条的两端添加半圆形线帽
square向线条的两端添加正方形线帽

案例:

       绘制三条直线,分别使用lineCap的三个属性绘制末端,观察区别。

代码:

<body>
    <canvas id="mycanvas" width="500" height="500"></canvas>
</body>
</html>
<script>
    // 获得canvas
    var mycanvas = document.getElementById('mycanvas');
    // 获取上下文
    var ctx = mycanvas.getContext('2d');
    // 绘制第一条直线
    ctx.beginPath();
    ctx.moveTo(50, 50);
    ctx.lineTo(200,50);
    ctx.strokeStyle = 'red';
    ctx.lineWidth = 10;
    // 使用butt
    ctx.lineCap = 'butt';
    ctx.stroke();
    // 绘制第二条直线
    ctx.beginPath();
    ctx.moveTo(50, 100);
    ctx.lineTo(200,100);
    ctx.strokeStyle = 'green';
    ctx.lineWidth = 10;
    // 使用square
    ctx.lineCap = 'square';
    ctx.stroke();
    // 绘制第三条直线
    ctx.beginPath();
    ctx.moveTo(50, 150);
    ctx.lineTo(200,150);
    ctx.strokeStyle = 'blue';
    ctx.lineWidth = 10;
    // 使用round
    ctx.lineCap = 'round';
    ctx.stroke();
</script>

运行结果:

结论:

       使用butt与square,直线两端的样式看似一样,均为方形。但是,square比butt两端要长。因为square相当与将两个正方形拼接到线条的两端,多处的部分就为正方形的长度。使用round,线条两端为半圆形,长度不butt要长,相当于将两个半圆拼接在线条的两端。所以,"round" 和 "square" 会使线条略微变长。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值