Posts Tagged ‘filter’

UILoader and borders ..

July 1st, 2008

Today i make a few of you fellow developers a very happy. When you are developing a image gallery you would like to draw a nice white border around your images. Now we all know that is not a problem when you are working with landscape format picture. Lets look at a common method used to give a UILoader a nice white border

JAVASCRIPT:
  1. import flash.display.Sprite;
  2. import flash.net.URLRequest;
  3. import fl.containers.UILoader;
  4. import flash.events.Event;
  5.  
  6. var myLoader:UILoader = new UILoader();
  7. myLoader.addEventListener(Event.COMPLETE, loadComplete);
  8.  
  9. myLoader.width  = 400;
  10. myLoader.height = 300;
  11. myLoader.x      = stage.stageWidth  * .5 - myLoader.width  /2;
  12. myLoader.y      = stage.stageHeight * .5 - myLoader.height / 2;
  13.  
  14. addChild (myLoader);
  15.  
  16. myLoader.load (new URLRequest ('BOY.JPG'));
  17.  
  18. function loadComplete (e:Event):void
  19. {
  20.  
  21. var myBorder:Sprite = new Sprite();
  22. myBorder.graphics.lineStyle(2, 0xFFFFFF);
  23. myBorder.graphics.drawRect (0,0,myLoader.width, myLoader.height);
  24.  
  25. myLoader.addChild (myBorder);
  26.  
  27. }

 

picture1

As you can see above here we have a cosmetic blooper because the border does not go around the inner content (where you want it) of the UILoader but the outer content of the loader its self leaving a black hole between your image and your border. :(

Not to worry my friends there is a solution to this little issue. If you trace UIloader.content you will see that it is in fact a Bitmap Object !. And what can you do with Bitmap objects my friends ? RIGHT !! Apply filters to them. I was abled to solve the issue by adding a filter to the content part of the UILoader on the stage its self. So lets remove the Sprite method and add a nice thin glow filter around it so i looks like a white border.  See for the proper code below...

 

JAVASCRIPT:
  1. import flash.net.URLRequest;
  2. import fl.containers.UILoader;
  3. import flash.events.Event;
  4. import flash.filters.GlowFilter;
  5.  
  6. var myLoader:UILoader = new UILoader();
  7. myLoader.addEventListener(Event.COMPLETE, loadComplete);
  8.  
  9. myLoader.width  = 400;
  10. myLoader.height = 300;
  11. myLoader.x      = stage.stageWidth  * .5 - myLoader.width  /2;
  12. myLoader.y      = stage.stageHeight * .5 - myLoader.height / 2;
  13.  
  14. addChild (myLoader);
  15.  
  16. myLoader.load (new URLRequest ('BOY.JPG'));
  17.  
  18. function loadComplete (e:Event):void
  19. {
  20.  
  21. var glow      = new GlowFilter (0xFFFFFF, 1,2,2,100);
  22. var oContent  = myLoader.content;
  23.  
  24. oContent.filters = [glow];
  25. }

 

picture2

 

Now you can see its all coming to gather in the end no need to worry any more we just acomplished our goal of putting a border around the inner content its self.

PS one more note

JAVASCRIPT:
  1. // <!-- SNIP
  2. var oContent  = myLoader.content;
  3. oContent.filters = [glow];
  4. // SNIP -->

As you see here im assigning the filter to a reference of the myLoader.content, i did this because myLoader its self is read-only so keep this in mind.

 

Download both the good and bad code here