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

forked from: fladdict challenge for professionals (stage of 'Far Away')

Theme:
* Play with BitmapPatterBuilder.
* Purpose of this trial is to find the possibility of the dot pattern.
*
* by Takayuki Fukatsu aka fladdict

simulating the stage of
* Hatsune Miku - Project DIVA - Far Away PV
/**
 * Copyright civet ( http://wonderfl.net/user/civet )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/doXS
 */

// forked from checkmate's fladdict challenge for professionals
/**
 * Theme:
 * Play with BitmapPatterBuilder.
 * Purpose of this trial is to find the possibility of the dot pattern.
 *
 * by Takayuki Fukatsu aka fladdict
 **/
 
/**
 * simulating the stage of
 * Hatsune Miku - Project DIVA - Far Away PV
 */
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.SpreadMethod;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Vector3D;
    import flash.utils.*;
    
    [SWF(width="480", height="480", backgroundColor="0x000000", frameRate="30")]
    
    public class Professional extends Sprite {
        public function Professional() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            init();
        }
        
        private static const WIDTH:int = 480;
        private static const HEIGHT:int = 480;
        private var container:Sprite;
        private var block:Shape;
        private var mask0:BitmapData;
        private var mask1:BitmapData;
        private var source0:BitmapData;
        private var source1:BitmapData;
        private var point:Point = new Point(0, 0);
        private var matrix0:Matrix = new Matrix();
        private var matrix1:Matrix = new Matrix();
        
        private function init():void
        {    
            //Container
            container = new Sprite;
            addChild(container);
            
            //Block
            block = new Shape();
            var colors:Array = [0xFF3366, 0xFF66CC, 0x6633FF, 0x00CCFF, 0x99FF66, 0xFFFF00, 0xFF3300];
            var alphas:Array = [1, 1, 1, 1, 1, 1, 1];
            var ratios:Array = [0x00, 0x2A, 0x54, 0x80, 0xAA, 0xD4, 0xFF];
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(WIDTH, HEIGHT, Math.PI/2, 0, 0);
            var g:Graphics = block.graphics;
            g.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.REPEAT);  
            g.drawRect(0, 0, WIDTH, HEIGHT * 2);
            g.endFill();
            
            //Star
            var star:Shape = new Shape();
            mask0 = new BitmapData(block.width, block.height, true, 0);
            mask1 = new BitmapData(block.width, block.height, true, 0);
            
            g = star.graphics;
            g.clear();
            g.beginBitmapFill( sample0(), new Matrix(32, 0, 0, 32, 0, 0) );
            drawStar(g, 240, 240, 5, 100, 240, 90);
            g.endFill();
            
            mask0.draw(star);
        
            g = star.graphics;
            g.clear();
            g.beginBitmapFill( sample1(), new Matrix(32, 0, 0, 32, 0, 0) );
            drawStar(g, 240, 240, 5, 100, 240, 90);
            g.endFill();
            
            mask1.draw(star);
            
            //Display
            source0 = new BitmapData(block.width, block.height, true, 0);
            source1 = new BitmapData(block.width, block.height, true, 0);
            
            source0.draw(block, matrix0);
            source0.copyChannel(mask0, mask0.rect, point, BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA); 
            matrix1.ty = -64;
            source1.draw(block, matrix1);
            source1.copyChannel(mask1, mask1.rect, point, BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
            
            container.addChild( new Bitmap(source0) );
            container.addChild( new Bitmap(source1) );
            
            container.z = 1;
            container.transform.matrix3D.appendTranslation(-240,-240,0);
            container.transform.matrix3D.appendRotation(45, Vector3D.Z_AXIS);
            container.transform.matrix3D.appendRotation(-60, Vector3D.X_AXIS);
            container.transform.matrix3D.appendTranslation(240,240,0);
            
            container.filters = [new DropShadowFilter(8, 45, 0x336666, 2, 0, 0)];
            
            stage.addEventListener(Event.ENTER_FRAME, enterFrame);        
        }
        
        private var t:int = 0;
        private var count:int = 0;
        private function enterFrame(e:Event):void
        {            
            if( (getTimer() - t) < 500 ) return;
            t = getTimer(); 
            
            matrix0.ty = count;
            matrix1.ty = count-64;
            source0.draw(block, matrix0);
            source0.copyChannel(mask0, mask0.rect, point, BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
            source1.draw(block, matrix1);
            source1.copyChannel(mask1, mask1.rect, point, BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
        
            count -= 32;
            if(count < -480) count = -32;
        }
        
        //pattern 0
        public function sample0():BitmapData {
            return BitmapPatternBuilder.build(
                [[0,1],
                 [1,0]],
                [0xff000000, 0x00ffffff]
            );
        }
        
        //pattern 1
        public function sample1():BitmapData{
            return BitmapPatternBuilder.build(
                [[1,0],
                 [0,1]],
                [0xff000000, 0x00ffffff]
            );
        }   
        
        /**
         * based on mc.drawStar() - by Ric Ewing (ric@formequalsfunction.com) - version 1.4 - 4.7.2002
         */
        public function drawStar(target:Graphics, x:Number, y:Number, 
        points:int, innerRadius:Number, outerRadius:Number, angle:Number=0):void
        {
            var count:int = Math.abs(points);
            if (count > 2) {
                // init vars
                var step:Number, halfStep:Number, start:Number, dx:Number, dy:Number;
                // calculate distance between points
                step = (Math.PI*2)/points;
                halfStep = step/2;
                // calculate starting angle in radians
                start = (angle/180)*Math.PI;
                target.moveTo(x+(Math.cos(start)*outerRadius), y-(Math.sin(start)*outerRadius));
                // draw lines
                for (var n:int=1; n<=count; n++) {
                    dx = x+Math.cos(start+(step*n)-halfStep)*innerRadius;
                    dy = y-Math.sin(start+(step*n)-halfStep)*innerRadius;
                    target.lineTo(dx, dy);
                    dx = x+Math.cos(start+(step*n))*outerRadius;
                    dy = y-Math.sin(start+(step*n))*outerRadius;
                    target.lineTo(dx, dy);
                }
            }
        }
    }
}

/**-----------------------------------------------------
 * 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();        
    }
}