Tag Archives: actionscript

Interesting Texture #2

Actionscript:
  1. var canvas:BitmapData = new BitmapData(1000,1000,false, 0x000000);
  2. addChild(new Bitmap(canvas));
  3. scaleX = scaleY = .5;
  4.  
  5. var w:int = canvas.width;
  6. var w10:int = w * 40;
  7. var size:int = canvas.width * canvas.height;
  8. for (var i:int = 0; i<size; i++){
  9.     var xp:int = i % w;
  10.      
  11.     var yp:int = int(i / w);
  12.     var c1:int = (255/8) * (Math.cos(xp* Math.PI/180 * 2) + Math.sin(yp*(xp + 200)/w10));
  13.     if (c1 <0) c1 = 256 - c1;
  14.     c1 = (c1 <<1 | c1) ;
  15.     canvas.setPixel(xp, yp, c1 <<15 | c1 <<7 | c1 );
  16. }
  17. var m:Matrix = new Matrix();
  18. m.scale(1,-1);
  19. m.translate(0,canvas.height);
  20. var clone:BitmapData = canvas.clone();
  21. canvas.draw(clone, m, null, BlendMode.SUBTRACT);
  22. clone.draw(canvas);
  23. clone.applyFilter(clone, clone.rect, new Point(0,0), new BlurFilter(10,10,1));
  24. canvas.draw(clone, null, null, BlendMode.ADD);

Randomly bit shifting on this one is actually getting a good deal of millage.. I know I can easily optimize so that it works in realtime - could even be done in pixelbender... but going to play a bit more first before I optimize....

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

Gumdrop Torus

Actionscript:
  1. var matrix:Matrix3D = new Matrix3D();
  2.  
  3. var verts:Vector.<Number> = new Vector.<Number>();
  4. var pVerts:Vector.<Number> = new Vector.<Number>();
  5. var uvts:Vector.<Number> = new Vector.<Number>();
  6.  
  7. for (var i:Number = -2; i<2; i+=.04){
  8.     for (var j:Number = -2; j<2; j+=.04){
  9.         for (var k:Number = -2; k<2; k+=.04){
  10. // equation from: http://local.wasp.uwa.edu.au/~pbourke/geometry/gumdrop/
  11.             var yz:Number = j * j + k * k;
  12.              var s:Number = 4 * (Math.pow(i,4) + Math.pow(yz,2))
  13.              + 17 * i * i *(yz) - 20 * (yz + i * i) + 17;
  14.             if (s <0 && s> -0.5){
  15.                 verts.push(i * 60);
  16.                 verts.push(j * 60);
  17.                 verts.push(k * 60);
  18.                 pVerts.push(0), pVerts.push(0);
  19.                 uvts.push(0), uvts.push(0), uvts.push(0);
  20.              }
  21.         }
  22.     }
  23. }
  24.  
  25. var tVerts:Vector.<Number> = new Vector.<Number>();
  26. matrix.appendRotation(90, Vector3D.X_AXIS);
  27. matrix.appendRotation(45, Vector3D.Y_AXIS);
  28. matrix.appendScale(1.7, 1.7, 1.7);
  29. matrix.transformVectors(verts, tVerts);
  30.  
  31. var p:Point = new Point();
  32. var brush:BitmapData=new BitmapData(3,3,true,0x41FFFFFF);
  33. var canvas:BitmapData = new BitmapData(500,500,false, 0x000000);
  34. addChild(new Bitmap(canvas));
  35. var dx:Number=0;
  36. var dy:Number=0;
  37. addEventListener(Event.ENTER_FRAME, onLoop);
  38. function onLoop(evt:Event):void {
  39.     dx += (mouseX - dx)/4;
  40.     dy += (mouseY - dy)/4;
  41.     matrix.identity();
  42.     matrix.appendRotation(dy,Vector3D.X_AXIS);
  43.     matrix.appendRotation(dx,Vector3D.Y_AXIS);
  44.     matrix.appendTranslation(250, 250, 0);
  45.     Utils3D.projectVectors(matrix, tVerts, pVerts, uvts);
  46.     canvas.lock();
  47.     canvas.fillRect(canvas.rect, 0x000000);
  48.     var inc:int = 0;
  49.     for (var i:int = 0; i<pVerts.length; i+=2){
  50.      
  51.         p.x = pVerts[i];
  52.         p.y = pVerts[i+1];
  53.         canvas.copyPixels(brush, brush.rect, p, null, null, true);
  54.     }
  55.     canvas.unlock();
  56. }

Felt like revisiting implicit surface plotting today...

Have a look at the swf over at wonderfl.net

Posted in 3D, BitmapData, Math | Also tagged , | Leave a comment

Convert Timeline Code to Document Class

For a few different reasons I decided to write some php code to convert timeline code to a document class. The script is done and it seems to work pretty nicely. I'm sure there are a few bugs, but I've tested around 30 snippets from this site and they all worked.

To test it out:

1) just take the below code (or any other timeline code), copy it to your clipboard
(The code doesn't have to be small, I did a test with 300 lines of timeline code and it worked just fine)

x = stage.stageWidth / 2;
y = stage.stageHeight / 2;
var xp:Number = 0, yp:Number = 0;
var r:Number = 0, t:Number = 0;
var speed:Number = .07;
var scale:Number = 20;
var plot0:Shape = Shape(addChild(new Shape()));
var plot1:Shape = Shape(addChild(new Shape()));
plot0.graphics.lineStyle(0,0x000000);
plot1.graphics.lineStyle(0,0x000000);
addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
	 r =  scale * Math.sqrt(t);
	 xp =  r * Math.cos(t);
	 yp =  r * Math.sin(t);
	 t += speed;
	plot0.graphics.lineTo(xp, yp);
	plot1.graphics.lineTo(-xp, -yp);
}

2) go to this page and follow the instructions:
http://actionsnippet.com/timeline_to_doc.php

I used this script to convert a few snippets from this site to work on wonderfl.net...

Have a look at them here.

In the next few days I may add a button to all code snippets that allows you to convert the code to a doc class...

The script adds import statements automatically, I may however have missed a few packages, so if the generated code is missing an import statement, let me know and I'll fix it...

Posted in misc | Also tagged , | 10 Comments

base32 Twitter Coordinates

Actionscript:
  1. [SWF(width = 800, height=700, frameRate=30, backgroundColor=0x000000)]
  2. var canvas:BitmapData = new BitmapData(1000,800,false, 0x000000);
  3. addChild(new Bitmap(canvas));
  4.  
  5. var coords:String ="";
  6. var index:int = 0;
  7. addEventListener(Event.ENTER_FRAME, onLoop);
  8.  
  9. var s:Shape = new Shape();
  10. with (s.graphics) beginFill(0xFFFFFF, 0.3), drawCircle(4,4,4)
  11. var brush:BitmapData = new BitmapData(10,10, true, 0x00000000);
  12. brush.draw(s);
  13.  
  14. var pnt:Point = new Point();
  15. function onLoop(evt:Event):void {
  16.      if (coords.length> 0){
  17.          for (var i:int = 0; i<100;i++){
  18.              if (index <coords.length){
  19.                  pnt.x+= 0.2;
  20.                  pnt.y = parseInt(coords.substr(index,3), 32) / 2;
  21.                  index+=3;
  22.                  canvas.copyPixels(brush, brush.rect, pnt, null, null, true);
  23.              }else{
  24.                 break;
  25.                 removeEventListener(Event.ENTER_FRAME, onLoop);
  26.              }
  27.          }
  28.      }
  29. }
  30.  
  31. var loader:URLLoader = new URLLoader();
  32. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  33. var vars:URLVariables = new URLVariables();
  34. vars.q = "as3, flash";
  35. vars.rpp = "100";
  36. vars.page = 1;
  37. vars.lang = "en";
  38. req.data = vars;
  39. req.method = URLRequestMethod.POST;
  40. loader.load(req);
  41. loader.addEventListener(Event.COMPLETE, onLoaded);
  42. function onLoaded(evt:Event):void{
  43.     var searchData:XML = new XML(loader.data);
  44.     var atom:Namespace = searchData.namespace("");
  45.     default xml namespace = atom;
  46.     var titles:String = "";
  47.     for each(var entry:XML in searchData.entry){
  48.         titles += entry.title.toString();
  49.     }
  50.     var index:int = 0;
  51.     var words:Array = titles.split(" ");
  52.     words.sort();
  53.     for (var i:int = 0; i<words.length; i++){
  54.         if (words[i].match(/RT|\@|\#|http/g).length == 0){
  55.             var word:String = words[i].replace(/[\W]/g, "");
  56.             if (word.length> 1){
  57.                 coords += word + " ";
  58.             }
  59.         }
  60.     }
  61. }

This snippet does a twitter search and sorts the results alphabetically. It then reads through the results 3 characters at a time - each set of 3 characters is converted to base32 and then used a y coordinate on a plot.

In order for the swf to be worth looking at, I think I'd need to add an input field. I may do that later... but for now here are a few stills:

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