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

flash on 2011-10-22

ActionScript3 なんて触ったことのない私が、Javascript/WebGL で作成中の分子ビューア http://jsdo.it/biochem_fan/iVf0 を Flash に移植してみる。
 
とりあえず球は出た。
Get Adobe Flash player
by biochem_fan 23 Oct 2011

    Talk

    9balls at 23 Oct 2011 07:50
    ぜひStage3D対応でPDBファイルの読み込みができて生体レベルの高分子でもヌルヌル動かせるFlashをお願いします!
    Embed
/**
 * Copyright biochem_fan ( http://wonderfl.net/user/biochem_fan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/m3O2
 */

// Reference:
// wonderfl trace by kalevionni
//  http://wonderfl.net/c/hCok

// This is my first ActionScript.
//  I am starting from "Hello, world" and trying to port
//    my 3D moelcular viewer written in WebGL http://jsdo.it/biochem_fan/iVf0 .

package {
    import flash.display.Sprite;
    import flash.events.UncaughtErrorEvent;
    import flash.events.ErrorEvent;
    import away3d.containers.View3D;
    import away3d.primitives.Sphere;
    import away3d.materials.ColorMaterial;
    
    public class FlashMol extends Sprite {
        public var atoms:Array;
        public var view:View3D;
        
        public function parsePDB(str:String):void {
            var atoms:Array = this.atoms;
            
            var lines:Array = str.split("\n");
            for (var i:int = 0; i < lines.length; i++) {
                var line:String = lines[i].replace(/^\s*/, ''); // remove indent
                var recodeName:String = line.substr(0, 6);
                if (recodeName == 'ATOM  ' || recodeName == 'HETATM') {
                    var atom:Object = new Object();
                    atom.serial = parseInt(line.substr(6, 5));
                    atom.atom = line.substr(12, 4).replace(/ /g, "");
                    atom.resn = line.substr(17, 3);
                    atom.chain = line.substr(21, 1);
                    atom.resi = parseInt(line.substr(22, 5)); 
                    atom.x = parseFloat(line.substr(30, 8));
                    atom.y = parseFloat(line.substr(38, 8));
                    atom.z = parseFloat(line.substr(46, 8));
                    atom.elem = line.substr(76, 2).replace(/ /g, "");
                    if (atom.elem == '') { // for some incorrect PDB files
                        atom.elem = line.substr(12, 4).replace(/ /g,"");
                    }
                    if (recodeName == 'HETATM') atom.hetflag = true;
                    else atom.hetflag = false;
                    atoms[atom.serial] = atom;
                }
            }
       }
       
        public function drawAtomsAsSphere():void {
            for (var i:int = 0; i < this.atoms.length; i++) {
                var atom:Object = this.atoms[i];
                if (atom == null) continue; //?
                
                var sphere:Sphere = new Sphere({x: atom.x, y: atom.y, z: atom.z, radius: 0.5});
                sphere.material = new ColorMaterial(0xCC0000);
                this.view.scene.addChild(sphere);
                
            }

        }

       
        public function FlashMol() {
            inittrace(stage);            

            this.atoms = [];       
            parsePDB(PDBSource);

            this.view = new View3D();
            view.x = stage.stageWidth / 2;
            view.y = stage.stageHeight / 2;
            view.camera.z = -10;
            view.camera.zoom = 50; // Which should I use?
            addChild(view);
            
            drawAtomsAsSphere();
            this.view.render();
        }

    }
}

var PDBSource:String = <pdb>
HETATM    1  C                  -0.805   2.557  -1.558
HETATM    2  C                   0.653   2.988  -1.885
HETATM    3  C                   1.639   2.237  -0.951
HETATM    4  O                   1.329   2.439   0.433
HETATM    5  C                   0.020   2.157   0.829
HETATM    6  C                  -1.075   2.829  -0.058
HETATM    7  H                  -0.953   1.473  -1.790
HETATM    8  O                  -1.651   3.340  -2.375
HETATM    9  H                   0.759   4.093  -1.756
HETATM   10  O                   0.994   2.618  -3.205
HETATM   11  C                   3.100   2.703  -1.123
HETATM   12  H                   1.603   1.141  -1.171
HETATM   13  H                   0.001   2.545   1.875
HETATM   14  O                   0.015   0.761   0.850
HETATM   15  O                  -2.352   2.301   0.243
HETATM   16  H                  -1.060   3.929   0.132
HETATM   17  C                  -0.006   0.321   2.176
HETATM   18  H                  -2.623   2.705   1.054
HETATM   19  H                  -2.530   3.009  -2.245
HETATM   20  H                   1.217   3.421  -3.657
HETATM   21  H                   3.748   2.209  -0.371
HETATM   22  O                   3.178   4.101  -0.974
HETATM   23  H                   3.459   2.402  -2.127
HETATM   24  H                   4.068   4.338  -1.195
HETATM   25  C                   1.087  -0.728   2.554
HETATM   26  C                   0.833  -2.067   1.819
HETATM   27  C                  -0.628  -2.518   2.101
HETATM   28  C                  -1.611  -1.399   1.665
HETATM   29  C                  -3.077  -1.714   2.029
HETATM   30  O                   2.369  -0.273   2.168
HETATM   31  O                   1.675  -3.086   2.319
HETATM   32  O                  -0.952  -3.656   1.329
HETATM   33  O                  -1.315  -0.149   2.297
HETATM   34  O                  -3.177  -2.012   3.402
HETATM   35  H                  -0.001   1.192   2.874
HETATM   36  H                   1.054  -0.895   3.657
HETATM   37  H                   0.999  -1.946   0.720
HETATM   38  H                  -0.751  -2.744   3.189
HETATM   39  H                  -1.557  -1.263   0.557
HETATM   40  H                  -3.724  -0.847   1.786
HETATM   41  H                  -3.423  -2.573   1.422
HETATM   42  H                   2.627   0.371   2.811
HETATM   43  H                   2.557  -2.856   2.061
HETATM   44  H                  -1.183  -4.338   1.945
HETATM   45  H                  -4.068  -2.300   3.542
</pdb>;

/////  The following is from  http://wonderfl.net/c/hCok
/////  WONDERFL TRACE /////

import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;


function inittrace(s:Stage):void
{
    WTrace.initTrace(s);
}

//global trace function
var trace:Function;

//wtreace class
class WTrace
{
        private static var FONT:String = "Fixedsys";
        private static var SIZE:Number = 12;
        private static var TextFields:Array = [];
        private static var trace_stage:Stage;
        
        public static function initTrace(stg:Stage):void
        {
            trace_stage = stg;
            trace = wtrace;
        }
        
        private static function scrollup():void
        {
            // maximum number of lines: 100
            if (TextFields.length > 100) 
            {
                var removeme:TextField = TextFields.shift();
                trace_stage.removeChild(removeme);
                removeme = null;
            }
            for(var x:Number=0;x<TextFields.length;x++)
            {
                (TextFields[x] as TextField).y -= SIZE*1.2;
            }
        }
    
        public static function wtrace(... args):void
        {
        
            var s:String="";
            var tracefield:TextField;
            
            for (var i:int;i < args.length;i++)
            {
                // imitating flash:
                // putting a space between the parameters
                if (i != 0) s+=" ";
                s+=args[i].toString();
            }
            

            tracefield= new TextField();
            tracefield.autoSize = "left";
            tracefield.text = s;
            tracefield.y = trace_stage.stageHeight - 20;

            var tf:TextFormat = new TextFormat(FONT, SIZE);
            tracefield.setTextFormat(tf);
            trace_stage.addChild(tracefield);
            scrollup();                      
            TextFields.push(tracefield);
            
        }
}