littlepad blog

BitmapクラスとBitmapDataクラスによるスプレー風描画

念を送ると顔が浮き出ます。

[swf]/blog/wp-content/uploads/2008/11/ColorSprayPaint.swf, 550, 400[/swf]

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    
    public class ColorSprayPaint extends Sprite {
        private var baseCanvas:BitmapData;
        private var splashCanvas:BitmapData;
        private var color:uint;
        private var sprayArea:Number = 300;     //スプレーするエリア半径
        private var spraySize:Number = 80;      //スプレーのサイズ
        private var sprayDensity:Number = 80;   //スプレー密度
        private var sprayNum:Number = 800;      //スプレー回数
        
        public function ColorSprayPaint() {
            init();
        }
        
        private function init():void {
            baseCanvas = new BitmapData(stage.stageWidth, stage.stageHeight, true);
            splashCanvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00FFFFFF);
            var baseBmp:Bitmap = new Bitmap(baseCanvas);        //顔の描画用キャンバス
            var splashBmp:Bitmap = new Bitmap(splashCanvas);    //まわりに飛び散らす用のキャンバス
            addChild(baseBmp);
            addChild(splashBmp);
            
            var face:Face = new Face();
            face.x = stage.stageWidth / 2;
            face.y = stage.stageHeight / 2;
            addChild(face);
            baseBmp.mask = face;    //マスクの設定
            
            var timer:Timer = new Timer(10, sprayNum);
            timer.addEventListener(TimerEvent.TIMER, onInterval, false, 0, true);
            timer.start();
        }
        
        private function onInterval(e:TimerEvent):void {
            color = Math.random() * 0xFFFFFF + 0xFF000000;
            var angle = Math.random() * Math.PI * 2;
            var radius:Number = Math.random() * sprayArea;
            spray(stage.stageWidth / 2 + Math.cos(angle) * radius, stage.stageHeight / 2 + Math.sin(angle) * radius);
        }
        
        private function spray(spX:Number,spY:Number):void {
            for (var i:int = 0; i < sprayDensity; i++ ) {
                var angle:Number = Math.random() * Math.PI * 2;
                var radius:Number = Math.random() * spraySize;
                var xpos:Number = spX + Math.cos(angle) * radius;
                var ypos:Number = spY + Math.sin(angle) * radius;
                baseCanvas.setPixel32(xpos, ypos, color);
                if (i % 10 == 0) {
                    splashCanvas.setPixel32(xpos, ypos, color);
                }
            }
        }
    }
}
モバイルバージョンを終了