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

Resolutions Pattern

Theme:
* Play with BitmapPatterBuilder.
* Purpose of this trial is to find the possibility of the dot pattern.
*
* by Takayuki Fukatsu aka fladdict
/**
 * Copyright alumican_net ( http://wonderfl.net/user/alumican_net )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/xs1w
 */

/**
 * Theme:
 * Play with BitmapPatterBuilder.
 * Purpose of this trial is to find the possibility of the dot pattern.
 *
 * by Takayuki Fukatsu aka fladdict
 **/
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import flash.filters.ConvolutionFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.utils.getTimer;
    
    public class ResolutionsPattern extends Sprite
    {
        private const W:uint = 465;
        private const H:uint = 465;
        
        private const SCALE:uint = 1;
        
        private const ZEROS:Point = new Point(0, 0);
        
        //front pattern
        private var _fpat:BitmapData;
        
        private var _canvas:Sprite;
        
        private var _ctf:ColorTransform = new ColorTransform(0.95, 0.95, 0.99);
        private var _blur:BlurFilter = new BlurFilter(2, 2, 1);
        
        private var _rad:Number = 0;
        private var _dir:Number = 1;
        
        public function ResolutionsPattern()
        {
            Wonderfl.capture_delay( 10 );
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            //init canvas
            _canvas = addChild( new Sprite() ) as Sprite;
            _canvas.graphics.beginFill(0x0);
            _canvas.graphics.drawRect(0, 0, W, H);
            _canvas.graphics.endFill();
            
            //update pattern
            addEventListener(Event.ENTER_FRAME, _update);
        }
        
        //update pattern
        private function _update(e:Event):void 
        {
            var div:uint = 3 * (Math.sin(_rad += 0.05) + 1) + 2;
            
            _updatePattern(div);
            
            _draw();
        }
        
        private function _draw():void
        {
            var g:Graphics = _canvas.graphics;
            g.beginBitmapFill(_fpat);
            g.drawRect(0, 0, W, H);
            g.endFill();
        }
        
        private function _updatePattern(division:uint):void
        {
            //calculate pattern size
            var size:uint = Math.round(W / division / SCALE);
            
            var old:BitmapData;
            if (!_fpat)
            {
                _fpat = new BitmapData(size * SCALE, size * SCALE, true, 0x00000000);
                old = _fpat.clone();
            }
            else
            {
                old = _fpat.clone();
                _fpat = new BitmapData(size * SCALE, size * SCALE, true, 0x00000000);
            }
            
            
            if (mouseX < 0 || mouseX >= W || mouseY < 0 || mouseY >= H) return;
            
            //create blank pattern
            var pat:Array = new Array(size);
            for (var i:uint = 0; i < size; ++i) 
            {
                pat[i] = new Array(size);
                for (var j:uint = 0; j < size; ++j) 
                {
                    pat[i][j] = 0;
                }
            }
            
            //draw pattern by mouse position
            var px:uint = mouseX / W * size;
            var py:uint = mouseY / H * size;
            
                                pat[py    ][px    ] = 1;
        //    if (py <  size - 1) pat[py + 1][px    ] = 1;
        //    if (px <  size - 1) pat[py    ][px + 1] = 1;
        //    if (py >= 1    )    pat[py - 1][px    ] = 1;
        //    if (px >= 1    )    pat[py    ][px - 1] = 1;
            
            //define colors
            var colors:Array = [0x00000000, 0xffffffff];
            
            //generate new pattern
            var cur:BitmapData = BitmapPatternBuilder.build(pat, colors);
            
            //convine
            var ratio:Number = size / _fpat.width;
            _fpat.draw(old, new Matrix(ratio, 0, 0, ratio));
            
            _fpat.colorTransform(_fpat.rect, _ctf);
            _fpat.draw(cur, new Matrix(SCALE, 0, 0, SCALE), null, "add");
            _fpat.applyFilter(_fpat, _fpat.rect, ZEROS, _blur);
        }
    }
}





/**-----------------------------------------------------
 * Use following BitmapPatternBuilder class 
 * 
 * DO NOT CHANGE any codes below this comment.
 *
 * -----------------------------------------------------
*/
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
    
class BitmapPatternBuilder{
    
    /**
     * creates BitmapData filled with dot pattern.
     * First parameter is 2d array that contains color index for each pixels;
     * Second parameter contains color reference table.
     *
     * @parameter pattern:Array 2d array that contains color index for each pixel.
     * @parameter colors:Array 1d array that contains color table.
     * @returns BitmapData
     */
    public static function build(pattern:Array, colors:Array):BitmapData{
        var bitmapW:int = pattern[0].length;
        var bitmapH:int = pattern.length;
        var bmd:BitmapData = new BitmapData(bitmapW,bitmapH,true,0x000000);
        for(var yy:int=0; yy<bitmapH; yy++){
            for(var xx:int=0; xx<bitmapW; xx++){
                var color:int = colors[pattern[yy][xx]];
                bmd.setPixel32(xx, yy, color);
            }
        }
        return bmd;
    }
    
    /**
     * short cut function for Graphics.beginBitmapFill with pattern.
     */
    public static function beginBitmapFill(pattern:Array, colors:Array, graphics:Graphics):void{
        var bmd:BitmapData = build(pattern, colors);
        graphics.beginBitmapFill(bmd);
        bmd.dispose();        
    }
}