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

Pinta y Colorea!!

Pinta y colorea!! Vladimir Israel Ramírez Díaz - VLD
/**
 * Copyright vlad.el.rojo ( http://wonderfl.net/user/vlad.el.rojo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/bVYU
 */

// 
// Pinta y colorea!! Vladimir Israel Ramírez Díaz - VLD

package {
    import flash.display.Sprite;
    import flash.events.*;
 
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // 
            addChild(new GameMain());
        }
    }
}

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.net.*;
import flash.filters.*;

internal class GameMain extends Sprite{

/*/
    static public const GLOW_BLUR:int = 6;//2
    static public const FILTER_GLOW:BlurFilter = new BlurFilter(GLOW_BLUR, GLOW_BLUR);
//*/

    
    static public const POS_ZERO:Point = new Point(0,0);

    
    public var m_BitmapData_Nrm:BitmapData;

   
    public var m_BitmapData_View:BitmapData;

    
    public var m_BitmapData_Glow:BitmapData;

    
    public var m_Layer_Particle:Sprite = new Sprite();


    

    public function GameMain(){
        
        addEventListener(Event.ADDED_TO_STAGE, Init);
    }
    
    public function Init(e:Event = null):void{
        //
        {
            removeEventListener(Event.ADDED_TO_STAGE, Init);
        }

        var W:int = stage.stageWidth;
        var H:int = stage.stageHeight;

        //
        {
            m_BitmapData_Nrm  = new BitmapData(W, H, false, Vector_to_Color(new Vector3D(0,0,1)));
            m_BitmapData_View = new BitmapData(W, H, true, 0x00000000);
            m_BitmapData_Glow = new BitmapData(W, H, true, 0x00000000);

            addChild(new Bitmap(m_BitmapData_Nrm));
            addChild(new Bitmap(m_BitmapData_View));

            var bmp_glow:Bitmap = new Bitmap(m_BitmapData_Glow, PixelSnapping.NEVER, true);
            bmp_glow.blendMode = BlendMode.ADD;
            addChild(bmp_glow);
        }

        //
        {
            var draw_flag:Boolean = false;

            var mouseX_Old:int = 0;
            var mouseY_Old:int = 0;

            var shape:Shape = new Shape();
            var g:Graphics = shape.graphics;
            {
                shape.filters = [
                    new BlurFilter()
                ];
            }

            //
            var draw:Function = function(in_X:int, in_Y:int, in_OldX:int, in_OldY:int):void{
                //
                var vec:Vector3D;
                {
                    const BASE_Z:Number = 10.0;

                    vec = new Vector3D(
                        in_X - in_OldX,
                        -(in_Y - in_OldY),
                        BASE_Z
                    );

                    vec.normalize();
                }

                //
                var nrm_color:uint;
                {
                    nrm_color = Vector_to_Color(vec);
                }

                //
                {
                    const RAD:uint = 20;

                    g.clear();
                    g.lineStyle(RAD, nrm_color, 1.0);

                    g.moveTo(in_OldX, in_OldY);
                    g.lineTo(in_X, in_Y);

                    m_BitmapData_Nrm.draw(shape);
                }
            };

            //
            addEventListener(
                MouseEvent.MOUSE_DOWN,
                function(e:MouseEvent):void{
                    //
                    {
                        draw_flag = true;
                    }

                    //
                    {
                        mouseX_Old = mouseX;
                        mouseY_Old = mouseY;
                    }

                    //
                    {
                        draw(mouseX, mouseY, mouseX_Old, mouseY_Old);
                    }
                }
            );
            
            //
            stage.addEventListener(
                MouseEvent.MOUSE_MOVE,
                function(e:MouseEvent):void{
                    //
                    {
                        if(! draw_flag){
                            return;
                        }
                    }

                    //
                    {
                        draw(mouseX, mouseY, mouseX_Old, mouseY_Old);
                    }

                    //
                    {
                        mouseX_Old = mouseX;
                        mouseY_Old = mouseY;
                    }
                }
            );

            //
            stage.addEventListener(
                MouseEvent.MOUSE_UP,
                function(e:MouseEvent):void{
                    //
                    {
                        draw_flag = false;
                    }
                }
            );
        }

        //
        {
            const Num:int = 1000;

            addChild(m_Layer_Particle);
            for(var i:int = 0; i < Num; i++){
                m_Layer_Particle.addChild(new PointObject(W*Math.random(), H*Math.random(), m_BitmapData_View, m_BitmapData_Nrm));
            }
        }

        //
        {
            addEventListener(Event.ENTER_FRAME, Update);
        }
    }

    public function Update(e:Event):void{
        //
        m_BitmapData_View.fillRect(m_BitmapData_View.rect, 0x00000000);

        //
        var num:int = m_Layer_Particle.numChildren;
        for(var i:int = 0; i < num; i++){
            (m_Layer_Particle.getChildAt(i) as PointObject).Update();
        }

        //
        m_BitmapData_Glow.applyFilter(m_BitmapData_Glow, m_BitmapData_Glow.rect, POS_ZERO, FILTER_GLOW);
        m_BitmapData_Glow.draw(m_BitmapData_View);
    }

    //
    static public function Vector_to_Color(in_Nrm:Vector3D):uint{
        var color:uint;
        {
            var r:uint = 0xFE * (0.5 + 0.5*in_Nrm.x);
            var g:uint = 0xFE * (0.5 + 0.5*in_Nrm.y);
            var b:uint = 0xFE * (0.5 + 0.5*in_Nrm.z);

            color = (r << 16) | (g << 8) | (b << 0);
        }

        return color;
    }
}

class PointObject extends Sprite
{
    //
    static public const RAD:int = 5;
    static public const VEL:int = 10;

    static public const PARTICLE_COLOR:uint = 0xFFFFFFFF;

    //
    public var m_BitmapData_View:BitmapData;
    public var m_BitmapData_Nrm:BitmapData;
    public var m_Vel:Vector3D = new Vector3D(0, 0);

    //
    public function PointObject(in_X:int, in_Y:int, in_BitmapData_View:BitmapData, in_BitmapData_Nrm:BitmapData){
        //
        {
            this.x = in_X;
            this.y = in_Y;
        }

        //
        {
            m_BitmapData_View = in_BitmapData_View;
            m_BitmapData_Nrm  = in_BitmapData_Nrm;
        }

    }

    public function Update(e:Event=null):void{
        //法線マップから得られる速度
        var NrmColor:uint;
        var NrmVel:Vector3D;
        {
            NrmColor = m_BitmapData_Nrm.getPixel(this.x, this.y);
            NrmVel = Color_to_Vector(NrmColor);
            NrmVel.scaleBy(VEL);
        }

        var ratio:Number = 1 - NrmVel.z/VEL;//

        
        {
//            

            m_Vel.x = lerp(m_Vel.x, NrmVel.x, ratio);
            m_Vel.y = lerp(m_Vel.y, NrmVel.y, ratio);

            if(this.y > m_BitmapData_View.height*3/4){
                if(m_Vel.y > 0){m_Vel.y = 0;}
            }else{
                m_Vel.y += 0.1;
            }
        }

        //
        var OldX:int;
        var OldY:int;
        var NewX:int;
        var NewY:int;
        {
            OldX = this.x;
            OldY = this.y;

            this.x += m_Vel.x;
            this.y += m_Vel.y;

            NewX = this.x;
            NewY = this.y;

            if(this.x < 0){this.x += m_BitmapData_View.width;}
            if(this.x >= m_BitmapData_View.width){this.x -= m_BitmapData_View.width;}
            if(this.y < 0){this.y = 0;}
            if(this.y >= m_BitmapData_View.height){this.y -= m_BitmapData_View.height;}
        }

        
        {
            m_BitmapData_View.setPixel32(this.x, this.y, PARTICLE_COLOR);
           
        }
    }

    //
    static public function Color_to_Vector(in_Color:uint):Vector3D{
        var dir:Vector3D;
        {
            dir = new Vector3D(
                2 * ((in_Color >> 16) & 0xFF) / 254.0 - 1,
                -(2 * ((in_Color >>  8) & 0xFF) / 254.0 - 1),
                2 * ((in_Color >>  0) & 0xFF) / 254.0 - 1
            );
        }

        return dir;
    }

    public function lerp(in_Src:Number, in_Dst:Number, in_Ratio:Number):Number{
        return (in_Src * (1 - in_Ratio)) + (in_Dst * in_Ratio);
    }
}