Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.14 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var vection = (function () {
  2.  
  3.     function vection(){
  4.         return vection.create.apply(vection, [].slice.call(arguments));
  5.     }
  6.    
  7.     function _extend( target, base) {
  8.         for (var key in base) {
  9.             if ({}.hasOwnProperty.call(base, key)) {
  10.                 target[key] = base[key];
  11.             }
  12.         }
  13.         return target;
  14.     }
  15.  
  16.     var undefined, _create = Object.create || function (obj) {
  17.         function F() {}
  18.         F.prototype = obj;
  19.         return new F;
  20.     };
  21.  
  22.     _extend(vection, {
  23.         create: function( left, top) {
  24.             var neu = _create( this.prototype);
  25.             neu.left = typeof left == "object" ? left.left : (left || 0);
  26.             neu.top = typeof left == "object" ? left.top : (top || 0);
  27.            
  28.             return neu;
  29.         },
  30.         is: function (obj) {
  31.             return this.prototype.isPrototypeOf(obj);
  32.         },
  33.         parse: function( str){
  34.             var left = /left:\s*(\d+)/.exec( str),
  35.                 top= /top:\s*(\d+)/.exec( str);
  36.             return vection.create(+(left&&left[1]), +(top&&top[1]));
  37.         }
  38.     });
  39.  
  40.     _extend(vection.prototype, {
  41.         left: 0,
  42.         top: 0,
  43.         clone: function(){
  44.             return vection.create( this);
  45.         },
  46.         scale: function (lambda) {
  47.             var neu = this.clone();
  48.             neu.left *= lambda;
  49.             neu.top *= lambda;
  50.             return neu;
  51.         },
  52.         add: function( a, b){
  53.             var neu = vection.create( a, b);
  54.             neu.left += this.left;
  55.             neu.top += this.top;
  56.             return neu;
  57.         },
  58.         sub: function( a, b){
  59.             return vection.create( a, b).scale(-1).add(this);
  60.         },
  61.         normalize: function(){
  62.             return this.scale(1/this.magnitude());
  63.         },
  64.         magnitude: function(){
  65.             return Math.sqrt( this.left* this.left+ this.top* this.top);
  66.         },
  67.         manhattan: function(){
  68.             return Math.abs( this.left) + Math.abs( this.top);
  69.         },
  70.         toCSS: function( unit){
  71.             return "left: "+this.left+(unit||"px")+"; top:"+this.top+(unit||"px")+" ;";
  72.         }
  73.     });
  74.  
  75.     return vection;
  76. })();