首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用HTML画布绘制圆角矩形?

如何使用HTML画布绘制圆角矩形?
EN

Stack Overflow用户
提问于 2009-08-10 23:22:28
回答 11查看 178.5K关注 0票数 165

HTML Canvas提供了绘制矩形、fillRect()strokeRect()的方法,但我找不到一种方法来制作圆角矩形。我该怎么做呢?

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2009-08-10 22:06:28

HTML5画布不提供绘制圆角矩形的方法。

使用lineTo()arc()方法怎么样?

您还可以使用quadraticCurveTo()方法而不是arc()方法。

票数 53
EN

Stack Overflow用户

发布于 2011-10-21 00:07:05

我从@jhoff的解决方案开始,但将其重写为使用宽度/高度参数,并且使用arcTo使其更加简洁:

代码语言:javascript
复制
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
  if (w < 2 * r) r = w / 2;
  if (h < 2 * r) r = h / 2;
  this.beginPath();
  this.moveTo(x+r, y);
  this.arcTo(x+w, y,   x+w, y+h, r);
  this.arcTo(x+w, y+h, x,   y+h, r);
  this.arcTo(x,   y+h, x,   y,   r);
  this.arcTo(x,   y,   x+w, y,   r);
  this.closePath();
  return this;
}

同时返回上下文,这样你就可以稍微链接一下。例如:

代码语言:javascript
复制
ctx.roundRect(35, 10, 225, 110, 20).stroke(); //or .fill() for a filled rect
票数 131
EN

Stack Overflow用户

发布于 2011-09-29 13:06:14

Juan,我对你的方法做了一点改进,允许单独改变每个矩形的圆角半径:

代码语言:javascript
复制
/** 
 * Draws a rounded rectangle using the current state of the canvas.  
 * If you omit the last three params, it will draw a rectangle  
 * outline with a 5 pixel border radius  
 * @param {Number} x The top left x coordinate 
 * @param {Number} y The top left y coordinate  
 * @param {Number} width The width of the rectangle  
 * @param {Number} height The height of the rectangle 
 * @param {Object} radius All corner radii. Defaults to 0,0,0,0; 
 * @param {Boolean} fill Whether to fill the rectangle. Defaults to false. 
 * @param {Boolean} stroke Whether to stroke the rectangle. Defaults to true. 
 */
CanvasRenderingContext2D.prototype.roundRect = function (x, y, width, height, radius, fill, stroke) {
    var cornerRadius = { upperLeft: 0, upperRight: 0, lowerLeft: 0, lowerRight: 0 };
    if (typeof stroke == "undefined") {
        stroke = true;
    }
    if (typeof radius === "object") {
        for (var side in radius) {
            cornerRadius[side] = radius[side];
        }
    }

    this.beginPath();
    this.moveTo(x + cornerRadius.upperLeft, y);
    this.lineTo(x + width - cornerRadius.upperRight, y);
    this.quadraticCurveTo(x + width, y, x + width, y + cornerRadius.upperRight);
    this.lineTo(x + width, y + height - cornerRadius.lowerRight);
    this.quadraticCurveTo(x + width, y + height, x + width - cornerRadius.lowerRight, y + height);
    this.lineTo(x + cornerRadius.lowerLeft, y + height);
    this.quadraticCurveTo(x, y + height, x, y + height - cornerRadius.lowerLeft);
    this.lineTo(x, y + cornerRadius.upperLeft);
    this.quadraticCurveTo(x, y, x + cornerRadius.upperLeft, y);
    this.closePath();
    if (stroke) {
        this.stroke();
    }
    if (fill) {
        this.fill();
    }
} 

像这样使用它:

代码语言:javascript
复制
var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
c.fillStyle = "blue";
c.roundRect(50, 100, 50, 100, {upperLeft:10,upperRight:10}, true, true);
票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1255512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档