Category Archives: string manipulation

Twitter Texture Alphabet

Actionscript:
  1. [SWF(width = 1000, height=800, frameRate=60)]
  2.  
  3. // if you don't have flex you'll need to embed this font in your library
  4. [Embed(source="/Library/Fonts/Verdana.ttf", fontFamily="Verdana")]
  5. var Verdana:Class;
  6.  
  7. var canvas:BitmapData = new BitmapData(1000,800,false, 0xFFFFFF);
  8. addChild(new Bitmap(canvas));
  9. var overlay:BitmapData = new BitmapData(1000,800,true, 0x01FFFFFF);
  10.  
  11.  
  12. var txt:TextField = TextField(addChild(new TextField()));
  13. with (txt){
  14.     defaultTextFormat = new TextFormat("Verdana", 10);
  15.     embedFonts = true;
  16.     width = stage.stageWidth-20;
  17.     height = stage.stageHeight-20;
  18.     multiline = true;
  19.     wordWrap = true;
  20.     text ="";
  21.     textColor = 0x444444;
  22.     selectable = false;
  23.     x = y = 10;
  24.     text = "loading...";
  25. }
  26.  
  27.  
  28. var selectWords:Array = [];
  29. var currFirstLetter:String="";
  30. var prevFirstLetter:String="";
  31. var index:int = 0;
  32. var anchorX:Number = 0, anchorY:Number = 0, theta:Number, radius:Number;
  33. addEventListener(Event.ENTER_FRAME, onLoop);
  34. function onLoop(evt:Event):void {
  35.      canvas.copyPixels(overlay, overlay.rect, new Point(0,0), null, null, true);
  36.      var leng:int = selectWords.length;
  37.      if (leng> 0){
  38.          var word:String = selectWords[index % leng];
  39.          trace(word);
  40.          currFirstLetter = word.charAt(0);
  41.          if (currFirstLetter != prevFirstLetter){
  42.              anchorX = Math.random() * (stage.stageWidth - 200) + 100
  43.              anchorY = Math.random() * (stage.stageHeight - 200) + 100;
  44.              theta = Math.random() * Math.PI * 2;
  45.              radius = 50 + Math.random() * 100;
  46.              txt.textColor = [0,0xFFFFFF,0xCCCCCC,0x666666][int(Math.random()*4)];
  47.          }else{
  48.              txt.x = anchorX + radius * Math.cos(theta);
  49.              txt.y = anchorY + radius * Math.sin(theta);
  50.              theta += Math.random() * .1;
  51.              radius += 3;
  52.              txt.scaleX = txt.scaleY = 1 + Math.random();
  53.              if (int(Math.random()*30) == 1) txt.scaleX = txt.scaleY = 1 + Math.random()*10;
  54.              txt.rotation = theta / Math.PI * 180;
  55.              txt.text = word;
  56.              canvas.draw(txt, txt.transform.matrix);
  57.          }
  58.          prevFirstLetter = currFirstLetter;
  59.          index++;
  60.      }
  61. }
  62.  
  63. var loader:URLLoader = new URLLoader();
  64. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  65. var vars:URLVariables = new URLVariables();
  66. vars.q = "alphabet";
  67. // results per page
  68. vars.rpp = "100";
  69. vars.page = 1;
  70. vars.lang = "en";
  71.  
  72. req.data = vars;
  73. req.method = URLRequestMethod.POST;
  74. loader.load(req);
  75. loader.addEventListener(Event.COMPLETE, onLoaded);                                 
  76. function onLoaded(evt:Event):void{
  77.     var searchData:XML = new XML(loader.data);
  78.     var atom:Namespace = searchData.namespace("");
  79.     default xml namespace = atom;
  80.     var titles:String = "";
  81.     for each(var entry:XML in searchData.entry){
  82.         titles += entry.title.toString();
  83.     }
  84.     var index:int = 0;
  85.     var words:Array = titles.split(" ");
  86.     for (var i:int = 0; i<words.length; i++){
  87.         // exclude a few things
  88.         if (words[i].match(/RT|\@|\#|http/g).length == 0){
  89.             selectWords[index++] = words[i].replace(/\s|\r|\n|\"/g, "");
  90.         }
  91.     }
  92.     selectWords.sort();
  93.     txt.text =""
  94.        removeChild(txt);
  95. }

This snippet draws a texture using a twitter search for the word "alphabet". The results are arranged alphabetically and used to draw a texture.


Have a look at the swf:

Also posted in BitmapData, external data, strings | Tagged , , , | 2 Comments

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...

Also posted in external data, strings | Tagged , , , | 5 Comments

Dot Syntax from String

Actionscript:
  1. function dotSyntax(target:*, path:String):* {
  2.     var level:Array=path.split(".");
  3.     var curr:* = target;
  4.     for (var i:int = 0; i<level.length; i++) {
  5.         curr=curr[level[i]];
  6.     }
  7.     return curr;
  8. }
  9.  
  10. trace(dotSyntax(this, "stage.stageWidth"));
  11. trace(dotSyntax(this, "graphics"));
  12. trace(dotSyntax(this, "root.loaderInfo.bytesTotal"));
  13.  
  14. /*outputs something like:
  15. 800
  16. [object Graphics]
  17. 1230
  18. */

This snippet shows how to parse dot syntax from a string. It does this by splitting the string and then using square bracket syntax. This is one of the main techniques that makes yesterdays post possible.

Also posted in dynamic, strings | Tagged , , | Leave a comment

XML to ActionScript #3 (AsXML)

XML:
  1. <code>
  2.   <make reference="w" class="BasicView" args="stage.stageWidth, stage.stageHeight, false"/>
  3.   <call method="addChild" args="w"/>
  4.  
  5.   <make reference="wireMat" class="WireframeMaterial" args="0x000000" />
  6.  
  7.   <make reference="sphere" class="Sphere" args="wireMat, 100" />
  8.  
  9.   <call method="w.scene.addChild" args="sphere" />
  10.  
  11.   <make reference="animation" class="Object">
  12.     <set z="-500" rotationY="360"  rotationX="360" ease="Back.easeOut"/>
  13.   </make>
  14.  
  15.   <call method="TweenLite.to" args="sphere, 3, animation" />
  16.  
  17.   <call method="setInterval" args="w.singleRender, 32" />
  18.  
  19. </code>

This snippet shows XML that the mini-library AsXML can read and run - in this case AsXML is set up to run with Papervision

A few days ago I had the idea to write some code that would run ActionScript based on XML. I spent some time getting rid of a few bugs and setting up some demos with TweenLite, Papervision and QuickBox2D. I wrapped everything up into a mini-library called AsXML.

Check out the demos here.


Download AsXML and demo files here.

AsXML Features:
1) call methods of the main timeline
2) read and write properties on the main timeline
3) instantiate classes on the main timeline
4) call methods on these classes
5) read and write properties on these classes
6) store references to return values from functions

Also posted in Box2D, Graphics, Math, QuickBox2D, XML, dynamic, external data, instantiation, misc, motion, return values, strings | Tagged , , | 11 Comments