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

Plasma Effect

Plasma Effect
by rect (http://blog.r3c7.net/)
Get Adobe Flash player
by rect 08 Jan 2009

    Talk

    O_MEG_A at 26 Apr 2014 01:41
    Wonderful
    Embed
//
// Plasma Effect
// by rect (http://blog.r3c7.net/)
//

package
{
    import flash.events.Event;
    import flash.display.*;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.utils.getTimer;
    import flash.utils.ByteArray;
    
    import frocessing.color.ColorHSV;

    [SWF(backgroundColor="#000000")]
    
    public class main extends Sprite
    {
        private var w:uint = 256;
        private var h:uint = 256;
        private var cont:Bitmap;
        private var buffer:BitmapData;
        private var zero:Point = new Point(0,0);
        
        private var plasma:Vector.<Vector.<uint>>;
        private var palette:Vector.<uint>;
        private var byte:ByteArray;

        public function main():void
        {
            stage.frameRate = 40;
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.quality = StageQuality.LOW;

            byte = new ByteArray();
            buffer = new BitmapData(w, h, false, 0x000000);
            var bmp:BitmapData = new BitmapData(w, h, false, 0x000000);
            cont = new Bitmap(bmp);
            addChild(cont);
            cont.width = stage.stageWidth;
            cont.height = stage.stageHeight;
            cont.x = (stage.stageWidth - cont.width) / 2;
            cont.y = (stage.stageHeight - cont.height) / 2;
            
            plasma = new Vector.<Vector.<uint>>(w);
            for (var i:int = 0; i < w; i++)
            {
                plasma[i] = new Vector.<uint>(h);
                for (var j:int = 0; j < h; j++)
                {
                    var pcolor:uint = uint
                    (
                    128.0 + (128.0 * Math.sin(i / 16))
                    + 128.0 + (128.0 * Math.sin(j / 32))
                    + 128.0 + (128.0 * Math.sin(Math.sqrt(Number((i - w / 2.0)* (i - w / 2) + (j - h / 2) * (j - h / 2))) / 16))
                    + 128.0 + (128.0 * Math.sin(Math.sqrt(Number(i * i + j * j)) / 8))
                    ) / 4;

                    var rgb:uint = (pcolor << 16) | (pcolor << 8 ) | pcolor;
                    plasma[i][j] = rgb;
                }
            }

            palette = new Vector.<uint>(100);
            for(var x:int = 0; x < 100; x++)
            {
                var csb:ColorHSV = new ColorHSV(x + 160, (150 - x)*0.01, Math.min(100, (x + 75)*0.01),1);
                palette[x] = csb.value;
            }

            addEventListener(Event.ENTER_FRAME, update);
        }

        private function update(e:Event):void
        {
            var targetBmd:BitmapData = cont.bitmapData;
            var bytes:ByteArray = byte;
            bytes.position = 0;

            var paletteShift:int = (getTimer() * 0.1) >> 0;
            var pixels:uint = w * h;

            for (var i:int = 0; i < pixels; i++)
            {
                var x:int = (i & 0xFF) >> 0;
                var y:int = (i / w) >> 0;
                var plNum:uint = ((plasma[x][y] + paletteShift) % 100);
                
                var rgb:uint = palette[ plNum ];
                bytes.writeUnsignedInt(rgb);
            }
            
            bytes.position = 0;
            buffer.lock();
            buffer.setPixels(buffer.rect, bytes);
            buffer.unlock();
            targetBmd.copyPixels(buffer, buffer.rect, zero);
        }
    }
}