Skip to content

Instantly share code, notes, and snippets.

@digitalicarus
Created June 29, 2012 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalicarus/3015428 to your computer and use it in GitHub Desktop.
Save digitalicarus/3015428 to your computer and use it in GitHub Desktop.
canvas rounded rectangle
var roundRect = function(ctx,o) {
if(!ctx || !o.x || !o.y || !o.w || !o.h || !o.r) {
return;
}
var x = o.x,
y = o.y,
w = o.w,
h = o.h,
r = o.r,
s = o.stroke,
f = o.fill;
ctx.save();
r = (r*2<w && r*2<h) ? r : (w<h) ? w/2 : h/2;
ctx.beginPath();
ctx.moveTo(x+r,y);
// top and top right
ctx.arcTo(x+w,y,x+w,y+h-r,r);
// bottom right and bottom
ctx.arcTo(x+w,y+h,x+r,y+h,r);
// bottom left corner and left
ctx.arcTo(x,y+h,x,y+r,r);
// top left corner
ctx.arcTo(x,y,x+r,y,r);
if(s) {
if(o.strokeStyle){ctx.strokeStyle = o.strokeStyle;}
ctx.stroke();
}
if(f) {
if(o.fillStyle){ctx.fillStyle = o.fillStyle;}
ctx.fill();
}
ctx.restore();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment