Beautifl - Flash Gallery

Preview

画用紙にお絵かき
okoi 2012年2月8日 MIT License
?
perlinNoiseとかConvolutionFilterフィルタとか
      package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.filters.ConvolutionFilter;
    import flash.geom.Matrix;
    import flash.geom.Point;
    
    [SWF(width = "465", height = "465", frameRate = "60")]
    
    /**
     * ...
     * @author okoi
     */
    public class Main extends Sprite 
    {
        public static const WIDTH:int = 465;
        public static const HEIGHT:int = 465;
        
        private var paper:BitmapData;
        
        private var isDrawing:Boolean = false;
        private var _bx:Number = 0;
        private var _by:Number = 0;
        
        private var _line:Shape = new Shape();
        private var _tmp_canvas:BitmapData;
        
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            
            paper = new BitmapData(WIDTH, HEIGHT, true, 0);
            paper.perlinNoise( 2, 2, 1, int(Math.random() * 256), false, false, 1, true, [new Point()] );
            paper.applyFilter( paper, paper.rect, new Point(), new BlurFilter(2,2) );
            
            var filterMat:Array = [
                0.5, 0, 0,
                0, 0, 0,
                0, 0, -0.5
            ];
            var filter:ConvolutionFilter = new ConvolutionFilter( 3, 3, filterMat, 1, 200 );
            paper.applyFilter( paper, paper.rect, new Point(), filter );
            
            addChild( new Bitmap(paper) );
            
            _tmp_canvas = new BitmapData( WIDTH, HEIGHT, true, 0 );
            
            addEventListener( Event.ENTER_FRAME, _enterFrameHandler );
            stage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler );
            stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler );
            
        }
        
        private function _enterFrameHandler( e:Event ) : void {
            
            
            if ( isDrawing ) {
                //if ( !(Math.abs( _bx - stage.mouseX ) < 0.1 || Math.abs( _by - stage.mouseY) < 0.1 ) ) {
                    _line.graphics.clear();
                    _line.graphics.lineStyle( 20 + (Math.random() * 10 - 5), 0 );
                    _line.graphics.moveTo( _bx, _by );
                    _line.graphics.lineTo( stage.mouseX, stage.mouseY );
                    
                    _tmp_canvas.draw( _line );
                    _tmp_canvas..pixelDissolve( _tmp_canvas, _tmp_canvas.rect, new Point(), Math.random() * 5, WIDTH * HEIGHT * 0.3, 0x00000000 );
                    
                    paper.draw( _tmp_canvas );
                    _bx = stage.mouseX;
                    _by = stage.mouseY;
                //}
            }
        }
        
        private function _mouseDownHandler( e:MouseEvent ) : void {
            _bx = stage.mouseX;
            _by = stage.mouseY;
            isDrawing = true;
        }
        private function _mouseUpHandler( e:MouseEvent ) : void {
            isDrawing = false;
        }
    }
    
}