![]() |
![]() |
||||||
|
|||||||
| Tags: |
![]() |
|
|||
|
I'm trying to make a timer in the form of a circle that fills as time passes. I
found some code to create the circle, but now I want to fill the circle as the 'hand' of the clock sweeps. My problem is that I cannot figure out how to fill successive circle segments with color. Instead, I appear to be filling a rectangle, but that does not achieve my goal of a smooth fill. I think the code below will clarify my issue. Does anyone know how I can fill successive segments as the clock arm continues to sweep? <?xml version="1.0"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"> <mx:UIComponent id="mainDisplay"/> <mx:Script> <![CDATA[ import flash.display.Shape; import flash.events.TimerEvent; import flash.utils.Timer; private var shape1:Shape private function init():void { var myTimer:Timer = new Timer(200); var timeInterval:int=0 myTimer.addEventListener("timer", timerHandler); myTimer.start(); function timerHandler():void{ timeInterval+=10 drawWedge(100,300, timeInterval, 360, 200, 200) } } private function drawWedge(x, y, startAngle, arc, radius, yRadius):void { var segAngle:Number var theta:Number var angle:Number var angleMid:Number var segs:Number var ax:Number var ay:Number var bx:Number var by:Number var cx:Number var cy:Number // Flash uses 8 segments per circle, to match that, we draw in a maximum // of 45 degree segments. First we calculate how many segments are needed // for our arc. segs = Math.ceil(Math.abs(arc)/45); // Now calculate the sweep of each segment. segAngle = arc/segs; // The math requires radians rather than degrees. To convert from degrees // use the formula (degrees/180)*Math.PI to get radians. theta = -(segAngle/180)*Math.PI; // convert angle startAngle to radians angle = -(startAngle/180)*Math.PI; // draw the curve in segments no larger than 45 degrees. if (segs>0) { // draw a line from the center to the start of the curve ax = x+Math.cos(startAngle/180*Math.PI)*radius; ay = y+Math.sin(-startAngle/180*Math.PI)*yRadius; shape1=new Shape shape1.graphics.moveTo(x, y); shape1.graphics.lineStyle(1, 0xFFff00); shape1.graphics.lineTo(ax, ay); // Loop for drawing curve segments for (var i:int = 0; i<segs; i++) { angle += theta; angleMid = angle-(theta/2); bx = x+Math.cos(angle)*radius; by = y+Math.sin(angle)*yRadius; cx = x+Math.cos(angleMid)*(radius/Math.cos(theta/2)); cy = y+Math.sin(angleMid)*(yRadius/Math.cos(theta/2)); shape1.graphics.curveTo(cx, cy, bx, by); } mainDisplay.addChild(shape1) // close the wedge by drawing a line to the center shape1.graphics.beginFill(0xFF0000); shape1.graphics.lineTo(x, y); shape1.graphics.endFill(); } }; ]]> </mx:Script> </mx:Application> |
| Sponsored Links |
|
|||
|
Wow, this your lucky day!
Flex pretty much sucks at drawing graphics, but the DeGrafa people have a cool framework you can use. I understand that Adobe has been working with DeGrafa to add such graphics capabilities to Flex 4. here is an example of a timer: http://dean-logan.com/demo/Flex/EggTimer/ here is the DeGrafa website: http://degrafa.org/samples/ Give it a try and see what you think. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise