fattymelt
August 28th, 2009, 03:59 PM
My idea was to create a method for drawing rectangle with rounded corners, but while being able to specify which of the four corners (any, all, none,etc.) you wanted to round.
The method below does the trick, but for some reason my corners don't match the same curves that I get using the same radius with drawRoundRect().
In other words, if I call my method (and set all four corners to be rounded) and compare that with the same call to drawRoundRect() the actual curves of the corners are slightly different.
Anyone have some insight into this?
public function drawComplexRoundedRect(startX:int,startY:int,rectW idth:int,rectHeight:int,radius:int,canvas:Sprite,r oundTopLeft:Boolean=false,roundTopRight:Boolean=fa lse,roundBottomLeft:Boolean=false,roundBottomRight :Boolean=false):void{
if(roundTopLeft){
canvas.graphics.moveTo(startX,(startY+radius));
}else{
canvas.graphics.moveTo(startX,startY);
}
if(roundBottomLeft){
canvas.graphics.lineTo(startX,(rectHeight-radius));
canvas.graphics.curveTo(startX,rectHeight,radius,r ectHeight);
}else{
canvas.graphics.lineTo(startX,rectHeight);
}
if(roundBottomRight){
canvas.graphics.lineTo((startX+rectWidth-radius),rectHeight);
canvas.graphics.curveTo((startX+rectWidth),rectHei ght,(startX+rectWidth),(rectHeight-radius));
}else{
canvas.graphics.lineTo((startX+rectWidth),rectHeig ht);
}
if(roundTopRight){
canvas.graphics.lineTo((startX+rectWidth),startY+r adius);
canvas.graphics.curveTo((startX+rectWidth),startY, (rectWidth-radius),startY);
}else{
canvas.graphics.lineTo((startX+rectWidth),startY);
}
if(roundTopLeft){
canvas.graphics.lineTo(startX+radius,startY);
canvas.graphics.curveTo(startX,startY,startX,(star tY+radius));
}else{
canvas.graphics.lineTo(startX,startY);
}
}then compare
var canvas:Sprite = new Sprite;
drawComplexRoundedRect(0,0,200,200,25,canvas,true, true,true,true);with
var canvas:Sprite = new Sprite;
canvas.drawRoundRect(0,0,200,200,25);and you will see the two rectangles are not identical.
The method below does the trick, but for some reason my corners don't match the same curves that I get using the same radius with drawRoundRect().
In other words, if I call my method (and set all four corners to be rounded) and compare that with the same call to drawRoundRect() the actual curves of the corners are slightly different.
Anyone have some insight into this?
public function drawComplexRoundedRect(startX:int,startY:int,rectW idth:int,rectHeight:int,radius:int,canvas:Sprite,r oundTopLeft:Boolean=false,roundTopRight:Boolean=fa lse,roundBottomLeft:Boolean=false,roundBottomRight :Boolean=false):void{
if(roundTopLeft){
canvas.graphics.moveTo(startX,(startY+radius));
}else{
canvas.graphics.moveTo(startX,startY);
}
if(roundBottomLeft){
canvas.graphics.lineTo(startX,(rectHeight-radius));
canvas.graphics.curveTo(startX,rectHeight,radius,r ectHeight);
}else{
canvas.graphics.lineTo(startX,rectHeight);
}
if(roundBottomRight){
canvas.graphics.lineTo((startX+rectWidth-radius),rectHeight);
canvas.graphics.curveTo((startX+rectWidth),rectHei ght,(startX+rectWidth),(rectHeight-radius));
}else{
canvas.graphics.lineTo((startX+rectWidth),rectHeig ht);
}
if(roundTopRight){
canvas.graphics.lineTo((startX+rectWidth),startY+r adius);
canvas.graphics.curveTo((startX+rectWidth),startY, (rectWidth-radius),startY);
}else{
canvas.graphics.lineTo((startX+rectWidth),startY);
}
if(roundTopLeft){
canvas.graphics.lineTo(startX+radius,startY);
canvas.graphics.curveTo(startX,startY,startX,(star tY+radius));
}else{
canvas.graphics.lineTo(startX,startY);
}
}then compare
var canvas:Sprite = new Sprite;
drawComplexRoundedRect(0,0,200,200,25,canvas,true, true,true,true);with
var canvas:Sprite = new Sprite;
canvas.drawRoundRect(0,0,200,200,25);and you will see the two rectangles are not identical.