Skip to content

Instantly share code, notes, and snippets.

@luokebi
Created June 16, 2012 15:58
Show Gist options
  • Save luokebi/2941769 to your computer and use it in GitHub Desktop.
Save luokebi/2941769 to your computer and use it in GitHub Desktop.
Canvas:Draw rounded rectangle
function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
if (typeof stroke == "undefined" ) {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
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();
if (stroke) {
ctx.stroke();
}
if (fill) {
ctx.fill();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment