Tag Archives: as3

I want Twitter

Actionscript:
  1. [SWF(width = 800, height=600, frameRate=15)]
  2.  
  3. var littleTxt:TextField = TextField(addChild(new TextField()));
  4. with (littleTxt){
  5.     defaultTextFormat = new TextFormat("_sans", 10);
  6.     width = stage.stageWidth-20;
  7.     height = stage.stageHeight-20;
  8.     multiline = true;
  9.     wordWrap = true;
  10.     text ="";
  11.     textColor = 0x444444;
  12.     selectable = false;
  13.     x = y = 10;
  14. }
  15.  
  16. var txt:TextField = TextField(addChild(new TextField()));
  17. with (txt){
  18.     defaultTextFormat = new TextFormat("_sans", 90);
  19.     autoSize = "center";
  20.     x = 0
  21.     y = stage.stageHeight / 2 - 61
  22.     width = stage.stageWidth;
  23.     selectable = false;
  24.     text = "loading";
  25. }
  26.  
  27. var selectWords:Array = [];
  28. addEventListener(Event.ENTER_FRAME, onLoop);
  29. function onLoop(evt:Event):void {
  30.      if (selectWords.length> 0){
  31.         var word:String = selectWords[int(Math.random() * selectWords.length)];
  32.         txt.text = word;
  33.         littleTxt.appendText(word+" ");
  34.      }
  35. }
  36.  
  37. var loader:URLLoader = new URLLoader();
  38. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  39. var vars:URLVariables = new URLVariables();
  40. vars.q = "i want";
  41. // results per page
  42. vars.rpp = "50";
  43. vars.page = 1;
  44. vars.lang = "en";
  45.  
  46. req.data = vars;
  47. req.method = URLRequestMethod.POST;
  48. loader.load(req);
  49. loader.addEventListener(Event.COMPLETE, onLoaded);                                 
  50. function onLoaded(evt:Event):void{
  51.     var searchData:XML = new XML(loader.data);
  52.     var atom:Namespace = searchData.namespace("");
  53.     default xml namespace = atom;
  54.     var titles:String = "";
  55.     for each(var entry:XML in searchData.entry){
  56.         titles += entry.title.toString();
  57.     }
  58.     var index:int = 0;
  59.     var words:Array = titles.split(" ");
  60.     for (var i:int = 0; i<words.length; i++){
  61.         // exclude a few things
  62.         if (words[i].match(/RT|\@|\#|http/g).length == 0){
  63.             selectWords[index++] = words[i];
  64.         }
  65.     }
  66. }

This snippet searches twitter for the phrase "I want" and then displays the results randomly word by word in a somewhat hypnotic way.


Have a look at the swf...

Posted in external data, string manipulation, strings | Also tagged , , | 5 Comments

I’m Twitter Search

Actionscript:
  1. [SWF(width = 400, height=600, frameRate=12)]
  2. var loader:URLLoader = new URLLoader();
  3. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  4. var vars:URLVariables = new URLVariables();
  5. vars.q = "I'm";
  6. // results per page
  7. vars.rpp = "7";
  8. vars.page = 1;
  9.  
  10. req.data = vars;
  11. req.method = URLRequestMethod.GET;
  12.  
  13. loader.addEventListener(Event.COMPLETE, onLoaded);
  14. loader.load(req);
  15.  
  16. // check for updates every 30 seconds -
  17. var timer:Timer = new Timer(30000);
  18. timer.addEventListener(TimerEvent.TIMER, onTick);
  19. timer.start();
  20. function onTick(evt:TimerEvent):void{
  21.     txt.htmlText = "loading...";
  22.     loader.load(req);
  23. }
  24.  
  25. var txt:TextField = TextField(addChild(new TextField()));
  26. txt.defaultTextFormat = new TextFormat("_sans", 12);
  27. with (txt){ x=10, y=10, width=stage.stageWidth-10;
  28. height=stage.stageHeight, multiline = true, wordWrap = true; }
  29. txt.htmlText = "loading...";
  30.                                                    
  31. function onLoaded(evt:Event):void{
  32.     var searchData:XML = new XML(loader.data);
  33.     var atom:Namespace = searchData.namespace("");
  34.     default xml namespace = atom;
  35.     var htmlText:String = "<b>Last " + vars.rpp + " '" + vars.q + "' tweets:</b><br><br>";
  36.     for each(var entry:XML in searchData.entry){
  37.         htmlText += entry.author.name.toString() + ": <br>";
  38.         htmlText += entry.title.toString() + "<br><br>";
  39.     }
  40.     txt.htmlText = htmlText;
  41. }

Was curious about the twitter search api so spent a few minutes and whipped up a quick demo - the api is simple and easy to understand...

As a test I created something that shows the last 7 tweets including the phrase I'm. A new search is done every 30 seconds... I chose the phrase I'm so that there will be different results every time a new search is done...

Read More »

Posted in XML, external data | Also tagged , , | 1 Comment

Perlin Outlines

Actionscript:
  1. [SWF(width = 600, height=600, backgroundColor=0xCCCCCC, frameRate=24)]
  2. var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0x000000);
  3. var blur:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0x000000);
  4. addChild(new Bitmap(canvas, "auto", true));
  5.  
  6. var w:int = canvas.width
  7. var hw:int = w / 2;
  8. var size:int = w * w;
  9. var seed:Number = Math.random()*100;
  10. var pnt:Point = new Point();
  11. var dy:Number = 0, dx:Number = 0;
  12. var blr:BlurFilter = new BlurFilter(10,10,1);
  13. addEventListener(Event.ENTER_FRAME, onLoop);
  14. function onLoop(evt:Event):void {
  15.    
  16.     dx += (mouseX - dx) / 4;
  17.     dy += (mouseY - dy) / 4;
  18.     canvas.lock();
  19.     canvas.perlinNoise(hw,hw,2,seed,false, false, 1, true, [new Point(dx, dy), new Point(-dx, -dy)]);
  20.     var pix:Vector.<uint> = canvas.getVector(canvas.rect);
  21.     for (var i:int = 0; i<size; i++){
  22.         var col:uint = 255 - pix[i] <<4 & 0x00FF00;
  23.         pix[i] = col <<8 | col | col>> 8;
  24.     }
  25.     canvas.setVector(canvas.rect, pix);
  26.     blur.copyPixels(canvas, canvas.rect, pnt);
  27.     blur.applyFilter(blur, blur.rect, pnt, blr);
  28.     canvas.draw(blur, null, null, BlendMode.DIFFERENCE);
  29.     canvas.draw(canvas, null, null, BlendMode.INVERT);
  30.     canvas.unlock();
  31. }

This is actually an optimized variation on some recent posts that made use of perlin noise. You can get a wide range of effects by changing just the BlendMode values alone.... I particularly like this combination of BlendModes because it reminds me a bit of a terrain map...

Have a look at the swf...

Posted in BitmapData, Vector, pixel manipulation | Also tagged , | Leave a comment

Parallax fp10

Actionscript:
  1. [SWF(backgroundColor=0x000000, width = 800, height = 600)]
  2.  
  3. // this is a trick to keep the 3D texture quality up...
  4. // try setting it right off the bat and you'll notice that the
  5. // Shapes look pixilated
  6. setTimeout(function():void{ stage.quality="low"}, 500);
  7.  
  8. var matrix:Matrix = new Matrix();
  9. matrix.createGradientBox(600, 600, 0, -450, -450);
  10.  
  11. var boxNum:int = 30;
  12. var boxes:Array = [];
  13. for (var i:int = 0; i<boxNum; i++) boxes[i] = makeBox();
  14.  
  15. var dx:Number = 0, dy:Number = 0;
  16. onLoop();
  17. addEventListener(Event.ENTER_FRAME, onLoop);
  18.  
  19. function onLoop(evt:Event=null):void {
  20.     dx += (mouseX - dx) / 4;
  21.     dy += (mouseY - dy) / 4;
  22.     for (var i:int = 0; i<boxNum; i++){
  23.         var box:Shape = boxes[i];
  24.         box.z = 400 - i * 20;
  25.         box.x = dx;
  26.         box.y = dy;
  27.         box.rotation = i + getTimer() / 10;
  28.     }
  29. }
  30.  
  31. function makeBox():Shape{
  32.     var box:Shape = Shape(addChild(new Shape()));
  33.     box.x = stage.stageWidth/2;
  34.     box.y = stage.stageHeight/2;
  35.     box.z = 1;
  36.     with (box.graphics){
  37.          beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0x333333], [1,1], [0, 255], matrix, SpreadMethod.PAD);
  38.         drawRect(-100, -100, 200, 200);
  39.         drawRect(-70, -70, 140, 140);
  40.     }
  41.     return box;
  42. }

This snippet draws 30 gradient box shapes, gives them different z values and then moves them based on the mouse. This technique is good if you just want a few layers of parallax motion - I got carried away and you'll notice that if you add more boxes it begins to slow down pretty quick.


Have a look at the swf....



I first used this technique for this small interactive drawing...

Something interesting I noticed about fp10 3D DisplayObjects is that if you set the stage.quality to low right off the bat, the display objects look pixelated... but if you wait a few milliseconds, you end up with less pixelation and you still get a speed boost from the low quality - I think it must have something to do with the way the 3D textures are handled by the player...

Tomorrow I think I'll post a version of this that uses IGraphicsData and Utils.projectVectors()... should be a huge speed boost...

Posted in 3D, motion | Also tagged , | 1 Comment