canvas绘制遮盖矩形的圆角

1.先画一个矩形,然后画2个三角遮盖一下,

2.然后把绿色换成和背景色一样的颜色就可以了

3.效果如下

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  #canvas {
    background: lightgoldenrodyellow;
  }
</style>

<body>

  <canvas id="canvas" width="600" height="600"></canvas>
  <script>
    const canvas = document.getElementById('canvas')
    let ctx = canvas.getContext('2d')

    let r = 50 //圆弧半径
    let x = 50//起始点坐标x
    let y = 20 //起始点坐标y
    let height = 300 //矩形高
    let width = 200 //矩形宽
    ctx.beginPath()
    ctx.fillStyle = 'red'
    ctx.fillRect(x, y, width, height)

    //左上三角
    ctx.beginPath()
    ctx.fillStyle = 'green'
    //移动到顺时针画圆弧的点
    ctx.moveTo(x, y + r)
    // 顺时针画1/4圆弧
    ctx.arc(x + r, y + r, r, Math.PI, 1.5 * Math.PI)
    //圆弧结束点画线到直角点
    ctx.lineTo(x, y)
    //直角点画线到圆弧起点
    ctx.lineTo(x, y + r)
    ctx.fill()
    //左下三角
    ctx.beginPath()
    ctx.fillStyle = 'green'
    ctx.moveTo(x + r, y + height)
    ctx.arc(x + r, y + height - r, r, 0.5 * Math.PI, Math.PI)
    ctx.lineTo(x, y + height)
    ctx.lineTo(x + r, y + height)

    ctx.fill()

    //右上三角
    ctx.beginPath()
    ctx.fillStyle = 'green'
    ctx.moveTo(width - r, y)
    ctx.arc(x + width - r, y + r, r, 1.5 * Math.PI, 2 * Math.PI)
    ctx.lineTo(x + width, y)
    ctx.lineTo(width - r, y)
    ctx.fill()

    //右下三角
    ctx.beginPath()
    ctx.fillStyle = 'green'
    ctx.moveTo(x + width, y + height - r)
    ctx.arc(x + width - r, y + height - r, r, 0, 0.5 * Math.PI)
    ctx.lineTo(x + width, y + height)
    ctx.lineTo(x + width, y + height - r)
    ctx.fill()
  </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值