canvas绘制圆角矩形拆分版

image.webp

<!DOCTYPE html>
<html>
<head>
  <title>绘制圆角矩形拆分版</title>
</head>
<body>
  <canvas id="my-canvas"></canvas>

  <script>
    const canvas = document.getElementById('my-canvas');
    const ctx = canvas.getContext('2d');

    // 绘制填充的圆角矩形
    ctx.fillStyle = '#f00';
    drawRoundRect(ctx, 50, 50, 200, 100, 20);
    ctx.fill();

    // 绘制描边的圆角矩形
    ctx.strokeStyle = '#0f0';
    drawRoundRect(ctx, 100, 100, 200, 100, 30);
    ctx.stroke();

    // 定义绘制圆角矩形的函数
    function drawRoundRect(ctx, x, y, width, height, radius) {
      ctx.beginPath();
      ctx.moveTo(x + radius, y);
      ctx.lineTo(x + width - radius, y);
      ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
      ctx.lineTo(x + width, y + height - radius);
      ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
      ctx.lineTo(x + radius, y + height);
      ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
      ctx.lineTo(x, y + radius);
      ctx.quadraticCurveTo(x, y, x + radius, y);
      ctx.closePath();
    }
  </script>
</body>
</html>
PS:写作不易,如要转裁,请标明转载出处。
%{ comment.page.total }条评论

猜你想看

微信小程序:前端开发宝典

最近文章
工具操作
  • 内容截图
  • 全屏
登录
注册
回顶部