Graphics Class and Twitter

Actionscript:
  1. [SWF(width = 700, height=700, frameRate=12)]
  2. var cmds:Array = [];
  3.  
  4. var loader:URLLoader = new URLLoader();
  5. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  6. var vars:URLVariables = new URLVariables();
  7. vars.q = "#asgraph";
  8. // results per page
  9. vars.rpp = "100";
  10. vars.page = 1;
  11.  
  12. req.data = vars;
  13. req.method = URLRequestMethod.GET;
  14.  
  15. loader.addEventListener(Event.COMPLETE, onLoaded);
  16. loader.load(req);
  17.  
  18. var txt:TextField = TextField(addChild(new TextField()));
  19. txt.defaultTextFormat = new TextFormat("_sans", 12);
  20. with (txt){ x=10, y=10, autoSize="left"; }
  21. txt.htmlText = "loading...";
  22.  
  23. function onLoaded(evt:Event):void{
  24.        removeChild(txt);
  25.    var searchData:XML = new XML(loader.data);
  26.    var atom:Namespace = searchData.namespace("");
  27.  
  28.    var leng:int = searchData.atom::entry.length() -1;
  29.   for (var i:int = leng; i>=0; i--){
  30.        var cmd:String =
  31. searchData.atom::entry[i].atom::title.toString().replace(/\#asgraph/g,
  32. "");
  33.       // added this to ignore words starting with @
  34.       cmd = cmd.replace(/@(\w+)/g, "");
  35.        cmds.push(cmd);
  36.    }
  37.  
  38.        var time:Timer = new Timer(100, cmds.length);
  39.        time.addEventListener(TimerEvent.TIMER, onTick);
  40.    time.start();
  41. }
  42. function onTick(evt:TimerEvent):void{
  43.        render(parseFunctions(cmds[evt.target.currentCount - 1]));
  44.        graphics.endFill();
  45.        graphics.lineStyle();
  46. }
  47.  
  48. // parse and run Graphics class commands
  49. function parseFunctions(dat:String):Array{
  50.    var a:Array = dat.split(";") ;
  51.    for (var i:int = 0; i<a.length-1; i++){
  52.        a[i] = a[i].split(/\(\)|\(|\)/g);
  53.        var f:String = a[i][0] = a[i][0].replace(/\s/g,"");
  54.        a[i] = a[i].splice(0, a[i].length - 1);
  55.        if (a[i].length> 1){
  56.         a[i] = a[i][1].split(",");
  57.         a[i].unshift(f);
  58.        }
  59.    }
  60.    return a.splice(0,a.length - 1);
  61. }
  62. function render(p:Array):void {
  63.    for (var i:int = 0; i<p.length; i++) {
  64.        try{
  65.        graphics[p[i][0]].apply(graphics,p[i].splice(1));
  66.        }catch(e:Error){};
  67.    }
  68. }

This is a simple idea I had awhile back... This snippet searches twitter for the #asgraph hashtag and if it finds standard Graphics class method calls it renders them.

So if you tweet something likethis :

#asgraph beginFill(0xFF); drawCircle(200,200,10);

it will get rendered into the below swf (you'll need to refresh to see your tweet get rendered):

This movie requires Flash Player 9

Here is a direct link to the swf

This entry was posted in Graphics, external data and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted December 11, 2009 at 3:27 pm | Permalink

    Very clever. Now off to tweet an advertisement for my site. ;-)

  2. Posted December 11, 2009 at 3:37 pm | Permalink

    Very nice Idea ;) Really !

  3. Posted December 11, 2009 at 3:50 pm | Permalink

    ha very cool. expand out to look for other stuff, loops, very cool idea.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*