Preloader Camomile


/ Published in: ActionScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import flash.display.Graphics;
  2. import flash.display.Shape;
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.TimerEvent;
  6. import flash.geom.Point;
  7. import flash.utils.Timer;
  8.  
  9. public class PreloaderCamomile extends Sprite
  10. {
  11. private static const RAD:Number = Math.PI / 180;
  12. private const petalsCount:int = 12;
  13.  
  14. private var radius:int;
  15. private var innerRadius:int;
  16. private var petalGage:Number;
  17. private var color:uint;
  18.  
  19. private const petals:Vector.<Shape> = new Vector.<Shape>;
  20. private const timer:Timer = new Timer(80);
  21.  
  22. public function PreloaderCamomile(size:int = 20, color:uint = 0)
  23. {
  24. radius = size * .5;
  25. innerRadius = radius * .42;
  26. petalGage = 360 / petalsCount;
  27. this.color = color;
  28. addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
  29. addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  30. }
  31.  
  32. private function addedToStageHandler(event:Event):void
  33. {
  34. for (var i:int = 1; i <= petalsCount; i++)
  35. {
  36. var petal:Shape = new Shape();
  37. drawPetal(petal.graphics, i * petalGage);
  38. addChild(petal);
  39. petals.push(petal);
  40. petal.alpha = 0;
  41. }
  42. timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
  43. timer.start();
  44. }
  45.  
  46. private function removedFromStageHandler(event:Event):void
  47. {
  48. timer.removeEventListener(TimerEvent.TIMER, timerEventHandler);
  49. timer.stop();
  50. while (petals.length)
  51. {
  52. removeChild(petals.shift());
  53. }
  54. }
  55.  
  56. private function timerEventHandler(event:TimerEvent):void
  57. {
  58. var start:int = timer.currentCount % petalsCount;
  59. var end:int = start + petalsCount + 1;
  60. for (var i:int = start; i < end; i++)
  61. {
  62. var index:int = (i < petalsCount) ? i : i - petalsCount;
  63. petals[index].alpha = 1 / (1 + end - i);
  64. }
  65. }
  66.  
  67. private function drawPetal(g:Graphics, angle:Number):void
  68. {
  69. var p1:Point = Point.polar(radius, angle * RAD);
  70. var p2:Point = Point.polar(radius, (angle + petalGage / 2) * RAD);
  71. var p3:Point = Point.polar(innerRadius, angle * RAD);
  72. var p4:Point = Point.polar(radius, (angle - petalGage / 2) * RAD);
  73.  
  74. g.moveTo(p1.x, p1.y);
  75. g.beginFill(color);
  76. g.curveTo(p2.x, p2.y, p3.x, p3.y);
  77. g.curveTo(p4.x, p4.y, p1.x, p1.y);
  78. g.endFill();
  79. }
  80.  
  81. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.