グラデーションマスク・アルファチャンネルマスクのテスト

by tkinjo
♥3 | Line 44 | Modified 2009-05-21 22:07:07 | MIT License
play

ActionScript3 source code

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

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="465" height="465" 
	paddingLeft="0" paddingTop="0" paddingRight="0" paddingBottom="0" applicationComplete="applicationCompleteHandler( event )">
	
	<mx:Script>
		<![CDATA[
			
			import flash.display.Bitmap;
			import flash.display.BitmapData;
			import flash.display.GradientType;
			import flash.display.Sprite;
			
			/**
			 * グラデーションマスク・アルファチャンネルマスクのテスト
			 * 
			 * 参考
			 * にゃあプロジェクト - グラデーション・マスク
			 * http://www.project-nya.jp/modules/weblog/details.php?blog_id=462
			 * 
			 * Flash CS3 ドキュメンテーション - アルファチャンネルマスクについて
			 * http://livedocs.adobe.com/flash/9.0_jp/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000873.html
			 * 
			 * @param	event
			 */
			private function applicationCompleteHandler( event:Event ):void {
				
				// 本体の生成
				var noiseBitmapData:BitmapData = new BitmapData( 100, 100 );
				noiseBitmapData.noise( 0 );
				
				var noiseBitmap:Bitmap = new Bitmap( noiseBitmapData );
				noiseBitmap.x = ( stage.stageWidth - noiseBitmap.width ) / 2;
				noiseBitmap.y = ( stage.stageHeight - noiseBitmap.height ) / 2 - noiseBitmap.height / 2;
				
				// 反射用の Bitmap
				var reflectionNoiseBitmap:Bitmap = new Bitmap( noiseBitmapData );
				reflectionNoiseBitmap.x = ( stage.stageWidth - reflectionNoiseBitmap.width ) / 2;
				reflectionNoiseBitmap.y = ( stage.stageHeight - reflectionNoiseBitmap.height ) / 2 + reflectionNoiseBitmap.height * 3 / 2 + 10;
				reflectionNoiseBitmap.scaleY = -1;
				reflectionNoiseBitmap.cacheAsBitmap = true;
				
				// グラデーションマスクの作成
				var gradiationMask:Sprite = new Sprite();
				
				var gradiationMaskMatrix:Matrix = new Matrix();
				gradiationMaskMatrix.createGradientBox( reflectionNoiseBitmap.width, reflectionNoiseBitmap.height / 2, Math.PI / 2, 0, 0 );
				
				gradiationMask.graphics.beginGradientFill( GradientType.LINEAR, [ 0, 0 ], [ 1, 0 ], [ 0, 255 ], gradiationMaskMatrix );
				gradiationMask.graphics.drawRect( 0, 0, reflectionNoiseBitmap.width, reflectionNoiseBitmap.height / 2 );
				gradiationMask.graphics.endFill();
				gradiationMask.alpha = 0.5;
				
				gradiationMask.x = ( stage.stageWidth - gradiationMask.width ) / 2;
				gradiationMask.y = ( stage.stageHeight - gradiationMask.height ) / 2 + gradiationMask.height / 2 + 10;
				gradiationMask.cacheAsBitmap = true;
				
				// bitmapContentHolder の子に登録
				bitmapContentHolder.addChild( noiseBitmap );
				bitmapContentHolder.addChild( reflectionNoiseBitmap );
				bitmapContentHolder.addChild( gradiationMask );
				
				// マスク設定
				reflectionNoiseBitmap.mask = gradiationMask;
			}
			
		]]>
	</mx:Script>
	
	<mx:Canvas id="canvas" width="100%" height="100%">
		
		<mx:UIComponent id="bitmapContentHolder" />
		
		<mx:ColorPicker selectedColor="0x000000" x="10" y="10"
			creationComplete="canvas.setStyle( 'backgroundColor', ( event.currentTarget as ColorPicker ).selectedColor )" 
			change="canvas.setStyle( 'backgroundColor', ( event.currentTarget as ColorPicker ).selectedColor )" />
		
	</mx:Canvas>
</mx:Application>

Forked