Tag Archives: flash

Hidden Sierpiński

Actionscript:
  1. var canvas:BitmapData=new BitmapData(255,255,false,0x000000);
  2. addChild(new Bitmap(canvas, "auto", true));
  3.  
  4. scaleX = scaleY = 1.5;
  5.  
  6. addEventListener(Event.ENTER_FRAME, onLoop);
  7. function onLoop(evt:Event):void {
  8.     for (var i:int  = 0; i<canvas.width * canvas.height; i++) {
  9.         var ox:int= i % canvas.width;
  10.         var oy:int= i / canvas.width;
  11.         var col =  (ox | oy) * mouseX % 255;
  12.         canvas.setPixel(ox, oy, col <<16 | col <<8 | col);
  13.     }
  14. }

Sometimes when I'm writing a program that does some pixel pushing I'll save the file and then start arbitrarily adding bitwise operations to it.... just to see what happens. Sierpinski-esque stuff occurs often. This will work in your timeline - move your mouse left and right to generate images like these:





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

Texture cos(atan2())

Actionscript:
  1. var col:int, i:int, j:int, s:int = 500, div:Number =20, outcoord:Point = new Point(), points:Vector.<Point> = new Vector.<Point>();
  2. for (i = 0; i<5; i++) points.push(new Point(int(Math.random()*s),int(Math.random()*s)));
  3. var canvas:Bitmap = Bitmap(addChild(new Bitmap(new BitmapData(s,s, false, 0xFF0000), "auto", true)));
  4. for (i  = 0; i<canvas.width * canvas.height; i++){
  5.     outcoord= new Point( i % canvas.width, i / canvas.width);
  6.     col = 0;
  7.     for (j= 0; j<points.length; j++) col += Math.max(0,255 * Math.cos(Math.atan2(outcoord.y - points[j].y, outcoord.x - points[j].x)*outcoord.x/div) );
  8.     col /= points.length;
  9.     canvas.bitmapData.setPixel(outcoord.x, outcoord.y,  col <<16 |  col <<8 |  col);
  10. }

This is inspired by some stuff I've been doing with PixelBender. I think I'm going to convert this to processing to see how fast I can get it to run... will post that over at shapevent. People have been asking me to post swfs.... and I plan on starting to that a little bit... but since this one doesn't animate, here are a few jpgs:

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

25 Lines Contest

Actionscript:
  1. for (var i:int = 1; i<26; i++) with(addChild(new TextField())) x = 10, y = i * 10, text = "line" + i;

code will create 25 TextFields that say "line1", "line2", "line3" etc...

If you don't already know, Keith Peters has started up the 25 line actionscript contest again... The first round of results are in and you can view the 12 finalists swfs and code here.

I was particularly impressed with this one...by Cay Garrido... when I looked at it I really couldn't figure out how that kind of seemingly complex flocking could be done with 25 lines. You can see the code here.

Inspired by the entries, Peters created a strange attractor in 6 lines.

I can't wait to see next months entries.

Posted in misc | Also tagged | Leave a comment

Yahoo Weather RSS

Actionscript:
  1. var temp:Number;
  2. var weatherData:XML;
  3. var yweather:Namespace;
  4.  
  5. var zipcode:String="11211";
  6. var units:String = "f";  // "f" or "c" for Fahrenheit or Celsius
  7.  
  8. var yahooURL:String = "http://weather.yahooapis.com/forecastrss?p=" + zipcode + "&u=" + units;
  9.  
  10. var yahooWeather:URLLoader = new URLLoader();
  11.  
  12. yahooWeather.load(new URLRequest(yahooURL));
  13. yahooWeather.addEventListener(Event.COMPLETE, onLoaded);
  14.  
  15. function onLoaded(evt:Event):void {
  16.  
  17.     weatherData=new XML(evt.target.data);
  18.     yweather = weatherData.namespace("yweather");
  19.    
  20.     temp = weatherData.channel.item.yweather::condition.@temp;
  21.      
  22.     trace(temp);
  23. }

This snippet comes from a student question about how to get the temperature from yahoo weather.

You could use this snippet to get lots of additional information based on a zipcode... like latitude, longitude, humidity, wind chill etc... The trickiest part of this code is the Namespace stuff... which I find generally annoying... probably just because I'm not used to it.

You can read some detailed info about the content contained in the RSS file here.

Posted in XML, external data | Also tagged , , , , , | Leave a comment