Graphics Class and Twitter

[SWF(width = 700, height=700, frameRate=12)]
var cmds:Array = [];

var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
var vars:URLVariables = new URLVariables();
vars.q = "#asgraph";
// results per page
vars.rpp = "100";
vars.page = 1;

req.data = vars;
req.method = URLRequestMethod.GET;

loader.addEventListener(Event.COMPLETE, onLoaded);
loader.load(req);

var txt:TextField = TextField(addChild(new TextField()));
txt.defaultTextFormat = new TextFormat("_sans", 12);
with (txt){ x=10, y=10, autoSize="left"; }
txt.htmlText = "loading...";

function onLoaded(evt:Event):void{
       removeChild(txt);
   var searchData:XML = new XML(loader.data);
   var atom:Namespace = searchData.namespace("");

   var leng:int = searchData.atom::entry.length() -1;
  for (var i:int = leng; i>=0; i--){
       var cmd:String =
searchData.atom::entry[i].atom::title.toString().replace(/#asgraph/g,
"");
      // added this to ignore words starting with @
      cmd = cmd.replace(/@(w+)/g, "");
       cmds.push(cmd);
   }

       var time:Timer = new Timer(100, cmds.length);
       time.addEventListener(TimerEvent.TIMER, onTick);
   time.start();
}
function onTick(evt:TimerEvent):void{
       render(parseFunctions(cmds[evt.target.currentCount - 1]));
       graphics.endFill();
       graphics.lineStyle();
}

// parse and run Graphics class commands
function parseFunctions(dat:String):Array{
   var a:Array = dat.split(";") ;
   for (var i:int = 0; i<a.length-1; i++){
       a[i] = a[i].split(/()|(|)/g);
       var f:String = a[i][0] = a[i][0].replace(/s/g,"");
       a[i] = a[i].splice(0, a[i].length - 1);
       if (a[i].length> 1){
        a[i] = a[i][1].split(",");
        a[i].unshift(f);
       }
   }
   return a.splice(0,a.length - 1);
}
function render(p:Array):void {
   for (var i:int = 0; i<p.length; i++) {
       try{
       graphics[p[i][0]].apply(graphics,p[i].splice(1));
       }catch(e:Error){};
   }
}

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):

[SWF]https://actionsnippet.com/wp-content/asgraph.swf, 700, 700[/SWF]

Here is a direct link to the swf

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *