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: FF: Gravity Particles

Click/hold mouse or use arrow keys to move particles.

I think it looks like an exploded bag of flour when you use the mouse :)
Get Adobe Flash player
by JZE 01 Sep 2011

    Talk

    bradsedito at 20 Sep 2011 05:23
    I like it! Nice forking.

    Tags

    Embed
/**
 * Copyright JZE ( http://wonderfl.net/user/JZE )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jDFL
 */

// forked from bradsedito's FF: Gravity Particles
// forked from JZE's Gravity Particles
package {
    import flash.filters.BlurFilter;
    import net.hires.debug.Stats;//
    import flash.geom.ColorTransform;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Point;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    

    [SWF(width=500,height=500,backgroundColor=0,frameRate=50)]

    public class particlesmain extends Sprite 
    {
        private var ps:Vector.<particle>;
        private var i:uint;
        private var curp:Point;
        private var c:BitmapData;
        private var bmp:Bitmap;
        private var btrans:ColorTransform;
        private var pxs:Vector.<uint>;
        private var down:Boolean;
        private var keys:Vector.<Boolean>;
        private var plen:uint;
        private var bfilt:BlurFilter;
        public function particlesmain() {
            keys = new Vector.<Boolean>(4, true);
//          btrans = new ColorTransform(0.9, 0.9, 0.9, 1.0);//the original
//          btrans = new ColorTransform(0.99, 0.8, 0.00, 1.0);//this filter is awesome, especially with the speed line on instead of the static color!
            btrans = new ColorTransform(0.99, 0.99, 0.99, 1.0);
            bfilt = new BlurFilter(2, 2, 3);
            plen = 7000;
            c = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
            bmp = new Bitmap(c);
            addChild(bmp);
            addChild(new Stats());//
            ps = new Vector.<particle>(plen, true);
            for(i = 0; i < plen; i++) {
                ps[i] = new particle(Math.round(Math.random()*stage.stageWidth), Math.round(Math.random()*stage.stageHeight));
            }
            addEventListener(Event.ENTER_FRAME, cycle);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mdo);
            stage.addEventListener(MouseEvent.MOUSE_UP, mup);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, kdo);
            stage.addEventListener(KeyboardEvent.KEY_UP, kup);
        }
        private function mdo(e:MouseEvent):void {
            down = true;
        }
        private function mup(e:MouseEvent):void {
            down = false;
        }
        private function kdo(e:KeyboardEvent):void {
            e.keyCode < 41 && e.keyCode > 36 ? keys[e.keyCode-37] = true:0;
        }
        private function kup(e:KeyboardEvent):void {
            e.keyCode < 41 && e.keyCode > 36 ? keys[e.keyCode-37] = false:0;
        }
        private function cycle(e:Event):void {
            pxs = c.getVector(c.rect);
            i = plen;
            while(--i > -1) {
                curp = ps[i].move(stage.stageWidth, stage.stageHeight, new Point(mouseX, mouseY), down, keys);
                curp.y*stage.stageWidth+curp.x <= pxs.length-1 && curp.x >= 0 && curp.y >= 0 ? pxs[curp.y*stage.stageWidth+curp.x] |= 0xFFFFFF:0;//change the color here for a different pattern. 0x0088FF and 0xFFAA00 and 0xFF8800 and 0xFF00DD are really cool.
                //switch this next line for the previous line to have the color depend on particle speed.
                //curp.y*stage.stageWidth+curp.x <= pxs.length-1 && curp.x >= 0 && curp.y >= 0 ? pxs[curp.y*stage.stageWidth+curp.x] |= ((curp.x-ps[i].x)*(curp.x-ps[i].x)+(curp.y-ps[i].y)*(curp.y-ps[i].y))/200*0xFFFFFF:0;
            }
            c.setVector(c.rect, pxs);
            c.colorTransform(c.rect, btrans);
            c.applyFilter(c, c.rect, new Point(0, 0), bfilt);
        }
    }
}
import flash.geom.Point;
class particle extends Point {
    private var xv:Number;
    private var yv:Number;
    private var prevx:int;
    private var prevy:int;
    private var angle:int;
    private var i:uint;
    public function particle(_x:int, _y:int):void {
        super(_x, _y);
        xv = yv = 0;
    }
    public function move(w:int, h:int, m:Point, d:Boolean, k:Vector.<Boolean>):Point {
        xv *= 0.99;
        yv *= 0.99;
        prevx = x;
        prevy = y;
        if(d) {
            angle = Math.atan2(m.y-y, m.x-x);
            xv += Math.cos(angle);
            yv += Math.sin(angle);
        }
        yv += 0.2;//downwards gravity
        for(i = 0; i < 4; i++) {
            if(k[i]) {
                switch(i) {
                    case 0:xv--;break;
                    case 1:yv--;break;
                    case 2:xv++;break;
                    case 3:yv++;break;
                }
            }
        }
        x += xv+Math.random()*4-2;
        y += yv+Math.random()*4-2;
        if(x > w) {
            xv = ~xv+1;
            x = w-1;
        }
        else if(x < 0) {
            xv = ~xv+1;
            x = 1;
        }
        if(y > h) {
            yv = ~yv+1;
            y = h-1;
        }
        else if(y < 0) {
            yv = ~yv+1;
            y = 1;
        }
        xv > 7 ? xv = 7:0;
        yv > 7 ? yv = 7:0;
        return new Point(prevx, prevy);
    }
}