Created
June 29, 2012 03:01
-
-
Save digitalicarus/3015428 to your computer and use it in GitHub Desktop.
canvas rounded rectangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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