In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 点描風

軽量・高速化。
うまく合成するBlendModeがなかったのでアルファはなくして上書き。
Get Adobe Flash player
by uwi 10 Mar 2010
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9bl6
 */

// forked from moringo2's 点描風
// 軽量・高速化。
// うまく合成するBlendModeがなかったのでアルファはなくして上書き。
package{
	
	//コードは書き殴った感じで、きれいではないですけど。。
	import flash.display.*;
	import flash.net.*
	import flash.events.Event;
	import flash.geom.*;
	import flash.system.LoaderContext;
	
	public class IMTest extends Sprite{
		
		private const imgPaths:Array = ["http://moringo.moo.jp/out/b01.jpg","http://moringo.moo.jp/out/b02.jpg",
										"http://moringo.moo.jp/out/b03.jpg","http://moringo.moo.jp/out/b04.jpg",
										"http://moringo.moo.jp/out/b05.jpg"];
		
		
		private var canvas:Bitmap;
		private var bm:Bitmap;
		private var bmd:BitmapData;
		private var counter:int = 0;
		private var loader:Loader;
		
		private var circles : Array;
		
		public function IMTest(){
			init();			
			circles = [null];
			var s : Shape = new Shape();
			for(var i : uint = 1;i <= 6;i++){
				var BMD : BitmapData = new BitmapData(2*i, 2*i, true, 0x00000000);
				var g : Graphics = s.graphics;
				g.clear();
				g.lineStyle(0, 0xffffff, 0);
				g.beginFill(0xffffff);
				g.drawCircle(i, i, i);
				g.endFill();
				BMD.draw(s);
				circles.push(BMD);
			}
		}
		
		private function init() : void{
			counter = 0;
			loader = new Loader(); 
			loader.load(new URLRequest(imgPaths[Math.floor(Math.random()*5)]),new LoaderContext(true));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,comp);
		}
		
		private function comp(e:Event) : void{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,comp);
			bm = loader.content as Bitmap;
			bmd = bm.bitmapData;
			addEventListener(Event.ENTER_FRAME,drawCircle);
			
			var cbmd : BitmapData = new BitmapData(bmd.width, bmd.height, false, 0x000000);
			canvas = new Bitmap(cbmd);
			addChild(canvas);
		}
		
		private function drawCircle(e:Event) : void{
			var range : uint = 2;
			var rr : uint = 2 * range + 1;
			canvas.bitmapData.lock();
			for(var i:int=0; i<40; i++){
				var randX:int = Math.random() * bmd.width;
				var randY:int = Math.random() * bmd.height;
				
				// 平均色を計算。intersectionをとっておかないとクラッシュする。
				var hist : Vector.<Vector.<Number>> = bmd.histogram(new Rectangle(randX - range, randY - range, rr, rr).intersection(bmd.rect));
				var avgColor : uint = 0;
				for(var k : uint = 0;k <= 2;k++){
					var vec : Vector.<Number> = hist[k];
					var sum : Number = 0;
					var num : uint = 0;
					for(var j : uint = 0;j < vec.length;j++){
						sum += j * vec[j];
						num += vec[j];
					}
					if(num == 0)num = 1;
					avgColor = avgColor << 8 | uint(sum / num);
				}
				var radius : int = Math.random() * 5 + 1;
				var cbmd : BitmapData = circles[radius].clone();
				cbmd.threshold(cbmd, cbmd.rect, new Point(), "!=", 0, 0xff000000 | avgColor);
				
				canvas.bitmapData.copyPixels(cbmd, cbmd.rect, new Point(randX - radius, randY - radius));
				cbmd.dispose();
			}
			canvas.bitmapData.unlock();
			counter++;
			if(counter >= 350){
				removeEventListener(Event.ENTER_FRAME,drawCircle);
				loader = null;
				removeChild(canvas);
				canvas = null;
				bm = null;
				bmd = null;
				init();
			}else{
				
			}
		}
	}

}