topical media & game development

talk show tell print

mobile-sugar-build-divsugar.js / js



  (function(){var e,t=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};e={_initialize:function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y=this;this.VERSION="1.2.0",this.EPSILON=1e-4,this.NUM_OF_DIGITS=4,this.DEG_TO_RAD=Math.PI/180,this.RAD_TO_DEG=180/Math.PI,console.log("DivSugar: Version "+this.VERSION),this.rootTask=null,this._frameCount=0,this._currentId=0,this._keyStates={},this._mouseX=0,this._mouseY=0,this._mouseStates={},this._touchFinger=-1,this._css3DTransforms=!0,s=["webkit","Moz","O","ms"],a=["transform","transformStyle","transformOrigin","perspective","perspectiveOrigin","backfaceVisibility"],e=document.createElement("div");for(h=0,v=a.length;h<v;h++){o=a[h],c=o.charAt(0).toUpperCase()+o.substring(1),u="_css"+c;if(o in e.style)this[u]=o;else for(p=0,m=s.length;p<m;p++){n=s[p],i=n+c;if(i in e.style){this[u]=i;break}}this[u]!=null?console.log("DivSugar: Use '"+this[u]+"'"):(console.log("DivSugar: Can't find '"+o+"'"),this._css3DTransforms=!1)}this._css3DTransforms||(t="DivSugar: CSS 3D Transforms is not supported",console.log(t),alert(t));if("requestAnimationFrame"in window)f="requestAnimationFrame";else for(d=0,g=s.length;d<g;d++){n=s[d],r=n.charAt(0).toLowerCase()+n.substring(1)+"RequestAnimationFrame";if(r in window){f=r;break}}return f!=null?(this._requestAnimationFrame=function(e){return window[f](e)},console.log("DivSugar: Use '"+f+"'")):(this._requestAnimationFrame=function(e){return window.setTimeout(e,1e3/60)},console.log("DivSugar: Can't find 'requestAnimationFrame'")),document.addEventListener("keydown",function(e){var t,n;t=e.keyCode,n=y._keyStates[t];if(n==null||n<0)return y._keyStates[t]=y._frameCount},!0),document.addEventListener("keyup",function(e){var t,n;t=e.keyCode,n=y._keyStates[t];if(n==null||n>0)return y._keyStates[t]=-y._frameCount},!0),document.addEventListener("mousemove",function(e){return y._mouseX=e.clientX,y._mouseY=e.clientY},!0),document.addEventListener("mousedown",function(e){var t,n;t=e.button,n=y._mouseStates[t];if(n==null||n<0)return y._mouseStates[t]=y._frameCount},!0),document.addEventListener("mouseup",function(e){var t,n;t=e.button,n=y._mouseStates[t];if(n==null||n>0)return y._mouseStates[t]=-y._frameCount},!0),document.addEventListener("touchmove",function(e){var t,n,r,i,s;i=e.targetTouches,s=[];for(n=0,r=i.length;n<r;n++)t=i[n],t.identifier===y._touchFinger?(y._mouseX=t.clientX,s.push(y._mouseY=t.clientY)):s.push(void 0);return s},!0),document.addEventListener("touchstart",function(e){var t,n;if(y._touchFinger===-1){n=e.changedTouches[0],y._mouseX=n.clientX,y._mouseY=n.clientY,y._touchFinger=n.identifier,t=y._mouseStates[0];if(t==null||t<0)return y._mouseStates[0]=y._frameCount}},!0),document.addEventListener("touchend",function(e){var t,n,r,i,s,o;s=e.changedTouches,o=[];for(r=0,i=s.length;r<i;r++)n=s[r],n.identifier===y._touchFinger?(y._touchFinger=-1,t=y._mouseStates[0],t==null||t>0?o.push(y._mouseStates[0]=-y._frameCount):o.push(void 0)):o.push(void 0);return o},!0),l=function(){var e,t;return e=(new Date).getTime(),t=e-y._lastUpdatedTime,y._lastUpdatedTime=e,y.rootTask.update(t),y._frameCount++,y._requestAnimationFrame(l)},this._lastUpdatedTime=(new Date).getTime(),this._requestAnimationFrame(l)},getFrameCount:function(){return this._frameCount},getKeyState:function(e,t){var n;n=this._keyStates[e];switch(t){case"on":return n!=null&&0<n&&n<this._frameCount;case"off":return n==null||-this._frameCount<n&&n<0;case"pressed":return n===this._frameCount-1;case"released":return n===1-this._frameCount;default:throw"DivSugar: Unknown key state '"+t+"'"}},getMouseX:function(){return this._mouseX},getMouseY:function(){return this._mouseY},getMouseState:function(e,t){var n;n=this._mouseStates[e];switch(t){case"on":return n!=null&&0<n&&n<this._frameCount;case"off":return n==null||-this._frameCount<n&&n<0;case"pressed":return n===this._frameCount-1;case"released":return n===1-this._frameCount;default:throw"DivSugar: Unknown mouse state '"+t+"'"}},inherit:function(e,t){var n;return n=function(){},n.prototype=t.prototype,e.prototype=new n,e.uber=t.prototype,e.prototype.constructor=e,this},generateId:function(){return"_divsugar_id_"+ ++this._currentId},getImageSize:function(e,t){var n;return n=new Image,n.src=e,n.onload=function(){return t(n.width,n.height)},this},getCSSColor:function(e,t,n){return e=Math.floor(e+.5),t=Math.floor(t+.5),n=Math.floor(n+.5),e=e<0?0:e>255?255:e,t=t<0?0:t>255?255:t,n=n<0?0:n>255?255:n,e=e.toString(16),t=t.toString(16),n=n.toString(16),e.length<=1&&(e="0"+e),t.length<=1&&(t="0"+t),n.length<=1&&(n="0"+n),"#"+e+t+n}},(window.DivSugar=e)._initialize(),e.Vector=function(){function t(e,t,n){var r;switch(arguments.length){case 0:this.x=this.y=this.z=0;break;case 1:r=e,this.x=r.x,this.y=r.y,this.z=r.z;break;case 3:this.x=e,this.y=t,this.z=n;break;default:throw"DivSugar: Invalid number of arguments"}}return t.prototype.set=function(e,t,n){var r;switch(arguments.length){case 1:r=e,this.x=r.x,this.y=r.y,this.z=r.z;break;case 3:this.x=e,this.y=t,this.z=n;break;default:throw"DivSugar: Invalid number of arguments"}return this},t.prototype.neg=function(){return this.x=-this.x,this.y=-this.y,this.z=-this.z,this},t.prototype.add=function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this},t.prototype.sub=function(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this},t.prototype.mul=function(e){return this.x*=e,this.y*=e,this.z*=e,this},t.prototype.div=function(e){var t;return t=1/e,this.x*=t,this.y*=t,this.z*=t,this},t.prototype.norm=function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},t.prototype.sqNorm=function(){return this.x*this.x+this.y*this.y+this.z*this.z},t.prototype.dist=function(t){return e.Vector._tmpVec1.set(this).sub(t).norm()},t.prototype.sqDist=function(t){return e.Vector._tmpVec1.set(this).sub(t).sqNorm()},t.prototype.dot=function(e){return this.x*e.x+this.y*e.y+this.z*e.z},t.prototype.cross=function(e){return this.set(this.y*e.z-this.z*e.y,this.z*e.x-this.x*e.z,this.x*e.y-this.y*e.x)},t.prototype.normalize=function(){var t;return t=this.norm(),t<e.EPSILON?this.set(e.Vector.X_UNIT):this.div(t)},t.prototype.rotate=function(t,n,r){var i,s;if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return t!==0&&(s=Math.sin(t*e.DEG_TO_RAD),i=Math.cos(t*e.DEG_TO_RAD),this.set(this.x,this.y*i-this.z*s,this.z*i+this.y*s)),n!==0&&(s=Math.sin(n*e.DEG_TO_RAD),i=Math.cos(n*e.DEG_TO_RAD),this.set(this.x*i+this.z*s,this.y,this.z*i-this.x*s)),r!==0&&(s=Math.sin(r*e.DEG_TO_RAD),i=Math.cos(r*e.DEG_TO_RAD),this.set(this.x*i-this.y*s,this.y*i+this.x*s,this.z)),this},t.prototype.lerp=function(t,n){var r;return n>1-e.EPSILON?this.set(t):n>=e.EPSILON&&(r=e.Vector._tmpVec1,r.set(t).mul(n),this.mul(1-n).add(r)),this},t.prototype.toLocal=function(t){var n;return n=e.Vector._tmpVec1,n.set(this.sub(t.trans)),this.set(n.dot(t.xAxis)/t.xAxis.sqNorm(),n.dot(t.yAxis)/t.yAxis.sqNorm(),n.dot(t.zAxis)/t.zAxis.sqNorm())},t.prototype.toGlobal=function(t){var n,r,i;return n=e.Vector._tmpVec1,r=e.Vector._tmpVec2,i=e.Vector._tmpVec3,n.set(t.xAxis).mul(this.x),r.set(t.yAxis).mul(this.y),i.set(t.zAxis).mul(this.z),this.set(n).add(r).add(i).add(t.trans)},t.prototype.toLocal_noTrans=function(e){return this.set(this.dot(e.xAxis)/e.xAxis.sqNorm(),this.dot(e.yAxis)/e.yAxis.sqNorm(),this.dot(e.zAxis)/e.zAxis.sqNorm())},t.prototype.toGlobal_noTrans=function(t){var n,r,i;return n=e.Vector._tmpVec1,r=e.Vector._tmpVec2,i=e.Vector._tmpVec3,n.set(t.xAxis).mul(this.x),r.set(t.yAxis).mul(this.y),i.set(t.zAxis).mul(this.z),this.set(n).add(r).add(i)},t.prototype.equal=function(e){return this.x===e.x&&this.y===e.y&&this.z===e.z},t.prototype.toString=function(){return"("+this.x+", "+this.y+", "+this.z+")"},t}(),e.Vector.ZERO=new e.Vector(0,0,0),e.Vector.X_UNIT=new e.Vector(1,0,0),e.Vector.Y_UNIT=new e.Vector(0,1,0),e.Vector.Z_UNIT=new e.Vector(0,0,1),e.Vector._tmpVec1=new e.Vector,e.Vector._tmpVec2=new e.Vector,e.Vector._tmpVec3=new e.Vector,e.Matrix=function(){function t(t){switch(arguments.length){case 0:this.xAxis=new e.Vector(e.Vector.X_UNIT),this.yAxis=new e.Vector(e.Vector.Y_UNIT),this.zAxis=new e.Vector(e.Vector.Z_UNIT),this.trans=new e.Vector(e.Vector.ZERO);break;case 1:this.xAxis=new e.Vector(t.xAxis),this.yAxis=new e.Vector(t.yAxis),this.zAxis=new e.Vector(t.zAxis),this.trans=new e.Vector(t.trans);break;case 12:this.xAxis=new e.Vector(arguments[0],arguments[1],arguments[2]),this.yAxis=new e.Vector(arguments[3],arguments[4],arguments[5]),this.zAxis=new e.Vector(arguments[6],arguments[7],arguments[8]),this.trans=new e.Vector(arguments[9],arguments[10],arguments[11]);break;default:throw"DivSugar: Invalid number of arguments"}}return t.prototype.set=function(e){switch(arguments.length){case 1:this.xAxis.set(e.xAxis),this.yAxis.set(e.yAxis),this.zAxis.set(e.zAxis),this.trans.set(e.trans);break;case 12:this.xAxis.set(arguments[0],arguments[1],arguments[2]),this.yAxis.set(arguments[3],arguments[4],arguments[5]),this.zAxis.set(arguments[6],arguments[7],arguments[8]),this.trans.set(arguments[9],arguments[10],arguments[11]);break;default:throw"DivSugar: Invalid number of arguments"}return this},t.prototype.fromQuaternion=function(e){var t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m;return n=e.x,r=e.y,i=e.z,t=e.w,a=n+n,h=r+r,v=i+i,s=t*a,o=t*h,u=t*v,f=n*a,l=n*h,c=n*v,p=r*h,d=r*v,m=i*v,this.set(1-(p+m),l+u,c-o,l-u,1-(f+m),d+s,c+o,d-s,1-(f+p),0,0,0),this},t.prototype.orthonormalize=function(){var t,n,r;return t=e.Matrix._tmpVec1,n=e.Matrix._tmpVec2,r=e.Matrix._tmpVec3,r.set(this.zAxis).normalize(),t.set(this.yAxis).cross(this.zAxis).normalize(),n.set(r).cross(t),this.xAxis.set(t),this.yAxis.set(n),this.zAxis.set(r),this},t.prototype.translate=function(t,n,r){var i,s,o;if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return i=e.Matrix._tmpVec1,s=e.Matrix._tmpVec2,o=e.Matrix._tmpVec3,i.set(this.xAxis).mul(t),s.set(this.yAxis).mul(n),o.set(this.zAxis).mul(r),this.trans.add(i).add(s).add(o),this},t.prototype.rotate=function(t,n,r){var i,s,o,u;if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return t!==0&&(o=t*e.DEG_TO_RAD,u=Math.sin(o),i=Math.cos(o),s=e.Matrix._tmpMat1,s.set(1,0,0,0,i,u,0,-u,i,0,0,0).toGlobal(this),this.set(s)),n!==0&&(o=n*e.DEG_TO_RAD,u=Math.sin(o),i=Math.cos(o),s=e.Matrix._tmpMat1,s.set(i,0,-u,0,1,0,u,0,i,0,0,0).toGlobal(this),this.set(s)),r!==0&&(o=r*e.DEG_TO_RAD,u=Math.sin(o),i=Math.cos(o),s=e.Matrix._tmpMat1,s.set(i,u,0,-u,i,0,0,0,1,0,0,0).toGlobal(this),this.set(s)),this},t.prototype.rotateAround=function(t,n){var r,i,s,o,u,a,f,l,c,h,p,d,v,m;return a=t.x,h=t.y,v=t.z,f=a*a,p=h*h,m=v*v,l=a*h,c=a*v,d=h*v,o=n*e.DEG_TO_RAD,u=Math.sin(o),r=Math.cos(o),i=1-r,s=e.Matrix._tmpMat1,s.set(f*i+r,l*i+v*u,c*i-h*u,l*i-v*u,p*i+r,d*i+a*u,c*i+h*u,d*i-a*u,m*i+r,0,0,0).toGlobal(this),this.set(s),this},t.prototype.scale=function(e,t,n){if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return this.xAxis.mul(e),this.yAxis.mul(t),this.zAxis.mul(n),this},t.prototype.slerp=function(t,n){var r,i,s;return n>1-e.EPSILON?this.set(t):n>=e.EPSILON&&(s=e.Matrix._tmpVec1,r=e.Matrix._tmpQuat1,i=e.Matrix._tmpQuat2,r.fromMatrix(this),i.fromMatrix(t),s.set(this.trans).lerp(t.trans,n),this.fromQuaternion(r.slerp(i,n)),this.trans.set(s)),this},t.prototype.slerp_noTrans=function(t,n){var r,i;return n>1-e.EPSILON?(this.set(t),this.trans.set(e.Vector.ZERO)):n>=e.EPSILON?(r=e.Matrix._tmpQuat1,i=e.Matrix._tmpQuat2,r.fromMatrix(this),i.fromMatrix(t),this.fromQuaternion(r.slerp(i,n))):this.trans.set(e.Vector.ZERO),this},t.prototype.toLocal=function(t){var n,r,i,s;return s=e.Matrix._tmpVec1,n=1/t.xAxis.sqNorm(),r=1/t.yAxis.sqNorm(),i=1/t.zAxis.sqNorm(),s.set(this.trans).sub(t.trans),this.set(this.xAxis.dot(t.xAxis)*n,this.xAxis.dot(t.yAxis)*r,this.xAxis.dot(t.zAxis)*i,this.yAxis.dot(t.xAxis)*n,this.yAxis.dot(t.yAxis)*r,this.yAxis.dot(t.zAxis)*i,this.zAxis.dot(t.xAxis)*n,this.zAxis.dot(t.yAxis)*r,this.zAxis.dot(t.zAxis)*i,s.dot(t.xAxis)*n,s.dot(t.yAxis)*r,s.dot(t.zAxis)*i),this},t.prototype.toGlobal=function(e){return this.xAxis.toGlobal_noTrans(e),this.yAxis.toGlobal_noTrans(e),this.zAxis.toGlobal_noTrans(e),this.trans.toGlobal(e),this},t.prototype.toLocal_noTrans=function(e){var t,n,r;return t=1/e.xAxis.sqNorm(),n=1/e.yAxis.sqNorm(),r=1/e.zAxis.sqNorm(),this.set(this.xAxis.dot(e.xAxis)*t,this.xAxis.dot(e.yAxis)*n,this.xAxis.dot(e.zAxis)*r,this.yAxis.dot(e.xAxis)*t,this.yAxis.dot(e.yAxis)*n,this.yAxis.dot(e.zAxis)*r,this.zAxis.dot(e.xAxis)*t,this.zAxis.dot(e.yAxis)*n,this.zAxis.dot(e.zAxis)*r,0,0,0),this},t.prototype.toGlobal_noTrans=function(t){return this.xAxis.toGlobal_noTrans(t),this.yAxis.toGlobal_noTrans(t),this.zAxis.toGlobal_noTrans(t),this.trans.set(e.Vector.ZERO),this},t.prototype.lookAt=function(e,t,n){return this.zAxis.set(e).sub(t).normalize(),this.xAxis.set(n).cross(this.zAxis).normalize(),this.yAxis.set(this.zAxis).cross(this.xAxis),this.trans.set(e),this},t.prototype.equal=function(e){return this.xAxis.equal(e.xAxis)&&this.yAxis.equal(e.yAxis)&&this.zAxis.equal(e.zAxis)&&this.trans.equal(e.trans)},t.prototype.toString=function(){return"("+this.xAxis.toString()+", "+this.yAxis.toString()+", "+this.zAxis.toString()+", "+this.trans.toString()+")"},t.prototype.toCSSTransform2D=function(){var t,n,r,i;return t=e.NUM_OF_DIGITS,r=this.xAxis,i=this.yAxis,n=this.trans,"matrix("+r.x.toFixed(t)+", "+r.y.toFixed(t)+", "+i.x.toFixed(t)+", "+i.y.toFixed(t)+", "+n.x.toFixed(t)+", "+n.y.toFixed(t)+")"},t.prototype.toCSSTransform3D=function(){var t,n,r,i,s;return t=e.NUM_OF_DIGITS,r=this.xAxis,i=this.yAxis,s=this.zAxis,n=this.trans,"matrix3d("+(""+r.x.toFixed(t)+", "+r.y.toFixed(t)+", "+r.z.toFixed(t)+", 0, ")+(""+i.x.toFixed(t)+", "+i.y.toFixed(t)+", "+i.z.toFixed(t)+", 0, ")+(""+s.x.toFixed(t)+", "+s.y.toFixed(t)+", "+s.z.toFixed(t)+", 0, ")+(""+n.x.toFixed(t)+", "+n.y.toFixed(t)+", "+n.z.toFixed(t)+", 1)")},t}(),e._css3DTransforms?e.Matrix.prototype._toCSSTransform=e.Matrix.prototype.toCSSTransform3D:e.Matrix.prototype._toCSSTransform=e.Matrix.prototype.toCSSTransform2D,e.Matrix.UNIT=new e.Matrix(1,0,0,0,1,0,0,0,1,0,0,0),e.Matrix._tmpVec1=new e.Vector,e.Matrix._tmpVec2=new e.Vector,e.Matrix._tmpVec3=new e.Vector,e.Matrix._tmpMat1=new e.Matrix,e.Quaternion=function(){function t(e,t,n,r){var i;switch(arguments.length){case 0:this.x=this.y=this.z=this.w=0;break;case 1:i=e,this.x=i.x,this.y=i.y,this.z=i.z,this.w=i.w;break;case 4:this.x=e,this.y=t,this.z=n,this.w=r;break;default:throw"DivSugar: Invalid number of arguments"}}return t.prototype.set=function(e,t,n,r){var i;switch(arguments.length){case 1:i=e,this.x=i.x,this.y=i.y,this.z=i.z,this.w=i.w;break;case 4:this.x=e,this.y=t,this.z=n,this.w=r;break;default:throw"DivSugar: Invalid number of arguments"}return this},t.prototype.fromMatrix=function(e){var t,n,r,i,s,o,u;n=e.xAxis,r=e.yAxis,i=e.zAxis,u=n.x+r.y+i.z;if(u>0)s=Math.sqrt(u+1),o=.5/s,this.set((r.z-i.y)*o,(i.x-n.z)*o,(n.y-r.x)*o,s*.5);else{t=r.y>n.x?i.z>r.y?2:1:i.z>n.x?2:0;switch(t){case 0:s=Math.sqrt(n.x-(r.y+i.z)+1),o=s!==0?.5/s:s,this.set(s*.5,(n.y+r.x)*o,(i.x+n.z)*o,(r.z-i.y)*o);break;case 1:s=Math.sqrt(r.y-(i.z+n.x)+1),o=s!==0?.5/s:s,this.set((n.y+r.x)*o,s*.5,(r.z+i.y)*o,(i.x-n.z)*o);break;case 2:s=Math.sqrt(i.z-(n.x+r.y)+1),o=s!==0?.5/s:s,this.set((i.x+n.z)*o,(r.z+i.y)*o,s*.5,(n.y-r.x)*o)}}return this},t.prototype.slerp=function(t,n){var r,i,s,o,u,a;return n>1-e.EPSILON?this.set(t):n>=e.EPSILON&&(s=e.Quaternion._tmpQuat1,r=this.x*t.x+this.y*t.y+this.z*t.z+this.w*t.w,r<0?(r=-r,s.set(-t.x,-t.y,-t.z,-t.w)):s.set(t),r>=1?this.set(t):(i=Math.acos(r>1?1:r),a=Math.sin(i),o=Math.sin(i*(1-n))/a,u=Math.sin(i*n)/a,this.set(this.x*o+s.x*u,this.y*o+s.y*u,this.z*o+s.z*u,this.w*o+s.w*u))),this},t.prototype.equal=function(e){return this.x===e.x&&this.y===e.y&&this.z===e.z&&this.w===e.w},t.prototype.toString=function(){return"("+this.x+", "+this.y+", "+this.z+", "+this.w+")"},t}(),e.Quaternion._tmpQuat1=new e.Quaternion,e.Matrix._tmpQuat1=new e.Quaternion,e.Matrix._tmpQuat2=new e.Quaternion,e.Scene=function(){function n(t){this.div=document.createElement("div"),t!=null&&(this.div.id=t),this.div.style.margin="0px",this.div.style.padding="0px",this.div.style.position="relative",this.div.style.overflow="hidden",this.div.sugar=this,this._isScene=!0,this._viewWidth=1,this._centerNode=new e.Node,this._centerNode.div.style[e._cssTransformOrigin]="0 0",this._centerNode.div.style[e._cssPerspectiveOrigin]="0 0",this._centerNode.div.sugar=this,this.div.appendChild(this._centerNode.div),this._rootNode=new e.Node,this._rootNode.div.sugar=this,this._centerNode.div.appendChild(this._rootNode.div),this.setViewAngle(45),this.setSize(400,300),this.setPosition(0,0),this.setVisible(!0),this.setClip(!0),this.setOpacity(1),this.setImage("#0000ff"),this.setImageClip(0,0,1,1)}return n.prototype.getParent=function(){return this.div.parentNode},n.prototype.append=function(e){return this._rootNode.div.appendChild(e.div),this},n.prototype.appendTo=function(e){return e.appendChild(this.div),this},n.prototype.remove=function(e){var n;return(n=e.div,t.call(this._rootNode.div.childNodes,n)>=0)&&this._rootNode.div.removeChild(e.div),this},n.prototype.getViewAngle=function(){return this._viewAngle},n.prototype.getPerspective=function(){return this._perspective},n.prototype.setViewAngle=function(t){return this._viewAngle=t,this._perspective=Math.tan((90-t/2)*e.DEG_TO_RAD)*this._viewWidth/2,this._centerNode.div.style[e._cssPerspective]=""+this._perspective.toFixed(e.NUM_OF_DIGITS)+"px",this},n.prototype.getWidth=function(){return this._width},n.prototype.getHeight=function(){return this._height},n.prototype.getViewWidth=function(){return this._viewWidth},n.prototype.getViewHeight=function(){return this._viewHeight},n.prototype.setSize=function(t,n,r,i){return r==null&&(r=t),i==null&&(i=n),this._width=t,this._height=n,this._viewWidth=r,this._viewHeight=i,this.div.style.width=""+t.toFixed(e.NUM_OF_DIGITS)+"px",this.div.style.height=""+n.toFixed(e.NUM_OF_DIGITS)+"px",this._centerNode.setPosition(t/2,n/2,0),this._rootNode.setTransform(e.Matrix.UNIT).setPosition(-t/2,-n/2,0).scale(t/r,n/i,1),this.setViewAngle(this._viewAngle),this},n.prototype.getPositionX=function(){return this._positionX},n.prototype.getPositionY=function(){return this._positionY},n.prototype.setPosition=function(t,n){if(arguments.length!==2)throw"DivSugar: Invalid number of arguments";return this._positionX=t,this._positionY=n,this.div.style.left=""+t.toFixed(e.NUM_OF_DIGITS)+"px",this.div.style.top=""+n.toFixed(e.NUM_OF_DIGITS)+"px",this},n.prototype.getVisible=function(){return this._visible},n.prototype.setVisible=function(e){return this._visible=e,this.div.style.visibility=e?"visible":"hidden",this},n.prototype.getClip=function(){return this._clip},n.prototype.setClip=function(e){return this._clip=e,this.div.style.overflow=e?"hidden":"visible",this},n.prototype.getOpacity=function(){return this._opacity},n.prototype.setOpacity=function(t){return this._opacity=t,this.div.style.opacity=t.toFixed(e.NUM_OF_DIGITS),this},n.prototype.getImage=function(){return this._image},n.prototype.setImage=function(t,n){return this._image=t,t==null?(this.div.style.backgroundColor="transparent",this.div.style.backgroundImage="none"):t.charAt(0)==="#"?(this.div.style.backgroundColor=t,this.div.style.backgroundImage="none"):(this.div.style.backgroundColor="transparent",this.div.style.backgroundImage="url("+t+")",n!=null&&e.getImageSize(t,n)),this},n.prototype.getImageClipU1=function(){return this._imageClipU1},n.prototype.getImageClipV1=function(){return this._imageClipV1},n.prototype.getImageClipU2=function(){return this._imageClipU2},n.prototype.getImageClipV2=function(){return this._imageClipV2},n.prototype.setImageClip=function(t,n,r,i){var s,o,u,a,f;return t=t<0?0:t>1?1:t,n=n<0?0:n>1?1:n,r=r<0?0:r>1?1:r,i=i<0?0:i>1?1:i,this._imageClipU1=t,this._imageClipV1=n,this._imageClipU2=r,this._imageClipV2=i,u=1/(r-t)*100,s=1/(i-n)*100,a=u>100?t*u/(u-100)*100:0,f=s>100?n*s/(s-100)*100:0,o=e.NUM_OF_DIGITS,this.div.style.backgroundPosition=""+a.toFixed(o)+"% "+f.toFixed(o)+"%",this.div.style.backgroundSize=""+u.toFixed(o)+"% "+s.toFixed(o)+"%",this},n.prototype.adjustLayout=function(e,t,n){switch(n){case"center":break;case"contain":e<t*this._viewWidth/this._viewHeight?this.setSize(e,e*this._viewHeight/this._viewWidth,this._viewWidth,this._viewHeight):this.setSize(t*this._viewWidth/this._viewHeight,t,this._viewWidth,this._viewHeight);break;case"cover":e>t*this._viewWidth/this._viewHeight?this.setSize(e,e*this._viewHeight/this._viewWidth,this._viewWidth,this._viewHeight):this.setSize(t*this._viewWidth/this._viewHeight,t,this._viewWidth,this._viewHeight);break;default:throw"DivSugar: Unknown layout style '"+n+"'"}return this.setPosition((e-this._width)/2,(t-this._height)/2),this},n.prototype.getLocalPosition=function(e,t,n){var r,i,s;return r=this.div.getBoundingClientRect(),i=(e-r.left)*this._viewWidth/this._width,s=(t-r.top)*this._viewHeight/this._height,n.set(i,s,0),this},n}(),e.Node=function(){function n(t){this.div=document.createElement("div"),t!=null&&(this.div.id=t),this.div.style.margin="0px",this.div.style.padding="0px",this.div.style.position="absolute",this.div.style[e._cssTransformStyle]="preserve-3d",this.div.style[e._cssTransformOrigin]="0% 0%",this.div.sugar=this,this._transform=new e.Matrix,this._animTasks=[],this.setSize(0,0),this.setPosition(0,0,0),this.setVisible(!0),this.setBackface(!0),this.setClip(!1),this.setOpacity(1),this.setImage(null),this.setImageClip(0,0,1,1)}return n.prototype.getParent=function(){var e;return e=this.div.parentNode,e!=null?e.sugar:null},n.prototype.append=function(e){return this.div.appendChild(e.div),this},n.prototype.appendTo=function(e){return e._rootNode!=null?e._rootNode.div.appendChild(this.div):e.div.appendChild(this.div),this},n.prototype.remove=function(e){var n;return(n=e.div,t.call(this.div.childNodes,n)>=0)&&this.div.removeChild(e.div),this},n.prototype.getWidth=function(){return this._width},n.prototype.getHeight=function(){return this._height},n.prototype.setSize=function(t,n){return this._width=t,this._height=n,this.div.style.width=""+t.toFixed(e.NUM_OF_DIGITS)+"px",this.div.style.height=""+n.toFixed(e.NUM_OF_DIGITS)+"px",this},n.prototype.getPositionX=function(){return this._transform.trans.x},n.prototype.getPositionY=function(){return this._transform.trans.y},n.prototype.getPositionZ=function(){return this._transform.trans.z},n.prototype.getPosition=function(e){return e.x=this._transform.trans.x,e.y=this._transform.trans.y,e.z=this._transform.trans.z,this},n.prototype.setPosition=function(t,n,r){var i;switch(arguments.length){case 1:i=t,this._transform.trans.set(i);break;case 3:this._transform.trans.x=t,this._transform.trans.y=n,this._transform.trans.z=r;break;default:throw"DivSugar: Invalid number of arguments"}return this.div.style[e._cssTransform]=this._transform._toCSSTransform(),this},n.prototype.getTransform=function(e){return e.set(this._transform),this},n.prototype.setTransform=function(t){return this._transform.set(t),this.div.style[e._cssTransform]=this._transform._toCSSTransform(),this},n.prototype.getVisible=e.Scene.prototype.getVisible,n.prototype.setVisible=e.Scene.prototype.setVisible,n.prototype.getBackface=function(){return this._backface},n.prototype.setBackface=function(t){return this._backface=t,this.div.style[e._cssBackfaceVisibility]=t?"visible":"hidden",this},n.prototype.getClip=e.Scene.prototype.getClip,n.prototype.setClip=e.Scene.prototype.setClip,n.prototype.getOpacity=e.Scene.prototype.getOpacity,n.prototype.setOpacity=e.Scene.prototype.setOpacity,n.prototype.getImage=e.Scene.prototype.getImage,n.prototype.setImage=e.Scene.prototype.setImage,n.prototype.getImageClipU1=e.Scene.prototype.getImageClipU1,n.prototype.getImageClipV1=e.Scene.prototype.getImageClipV1,n.prototype.getImageClipU2=e.Scene.prototype.getImageClipU2,n.prototype.getImageClipV2=e.Scene.prototype.getImageClipV2,n.prototype.setImageClip=e.Scene.prototype.setImageClip,n.prototype.translate=function(t,n,r){if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return this._transform.translate(t,n,r),this.div.style[e._cssTransform]=this._transform._toCSSTransform(),this},n.prototype.rotate=function(t,n,r){if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return this._transform.rotate(t,n,r),this.div.style[e._cssTransform]=this._transform._toCSSTransform(),this},n.prototype.rotateAround=function(t,n){return this._transform.rotateAround(t,n),this.div.style[e._cssTransform]=this._transform._toCSSTransform(),this},n.prototype.scale=function(t,n,r){if(arguments.length!==3)throw"DivSugar: Invalid number of arguments";return this._transform.scale(t,n,r),this.div.style[e._cssTransform]=this._transform._toCSSTransform(),this},n.prototype.playAnimation=function(t){var n,r=this;return n=new e.Task,n.animation=t,n._cmdIndex=0,n._firstFrame=!0,n.onUpdate=function(){return r._updateAnimation(n)},n.onDestroy=function(){return r._destroyAnimation(n)},n.appendTo(e.rootTask),this._animTasks.push(n),n},n.prototype._updateAnimation=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w;while(t.deltaTime>0){if(t._cmdIndex>=t.animation.length){t.destroy();return}o=t.animation[t._cmdIndex];switch(o[0]){case"to":if(t._firstFrame){t._firstFrame=!1,t._currentTime=0,t._totalTime=(h=o[2])!=null?h:0,t._easeFunc=(p=o[3])!=null?p:e.Ease.linear,t._fromTransform=null,d=o[1];for(a in d){c=d[a];switch(a){case"size":t._fromSize=[this._width,this._height];break;case"position":t._fromPosition=[this._transform.trans.x,this._transform.trans.y,this._transform.trans.z];break;case"transform":(v=t._fromTransform)==null&&(t._fromTransform=new e.Matrix(this._transform));break;case"visible":this.setVisible(c);break;case"backface":this.setBackface(c);break;case"clip":this.setClip(c);break;case"opacity":t._fromOpacity=this._opacity;break;case"image":this.setImage(c);break;case"imageClip":t._fromImageClip=[this._imageClipU1,this._imageClipV1,this._imageClipU2,this._imageClipV2];break;case"translate":(m=t._fromTransform)==null&&(t._fromTransform=new e.Matrix(this._transform));break;case"rotate":(g=t._fromTransform)==null&&(t._fromTransform=new e.Matrix(this._transform));break;case"scale":(y=t._fromTransform)==null&&(t._fromTransform=new e.Matrix(this._transform));break;default:throw"DivSugar: Unknown animation parameter '"+a+"'"}}}t._fromTransform!=null&&this._transform.set(t._fromTransform),t._totalTime>t._currentTime+t.deltaTime?(t._currentTime+=t.deltaTime,t.deltaTime=0):(t.deltaTime-=t._totalTime,t._currentTime=t._totalTime,t._cmdIndex++,t._firstFrame=!0),t._totalTime>0?(r=t._easeFunc(t._currentTime/t._totalTime),n=1-r):(r=1,n=0),b=o[1];for(a in b){c=b[a];switch(a){case"size":l=t._fromSize,this.setSize(l[0]*n+c[0]*r,l[1]*n+c[1]*r);break;case"position":f=t._fromPosition,this.setPosition(f[0]*n+c[0]*r,f[1]*n+c[1]*r,f[2]*n+c[2]*r);break;case"transform":this.setTransform(e.Node._tmpMat1.set(this._transform).slerp(c,r));break;case"opacity":this.setOpacity(t._fromOpacity*n+c*r);break;case"imageClip":s=t._fromImageClip,this.setImageClip(s[0]*n+c[0]*r,s[1]*n+c[1]*r,s[2]*n+c[2]*r,s[3]*n+c[3]*r);break;case"translate":this.translate(c[0]*r,c[1]*r,c[2]*r);break;case"rotate":this.rotate(c[0]*r,c[1]*r,c[2]*r);break;case"scale":this.scale(n+c[0]*r,n+c[1]*r,n+c[2]*r)}}break;case"wait":t._firstFrame&&(t._firstFrame=!1,t._waitTime=o[1]),t._waitTime>t.deltaTime?(t._waitTime-=t.deltaTime,t.deltaTime=0):(t.deltaTime-=t._waitTime,t._waitTime=0,t._cmdIndex++,t._firstFrame=!0);break;case"play":for(u in o)i=o[u],u>0&&this.playAnimation(o[u]);t._cmdIndex++,t._firstFrame=!0;break;case"call":for(u in o)i=o[u],u>0&&o[u]();t._cmdIndex++,t._firstFrame=!0;break;case"repeat":t._repeatCount==null&&(t._repeatCount=(w=o[1])!=null?w:-1),t._repeatCount===0?(t._cmdIndex++,t._firstFrame=!0):(t._repeatCount>0&&t._repeatCount--,t._cmdIndex=0,t._firstFrame=!0);break;default:throw"DivSugar: Unknown animation command '"+o[0]+"'"}}},n.prototype._destroyAnimation=function(e){var t;t=this._animTasks.indexOf(e);if(t>-1)return this._animTasks.splice(t,1)},n.prototype.clearAnimations=function(){while(this._animTasks.length>0)this._animTasks.shift().destroy();return this},n.prototype.getWorldPosition=function(e){var t;e.set(this._transform.trans),t=this.div.parentNode;while(t!=null&&t.sugar!=null&&t.sugar._isScene==null)e.toGlobal(t.sugar._transform),t=t.parentNode;return this},n.prototype.getWorldTransform=function(e){var t;e.set(this._transform),t=this.div.parentNode;while(t!=null&&t.sugar!=null&&t.sugar._isScene==null)e.toGlobal(t.sugar._transform),t=t.parentNode;return this},n}(),e.Node._tmpMat1=new e.Matrix,e.Task=function(){function e(e){this.id=e!=null?e:null,this.active=!0,this.deltaTime=0,this.onUpdate=null,this.onDestroy=null,this._parent=null,this._children=[]}return e.prototype.getParent=function(){return this._parent},e.prototype.append=function(e){return this.remove(e),this._children.push(e),e._parent=this,this},e.prototype.appendTo=function(e){return e.append(this),this},e.prototype.remove=function(e){var t;return t=this._children.indexOf(e),t>-1&&(this._children[t]=null,e._parent=null),this},e.prototype.update=function(e){var t,n,r;if(this.active){this.deltaTime+=e,typeof this.onUpdate=="function"&&this.onUpdate(),this.deltaTime=0,n=0,r=this._children.length;while(n<r)t=this._children[n],t!=null?(t.update(e),n++):(this._children.splice(n,1),r--)}return this},e.prototype.destroy=function(){var e,t,n,r,i;typeof this.onDestroy=="function"&&this.onDestroy(),(r=this._parent)!=null&&r.remove(this),i=this._children;for(t=0,n=i.length;t<n;t++)e=i[t],e!=null&&e.destroy();return this},e.prototype.getTaskById=function(e){var t,n,r,i,s;if(this.id===e)return this;s=this._children;for(r=0,i=s.length;r<i;r++){t=s[r];if(t!=null){n=t.getTaskById(e);if(n!=null)return n}}return null},e}(),e.rootTask=new e.Task,e.Ease={linear:function(e){return e},quadIn:function(e){return Math.pow(e,2)},cubicIn:function(e){return Math.pow(e,3)},quartIn:function(e){return Math.pow(e,4)},quintIn:function(e){return Math.pow(e,5)},quadOut:function(e){return 1-Math.pow(1-e,2)},cubicOut:function(e){return 1-Math.pow(1-e,3)},quartOut:function(e){return 1-Math.pow(1-e,4)},quintOut:function(e){return 1-Math.pow(1-e,5)},quadInOut:function(e){return e<.5?Math.pow(e*2,2)*.5:1-Math.abs(Math.pow(2-e*2,2))*.5},cubicInOut:function(e){return e<.5?Math.pow(e*2,3)*.5:1-Math.abs(Math.pow(2-e*2,3))*.5},quartInOut:function(e){return e<.5?Math.pow(e*2,4)*.5:1-Math.abs(Math.pow(2-e*2,4))*.5},quintInOut:function(e){return e<.5?Math.pow(e*2,5)*.5:1-Math.abs(Math.pow(2-e*2,5))*.5}}}).call(this);


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.