taquaki-satwo
11/27/2017 - 3:06 PM

Canvasで角丸の矩形を描く

JS- Canvasで角丸の矩形を描く

window.onload = function() {
  const canvas = document.getElementById('mycanvas');
  const ctx = canvas.getContext('2d');
  
  strokeRoundedRect(ctx, 10, 10, 100, 80, 20)

  function strokeRoundedRect(ctx, x, y, width, height, radius) {
    ctx.beginPath();
    ctx.moveTo(x+radius, y);
    ctx.arcTo(x+width, y, x+width, y+height, radius);
    ctx.arcTo(x+width, y+height, x, y+height, radius);
    ctx.arcTo(x, y+height, x, y, radius);
    ctx.arcTo(x, y, x+width, y, radius);
    ctx.stroke();
  }
}

JS- Canvasで角丸の矩形を描く

A Pen by Takaaki Sato on CodePen.

License.

canvas#mycanvas(width='640' height='400')