BlurFilter( with other site's image data)

by 883108
♥0 | Line 63 | Modified 2010-03-05 04:30:46 | MIT License
play

ActionScript3 source code

/**
 * Copyright 883108 ( http://wonderfl.net/user/883108 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kV1L
 */

package{
	import flash.display.*;
	import flash.net.*;
	import flash.system.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.filters.*;

	public class BluFilterDocument extends Sprite{
		private var _url:String = '';
		private var _loader:Loader;
		
		public function BluFilterDocument(){
			init();
		}
		
		private function init():void{
			var tex:TextField = new TextField  ;
			addChild(tex);
			tex.width = 450;
			tex.height = 400;
			tex.x = tex.y = 10;
			tex.multiline = true;
			tex.text = 
				'読み込んだ外部画像にぼかしフィルタを適用します。\n\n\n\n\n\n\n\n\n' +
				'画像の読み込み中です....';
			// 画像のロード
			var image_url:String = 'http://farm5.static.flickr.com/4004/4406799106_b9231cfcf5.jpg';
			/* -----------------------------------------------------------------------------
				本来、外部ドメインからロードしたメディアに対して、ピクセルデータやオーディオデータに直接アクセスすることは許可されない。
				ただし、flickrのように、画像が置いてあるドメインのcrossdomain.xmlで、アクセスが許可されている場合は、
				データとしてアクセスできる。この場合は、LoaderContext の checkPolicyFile を true にしてインスタンス化し、
				loader.loadのオプションとして指定しておけば良い。
				詳細はてっく煮ブログのこちらのエントリーを参照
				-> http://d.hatena.ne.jp/nitoyon/20071112/crossdomain_img 
			----------------------------------------------------------------------------- */
			var context:LoaderContext = new LoaderContext(true);
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			_loader.load(new URLRequest(image_url), context);
		}
		
		private function completeHandler($event:Event):void{
			var bitmapData:BitmapData = Bitmap(_loader.content).bitmapData;
			var bitMap:Bitmap = new Bitmap(bitmapData);
			addChild(bitMap);

			addChild(create_button('ぼかし実行', clickHandle1));
			function clickHandle1($event:MouseEvent):void{
				var blur:BlurFilter = new BlurFilter(5, 5, 3);
				var filters:Array = [];
				filters.push(blur);
				bitMap.filters = filters;
			}
		}
		
		
		private function create_button($label:String, $clickHandler:Function):Sprite{
			// ボタンを生成します。
			var button:Sprite = new Sprite;
			addChild(button);
			button.graphics.beginFill(0x880000);
			button.graphics.drawRoundRect(0, 0, 100, 20, 5);
			button.graphics.beginFill(0xeeeeee);
			button.graphics.drawRoundRect(1, 1, 98, 18, 5);
			button.buttonMode = true;
			button.addEventListener(MouseEvent.CLICK, $clickHandler);
			// ボタンのラベルを生成します。
			var label:TextField = new TextField();
			button.addChild(label);
			label.x = 5;
			label.y = 3;
			label.text = $label;
			label.width = 100;
			label.height = 16;
			label.selectable = false;
			//
			return button;
		}

	}
}