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

Particle3D experimenting

Get Adobe Flash player
by bradsedito 18 Sep 2011
/**
 * Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/eDcA
 */

// forked from lizhi's flash on 2011-9-17
package  
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.geom.Point;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import com.greensock.easing.*;
    import com.greensock.*;
 
    [SWF(width=465,height=465,backgroundColor=0xffffff,frameRate=60)]
    
    public class Particle3D extends Sprite 
    {
                 
        public  var p:Particle = new Particle;
        public  var SW:int;
        public  var SH:int;
        public  var DEPTH:uint = new uint( 1000 );
        private var first:Particle;
        private var vbmp:BitmapData;
        private var canvas:Shape = new Shape;
        private var offsets:Array = [new Point,new Point];
        
        public function Particle3D() 
        {
            SW = stage.stageWidth;
            SH = stage.stageHeight;
            vbmp = new BitmapData(SW, SH, false, 0x123456);
            addChild(canvas);
            
            var c:int = 10;
            var last:Particle;

            while (c-->0) 
            {
                var p:Particle = new Particle;

                p.x  = SW * Math.random();
                p.y  = SH * Math.random();
                p.z  = DEPTH * Math.random();   
                
                p.vx = 0;
                p.vy = 0;

                if (first == null) first = p;
                else last.next = p;
                last = p;
            }
            addEventListener(Event.ENTER_FRAME, update);
        }
        
        private function update(e:Event):void 
        {
            offsets[0].x -= (mouseX - SW / 2)/10;
            offsets[1].y -= (mouseY - SH / 2)/10;
            vbmp.perlinNoise(100, 100, 2, 1, true, true, 7, true,offsets);
            var graphics:Graphics = canvas.graphics;
            var p:Particle = first;
            graphics.clear();
            
            while (p != null) 
            {
                var left:uint = vbmp.getPixel(p.x - 1, p.y)&0xff;
                var right:uint = vbmp.getPixel(p.x + 1, p.y) & 0xff;
                var top:uint = vbmp.getPixel(p.x, p.y-1)&0xff;
                var bottom:uint = vbmp.getPixel(p.x, p.y+1)&0xff;
                var s:Number = 0.02;
                p.vx += (right - left) * s;
                p.vy += (bottom - top) * s;
                p.vx *= 0.99;
                p.vy *= 0.99;
                p.x += p.vx;
                p.y += p.vy;
                var a:Number = Math.atan2(p.vy, p.vx);
                var x0:Number = p.x+3 * Math.cos(a);
                var y0:Number = p.y+3 * Math.sin(a);
                var x1:Number = p.x+3 * Math.cos(a+Math.PI+0.5);
                var y1:Number = p.y+3 * Math.sin(a+Math.PI+0.5);
                var x2:Number = p.x+3 * Math.cos(a+Math.PI-0.5);
                var y2:Number = p.y + 3 * Math.sin(a + Math.PI - 0.5);
                
                graphics.beginFill(0);
                graphics.moveTo(x0, y0);
                graphics.lineTo(x1, y1);
                graphics.lineTo(x2, y2);
                graphics.lineTo(x0, y0);
                graphics.endFill();

                if (p.x < 0  ||  p.y < 0  ||  p.x > SW  ||  p.y > SH) 
                {
                    p.vx = 0;
                    p.vy = 0;
                    p.x = SW * Math.random();
                    p.y = SH * Math.random();
                    p.z = DEPTH * Math.random()
                }
                p = p.next;
            }
        }
    }
}


class Particle 
{
    public var x:Number;
    public var y:Number;
    public var z:Number;
    public var vx:Number;
    public var vy:Number;
    public var next:Particle;
}