Tuesday, 11 February 2014

Adding 3d model --> Away3D

Loading a 3D mesh made with the 3d software of your preference in as3 with away3d libraries using AssetLibrary (inside away3d.library package) you can use loader3d as well (inside away3d.loaders package).There's a parser class for some 3d file format as AC3DParser, AWD1Parser, DAEParser, OBJParser,between others, see parsers for more file formats availables.

code:

package code {
   
 import away3d.cameras.Camera3D;
 import away3d.containers.View3D;
 import away3d.events.LoaderEvent;
 import away3d.utils.*;
 import away3d.library.*;
 import away3d.library.assets.*;
 import away3d.events.AssetEvent;
 import away3d.loaders.misc.AssetLoaderContext;
 import away3d.loaders.parsers.*;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.net.URLRequest;
 import away3d.entities.Mesh;
 import away3d.textures.BitmapTexture;
 import away3d.materials.TextureMaterial;
 import away3d.tools.utils.Bounds;
 import away3d.tools.helpers.MeshHelper;
 import flash.display.BitmapData;

 [SWF(width="1000", height="600", frameRate="60", backgroundColor="#000000")]
   
    public class model extends Sprite {
        private var _view:View3D;
        private const WIDTH:Number = 1000;
        private const HEIGHT:Number= 600;
        private var sphere : Mesh;
        private var txture : BitmapTexture = new BitmapTexture(new BitmapData(512, 512, false, 0xCC0000));
        private var sphereMaterial : TextureMaterial;
       
       public function model() {
            // constructor code
            super();
           if (stage) initialize();
            else addEventListener(Event.ADDED_TO_STAGE, initialize);
        }
       
         private function initialize(evt:Event = null):void
        {
          removeEventListener( Event.ADDED_TO_STAGE, initialize );
            turnon();
        }
       
        private function turnon():void{
            initStage();
            initEngine();
            setUp3DModel();
            addEventListener( Event.ENTER_FRAME, enterframeHandler );
        }
        private function initStage():void {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;
           
           
        }
        private function initEngine():void
        {
            _view = new View3D();
            _view.backgroundColor = 0x333333;
            _view.width = WIDTH;
            _view.height = HEIGHT;
            _view.antiAlias = 4;
            _view.camera.lens.far = 15000;
            _view.addSourceURL( "srcview/index.html" );
            _view.forceMouseMove = true;
            _view.camera.x=-5000.76;
            _view.camera.y = -700;
            _view.camera.z =-10026.991;
            _view.camera.rotationX=4.5 ;
           
            addChild( _view );
           
        }
       
        private function enterframeHandler( event:Event ):void {
            update();
        }
        private function update():void {
            _view.render();
        }
        private function setUp3DModel():void{
          //With AssetLibrary

            AssetLibrary.enableParser(Max3DSParser);/*See for the rest of parsers http://away3d.com/livedocs/away3d/4.0/away3d/loaders/parsers/package-detail.html*/
            AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
            AssetLibrary.load(new URLRequest("embeds/Sphere.3ds"),new AssetLoaderContext(false));
           
         // With Loader3D

           var loader3d:Loader3D = new Loader3D(false);
           var context:AssetLoaderContext = new AssetLoaderContext(false);
            loader3d.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete, false, 0, true);
            loader3d.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete, false, 0, true);
            loader3d.load(new URLRequest("embeds/Sphere.3ds"),context, null, new Max3DSParser());

        }
        private function onAssetComplete(event:AssetEvent):void
        {
            if (event.asset.assetType == AssetType.MESH) {
                sphere = event.asset as Mesh;
                sphereMaterial = new TextureMaterial(txture);
                sphere.material = sphereMaterial;
                           
                _view.scene.addChild(sphere);
                sphere.x= -5000;
                sphere.y=-800;
                sphere.z=-9000
                sphere.scale(6);
               
                //To obtain bounds of the 3d file
                Bounds.getMeshBounds(sphere);//Retrieve object bounds
                var depth:Number = Bounds.depth;
                var width:Number = Bounds.width;
                var height:Number = Bounds.height;
                trace("depth "+depth +" width "+width+" height "+height);
            }
         
        }
    
    }
   
}
       

Saturday, 1 February 2014

Simple MenuBar - Down List ->Jquery

 Simple MenuBar - Down List, slides on mouseenter, hides on mouseleave.

JS.js file:
(function ($) {
    $.fn.MenuBar = function(options) {
            var options = $.extend(variables, options);
            var variables = {// default variable
                speed : 2000
              }
            var $this    = $(this);
            var opts    = options;
            $this.children("ul").addClass("menudown");
            var list    = $this.find("ul.menudown");// all div
            $this.find("ul.menudown>li:first-child").addClass("firstchild");
            var firstchild= list.find(".firstchild");// first child   
            $this.find("ul.menudown>li").not($this.find("ul.menudown>li:first-child")).addClass("restchildren");           
            var restchildren    = list.find(".restchildren");// rest children

            /* If mouse is entered on first child slide rest children, bind mouseleave event for all div*/ 

                firstchild.mouseenter(function() {
                    restchildren.animate({
                       height:restchildren.find("ul").height()+8,
                      },opts.speed, "linear");
           
                     $this.bind({
             * If mouseleave on rest children hide rest children, unbind mouseout for all div*/

                        mouseleave: function(event) {
                             restchildren.animate({
                              height:0,
                             },opts.speed, "linear");
                             $this.unbind(event)
                         }
                    });
                    event.preventDefault();
                   
                   })
                 };
})(jQuery);

CSS.css file:
@charset "utf-8";
body {font-family:"Arial Black", Gadget, sans-serif; font-size:14px;width:60%;margin:0 auto;background-color: #CCC;}
#menubar a {font-family:Verdana, Geneva, sans-serif;font-size:100%;font-weight:bold;text-decoration:none;}
#menubar {position: absolute;left:46px;top: 0px;width:150px;height:58px;overflow:visible;opacity:0.7;filter:alpha(opacity=70);}
#menubar .menudown {position:relative;list-style-type:none;display:block;margin:0;padding:0;background:transparent;color:#fff;cursor:pointer;}
#menubar .restchildren ul {width:auto;height:auto;list-style-type:none;margin:0;padding:0;background:#fff;}
#menubar .restchildren ul li {display:block;padding:0;margin:0;background: #000000; }
#menubar .restchildren ul li a:hover {background:#C0C;color:#fff; border:double}
#menubar .restchildren {width:100%;border:none;position: absolute;height:0;overflow:hidden;margin-top: 0;margin-bottom: 0;}
#menubar .firstchild{width:auto;padding:0; }
#menubar .firstchild a{padding:10px 8px;display:block;color:#fff; background-color:#F06 ; border:1px solid #a1a1a1;
border-radius:5px;}/*some radius border for first child */
#menubar .menudown ul li a {width:auto;text-decoration:none;color:#fff;display:block;margin:0;padding:10px 8px;}

HTML.html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" media="all" href="CSS.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="JS.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#menubar").MenuBar();/*Calling the function throu ID div*/
});
</script>
</head>
<body>
<div id="menubar">
    <ul>
        <li><a href="http://bellacarta.free.fr" target="_blank">Gender</a></li>
        <li>
            <ul>
                <li><a href="http://en.wikipedia.org/wiki/Male" target="_blank">Male</a></li>
                <li><a href="http://en.wikipedia.org/wiki/Female" target="_blank">Female</a></li>
            </ul>
        </li>
    </ul>
</div>
</body>
</html>

the result:

Menu bar- jquery


Tuesday, 14 January 2014

Given two values from a serie of numbers find the shortest distance

package code {
   
    public class findShortestDistance{
       private var total:Number =32;
      
        public function findShortestDistance(from:Number, to:Number):Number {
            var CountergoLeft:Number = 1; var CountergoRight:Number = 1; var pivot:Number;
                pivot = from;
                while ((pivot = getPrevNum(pivot)) != to) {//Search backwards mean while "from" is different "to" 
                    CountergoLeft++;//Increase counter Left
                }
              pivot = from;
                while ((pivot = getNextNum(pivot)) != to)  {//Search forwards mean while "from" is different "to" 
                    CountergoRight++;//Increase counter Right
                }
                return (CountergoLeft < CountergoRight) ? CountergoLeft * -1 : CountergoRight;//if right times are bigger than left ones than go left with a negative value ( CountergoLeft * -1 ) otherwise go right  (pivot 25 , to 2 =>(23 CountergoLeft - 9 CountergoRight)  returns the shortest distance - 9- to right side)
        }
       
         private function getPrevNum(num):Number {
                if ((num - 1) == 0) {
                    return total;
                } else {
                    return num - 1;
                }
            }

         private function getNextNum(num):Number {
                if ((num + 1) > total) {
                    return 1;
                } else {
                    return num + 1;
                }
            }

    }
   
}