I've used the graphics
package before, but not sure if this can be done. I'm trying to create a dog ear effect programmatically with flex. Can it be done? If not possible, what other options or libraries do I have.
views:
134answers:
2You can do a simple version of this using the graphics object.
First, draw a dark square rectangle in the top corner of your container. Then draw a filled triangle with a white background starting with the top left of your dark rectangle + 1, moveTo bottom left of your dark rectangle + 1, moveTo rectangle left + rectangle width, and finally back to where you started.
Learn about the Graphics class here.
You can get away with the undocumented drawRoundRectComplex() for a trendy rounded corners version:
graphics.drawRoundRectComplex(x, y, width, height, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
It's the actionscript version of the Rectangle Primitive Tools from the Tools Panel.
Surely you can get your head around the Graphics class, as @Robusto suggested. Meanwhile, here's a simple 45 angled version:
var dogEars:Sprite = getDogEars45(200,100,15,0x009900,0x007700);
dogEars.x = dogEars.y = 50;
addChild(dogEars);
function getDogEars45(width:Number,height:Number,cornerSize:Number,baseFill:Number,highlightFill:Number):Sprite{
var rect:Sprite = new Sprite();
var base:Shape = new Shape();
base.graphics.beginFill(baseFill);
base.graphics.lineTo(width-cornerSize,0);
base.graphics.lineTo(width,cornerSize);
base.graphics.lineTo(width,height);
base.graphics.lineTo(0,height);
base.graphics.lineTo(0,0);
rect.addChild(base);
var corner:Shape = new Shape();
corner.graphics.beginFill(highlightFill);
corner.graphics.lineTo(cornerSize,cornerSize);
corner.graphics.lineTo(0,cornerSize);
corner.graphics.lineTo(0,0);
corner.graphics.endFill();
rect.addChild(corner);
corner.x = width-cornerSize;
return rect;
}
Here's how the rough(45 angled) version should look like:
Update: Had a few minutes to play with this, here is some code for rounded versions, for documenting:
var dogEarsRounded:Sprite = getFlippedCornerRect(200,150,25,0x009900,0x00CC00);
dogEarsRounded.x = dogEarsRounded.y = 150;
addChild(dogEarsRounded);
var dogEarsRounded2:Sprite = getFlippedCornerRoundedRect(200,150,15,35,0x990000,0xCC0000);
dogEarsRounded2.x = dogEarsRounded2.y = 200;
addChild(dogEarsRounded2);
var dropShadow:DropShadowFilter = new DropShadowFilter(2,30,0,.5,2,2);
dogEarsRounded.filters = dogEarsRounded2.filters = [dropShadow];
function getFlippedCornerRect(width:Number,height:Number,cornerSize:Number,mainFill:int,cornerFill:int):Sprite{
var result:Sprite = new Sprite();
var topRight:Shape = new Shape();
topRight.graphics.beginFill(mainFill);
topRight.graphics.lineTo(width-cornerSize,0);
topRight.graphics.lineTo(width,cornerSize);
topRight.graphics.lineTo(width,height);
topRight.graphics.lineTo(0,height);
topRight.graphics.lineTo(0,0);
topRight.graphics.endFill();
result.addChild(topRight);
var corner:Shape = new Shape();
corner.graphics.beginFill(cornerFill);
corner.graphics.curveTo(0,cornerSize,cornerSize,cornerSize);
corner.graphics.lineTo(0,0);
corner.graphics.endFill();
result.addChild(corner);
corner.x = width-cornerSize;
return result;
}
function getFlippedCornerRoundedRect(width:Number,height:Number,rectRadius:Number,cornerSize:Number,mainFill:int,cornerFill:int):Sprite{
var result:Sprite = new Sprite();
var topRight:Shape = new Shape();
var hw:Number = width * .5;
var hh:Number = height* .5;
topRight.graphics.beginFill(mainFill);
topRight.graphics.lineTo(hw-cornerSize,0);
topRight.graphics.lineTo(hw,cornerSize);
topRight.graphics.lineTo(hw,hw);
topRight.graphics.lineTo(0,hw);
topRight.graphics.lineTo(0,0);
topRight.graphics.endFill();
topRight.x = hw;
result.addChild(topRight);
var corner:Shape = new Shape();
corner.graphics.beginFill(cornerFill);
corner.graphics.curveTo(0,cornerSize,cornerSize,cornerSize);
corner.graphics.lineTo(0,0);
corner.graphics.endFill();
result.addChild(corner);
corner.x = width-cornerSize;
var topLeft:Shape = new Shape();
topLeft.graphics.beginFill(mainFill);
topLeft.graphics.drawRoundRectComplex(0, 0, hw, hh, rectRadius, 0,0,0);
topLeft.graphics.endFill();
result.addChild(topLeft);
var bottomLeft:Shape = new Shape();
bottomLeft.graphics.beginFill(mainFill);
bottomLeft.graphics.drawRoundRectComplex(0, 0, hw, hh, 0, 0,rectRadius,0);
bottomLeft.graphics.endFill();
bottomLeft.y = hh;
result.addChild(bottomLeft);
var bottomRight:Shape = new Shape();
bottomRight.graphics.beginFill(mainFill);
bottomRight.graphics.drawRoundRectComplex(0, 0, hw, hh, 0, 0,0,rectRadius);
bottomRight.graphics.endFill();
bottomRight.x = hw;
bottomRight.y = hh;
result.addChild(bottomRight);
return result;
}
With a soft drop shadow, looks ok:
You can fill the corner with a nice linear gradient, you can modify the function so you can choose which corners are rounded, and which aren't, discretely animate it, etc. Have fun!
I understand dog ears now, just wondering what ever happened to folded corner :P