Skip to content
Navigation Menu
{{ message }}
forked from shrekshrek/css3d-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss3d.min.js
More file actions
9 lines (8 loc) · 12.2 KB
/
Copy pathcss3d.min.js
File metadata and controls
9 lines (8 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
/*!
* VERSION: 0.7.0
* DATE: 2015-12-20
* GIT:https://github.com/shrekshrek/css3d-engine
*
* @author: Shrek.wang, shrekshrek@gmail.com
**/
!function(t){var i="object"==typeof self&&self.self==self&&self||"object"==typeof global&&global.global==global&&global;"function"==typeof define&&define.amd?define(["exports"],function(e){i.C3D=t(i,e)}):"undefined"!=typeof exports?t(i,exports):i.C3D=t(i,{})}(function(t,i){function e(t){return Math.round(t)}function s(t){return t.replace(/\b(\w)|\s(\w)/g,function(t){return t.toUpperCase()})}function n(t){var e;switch(t.type){case"sprite":e=new i.Sprite(t.el?{el:t.el}:void 0);break;case"plane":e=new i.Plane(t.el?{el:t.el}:void 0);break;case"cube":e=new i.Cube(t.el?{el:t.el}:void 0)}if(void 0!=t.size&&e.size.apply(e,t.size),void 0!=t.position&&e.position.apply(e,t.position),void 0!=t.rotation&&e.rotation.apply(e,t.rotation),void 0!=t.scale&&e.scale.apply(e,t.scale),void 0!=t.origin&&e.origin.apply(e,t.origin),void 0!=t.visibility&&e.visibility.apply(e,t.visibility),void 0!=t.material&&e.material.apply(e,t.material),void 0!=t.filter&&e.filter.apply(e,t.filter),void 0!=t.name&&e.name.apply(e,[t.name]),e.update(),t.children)for(var s=0,r=t.children.length;r>s;s++){var h=t.children[s],a=n(h);e.addChild(a)}return e}var r=function(t){var i=[];for(var e in t)i.push(e);return i},h=function(t){var i=arguments.length;if(2>i||null==t)return t;for(var e=1;i>e;e++)for(var s=arguments[e],n=r(s),h=n.length,a=0;h>a;a++){var o=n[a];t[o]=s[o]}return t},a=function(t,i){var e,s=this;e=t&&Object.prototype.hasOwnProperty.call(t,"constructor")?t.constructor:function(){return s.apply(this,arguments)},h(e,s,i);var n=function(){this.constructor=e};return n.prototype=s.prototype,e.prototype=new n,t&&h(e.prototype,t),e.__super__=s.prototype,e},o="";return function(){var t=document.createElement("div"),i=["Webkit","Moz","Ms","O"];for(var e in i)if(i[e]+"Transform"in t.style){o=i[e];break}}(),i.getRandomColor=function(){return"#"+("00000"+(16777216*Math.random()<<0).toString(16)).slice(-6)},i.rgb2hex=function(t,i,e){return(t<<16|i<<8|e).toString(16)},i.hex2rgb=function(t){var i=Math.floor("0x"+t),e=i>>16&255,s=i>>8&255,n=255&i;return[e,s,n]},i.Object=function(){this.initialize.apply(this,arguments)},h(i.Object.prototype,{x:0,y:0,z:0,position:function(t,i,e){switch(arguments.length){case 1:this.x=t,this.y=t,this.z=t;break;case 2:this.x=t,this.y=i;break;case 3:this.x=t,this.y=i,this.z=e}return this},move:function(t,i,e){switch(arguments.length){case 1:this.x+=t,this.y+=t,this.z+=t;break;case 2:this.x+=t,this.y+=i;break;case 3:this.x+=t,this.y+=i,this.z+=e}return this},rotationX:0,rotationY:0,rotationZ:0,rotation:function(t,i,e){switch(arguments.length){case 1:this.rotationX=t,this.rotationY=t,this.rotationZ=t;break;case 2:this.rotationX=t,this.rotationY=i;break;case 3:this.rotationX=t,this.rotationY=i,this.rotationZ=e}return this},rotate:function(t,i,e){switch(arguments.length){case 1:this.rotationX+=t,this.rotationY+=t,this.rotationZ+=t;break;case 2:this.rotationX+=t,this.rotationY+=i;break;case 3:this.rotationX+=t,this.rotationY+=i,this.rotationZ+=e}return this},scaleX:1,scaleY:1,scaleZ:1,scale:function(t,i,e){switch(arguments.length){case 1:this.scaleX=t,this.scaleY=t,this.scaleZ=t;break;case 2:this.scaleX=t,this.scaleY=i;break;case 3:this.scaleX=t,this.scaleY=i,this.scaleZ=e}return this},width:0,height:0,depth:0,size:function(t,i,e){switch(arguments.length){case 1:this.width=t,this.height=t,this.depth=t;break;case 2:this.width=t,this.height=i;break;case 3:this.width=t,this.height=i,this.depth=e}return this},originX:0,originY:0,originZ:0,_orgO:{x:0,y:0,z:0},_orgT:{x:0,y:0,z:0},_orgF:{x:0,y:0,z:0},origin:function(t,i,e){switch(arguments.length){case 1:this.originX=t,this.originY=t,this.originZ=t;break;case 2:this.originX=t,this.originY=i;break;case 3:this.originX=t,this.originY=i,this.originZ=e}return this},_name:"",name:function(t){return this._name=t,""==t?delete this.el.dataset.name:this.el.dataset.name=t,this},initialize:function(){this.x=0,this.y=0,this.z=0,this.rotationX=0,this.rotationY=0,this.rotationZ=0,this.scaleX=1,this.scaleY=1,this.scaleZ=1,this.width=0,this.height=0,this.depth=0,this.originX="50%",this.originY="50%",this.originZ="0px",this._orgO={x:"50%",y:"50%",z:"0px"},this._orgT={x:"-50%",y:"-50%",z:"0px"},this._orgF={x:0,y:0,z:0},this.children=[],this._name=""},parent:null,children:null,addChild:function(t){if(null!=t.parent&&t.parent.removeChild(t),""!=t._name){if(void 0!==this[t._name])throw t._name+" already exist!";this[t._name]=t}return this.children.push(t),t.parent=this,this},removeChild:function(t){for(var i=this.children.length-1;i>=0;i--)if(this.children[i]===t)return""!=t._name&&delete this[t._name],this.children.splice(i,1),t.parent=null,this;return this},removeAllChild:function(){for(var t=this.children.length-1;t>=0;t--){var i=this.children[t];""!=i._name&&delete this[i._name],i.parent=null}return this.children=[],this},remove:function(){return null!=this.parent&&this.parent.removeChild(this),this}}),i.Object.extend=a,i.Sprite=i.Object.extend({el:null,alpha:1,visible:!0,mat:null,initialize:function(t){i.Sprite.__super__.initialize.apply(this,[t]),this.alpha=1,this.visible=!0;var e;if(t&&t.el)switch(typeof t.el){case"string":e=document.createElement("div"),e.innerHTML=t.el;break;case"object":1===t.el.nodeType&&(e=t.el)}e||(e=document.createElement("div")),e.style.position="absolute",e.style[o+"Transform"]="translateZ(0px)",e.style[o+"TransformStyle"]="preserve-3d",this.el=e,e.le=this},update:function(){return this.updateS(),this.updateM(),this.updateO(),this.updateT(),this.updateV(),this},updateS:function(){return this},updateM:function(){if(!this.mat)return this;for(var t in this.mat)switch(t){case"bothsides":this.el.style[o+"BackfaceVisibility"]=this.mat[t]?"visible":"hidden";break;case"image":this.el.style["background"+s(t)]=""!==this.mat[t]?"url("+this.mat[t]+")":"";break;default:this.el.style["background"+s(t)]=this.mat[t]}return this},updateO:function(){if("number"==typeof this.originX){var t=this.originX-this._orgF.x;this._orgO.x=t+"px",this._orgT.x=-t+"px"}else this._orgO.x=this.originX,this._orgT.x="-"+this.originX;if("number"==typeof this.originY){var i=this.originY-this._orgF.y;this._orgO.y=i+"px",this._orgT.y=-i+"px"}else this._orgO.y=this.originY,this._orgT.y="-"+this.originY;if("number"==typeof this.originZ){var e=this.originZ-this._orgF.z;this._orgO.z=e+"px",this._orgT.z=-e+"px"}else this._orgO.z=this._orgT.z="0px";return this.el.style[o+"TransformOrigin"]=this._orgO.x+" "+this._orgO.y+" "+this._orgO.z,this},updateT:function(){return this.el.style[o+"Transform"]="translate3d("+this._orgT.x+", "+this._orgT.y+", "+this._orgT.z+") translate3d("+this.x+"px,"+this.y+"px,"+this.z+"px) rotateX("+this.rotationX%360+"deg) rotateY("+this.rotationY%360+"deg) rotateZ("+this.rotationZ%360+"deg) scale3d("+this.scaleX+", "+this.scaleY+", "+this.scaleZ+") ",this},updateV:function(){return this.el.style.opacity=this.alpha,this.el.style.display=this.visible?"block":"none",this},addChild:function(t){return i.Sprite.__super__.addChild.apply(this,[t]),this.el&&t.el&&this.el.appendChild(t.el),this},removeChild:function(t){for(var i=this.children.length-1;i>=0;i--)if(this.children[i]===t)return""!=t._name&&delete this[t._name],this.children.splice(i,1),t.parent=null,this.el.removeChild(t.el),this;return this},removeAllChild:function(){for(var t=this.children.length-1;t>=0;t--){var i=this.children[t];""!=i._name&&delete this[i._name],i.parent=null,this.el.removeChild(i.el)}return this.children=[],this},on:function(t){if("object"==typeof t)for(var i in t)this.el.addEventListener(i,t[i],!1);else 2===arguments.length?this.el.addEventListener(arguments[0],arguments[1],!1):3===arguments.length&&this.el.addEventListener(arguments[0],arguments[1],arguments[2]);return this},off:function(t){if("object"==typeof t)for(var i in t)this.el.removeEventListener(i,t[i],!1);else 2===arguments.length&&this.el.removeEventListener(arguments[0],arguments[1],!1);return this},buttonMode:function(t){return this.el.style.cursor=t?"pointer":"auto",this},material:function(t){return this.mat=t,this},visibility:function(t){return void 0!==t.visible&&(this.visible=t.visible),void 0!==t.alpha&&(this.alpha=t.alpha),this}}),i.Stage=i.Sprite.extend({camera:null,fov:null,__rfix:null,__pfix:null,initialize:function(t){i.Stage.__super__.initialize.apply(this,[t]),t&&t.el||(this.el.style.top="0px",this.el.style.left="0px",this.el.style.width="0px",this.el.style.height="0px"),this.el.style[o+"Perspective"]="800px",this.el.style[o+"TransformStyle"]="flat",this.el.style[o+"Transform"]="",this.el.style.overflow="hidden",this.__rfix=new i.Sprite,this.el.appendChild(this.__rfix.el),this.__pfix=new i.Sprite,this.__rfix.el.appendChild(this.__pfix.el),this.setCamera(new i.Camera)},updateS:function(){return this.el.style.width=e(this.width)+"px",this.el.style.height=e(this.height)+"px",this},updateT:function(){return this.fov=e(.5/Math.tan(.5*this.camera.fov/180*Math.PI)*this.height),this.el.style[o+"Perspective"]=this.fov+"px",this.__rfix.position(e(this.width/2),e(this.height/2),this.fov).rotation(-this.camera.rotationX,-this.camera.rotationY,-this.camera.rotationZ).updateT(),this.__pfix.position(-this.camera.x,-this.camera.y,-this.camera.z).updateT(),this},addChild:function(t){return this.__pfix.addChild(t),this},removeChild:function(t){return this.__pfix.removeChild(t),this},setCamera:function(t){return this.camera&&(this.camera.stage=null),this.camera=t,this.camera.stage=this,this}}),i.Camera=i.Object.extend({fov:null,stage:null,initialize:function(t){i.Camera.__super__.initialize.apply(this,[t]),this.fov=75},update:function(){return this.updateT(),this},updateS:function(){return this},updateM:function(){return this},updateT:function(){return this.stage&&this.stage.updateT(),this},updateV:function(){return this}}),i.Plane=i.Sprite.extend({flt:null,initialize:function(t){i.Plane.__super__.initialize.apply(this,[t])},update:function(){return i.Plane.__super__.update.apply(this),this.updateF(),this},updateS:function(){return this.el.style.width=e(this.width)+"px",this.el.style.height=e(this.height)+"px",this},updateF:function(){if(!this.flt)return this;var t="";for(var i in this.flt)t+=""!==this.flt[i]?i+"("+this.flt[i].join(",")+")":"";return""!==t&&(this.el.style[o+"Filter"]=t),this},filter:function(t){return this.flt=t,this}}),i.Cube=i.Sprite.extend({front:null,back:null,left:null,right:null,up:null,down:null,flt:null,initialize:function(t){i.Cube.__super__.initialize.apply(this,[t]),this.front=new i.Plane,this.addChild(this.front),this.back=new i.Plane,this.addChild(this.back),this.left=new i.Plane,this.addChild(this.left),this.right=new i.Plane,this.addChild(this.right),this.up=new i.Plane,this.addChild(this.up),this.down=new i.Plane,this.addChild(this.down)},update:function(){return i.Cube.__super__.update.apply(this),this.updateF(),this},updateS:function(){var t=e(this.width),i=e(this.height),s=e(this.depth);return this._orgF.x=this.width/2,this._orgF.y=this.height/2,this._orgF.z=this.depth/2,this.front.size(t,i,0).position(0,0,-s/2).rotation(0,0,0).updateS().updateT(),this.back.size(t,i,0).position(0,0,s/2).rotation(0,180,0).updateS().updateT(),this.left.size(s,i,0).position(-t/2,0,0).rotation(0,90,0).updateS().updateT(),this.right.size(s,i,0).position(t/2,0,0).rotation(0,-90,0).updateS().updateT(),this.up.size(t,s,0).position(0,-i/2,0).rotation(-90,0,0).updateS().updateT(),this.down.size(t,s,0).position(0,i/2,0).rotation(90,0,0).updateS().updateT(),this},updateM:function(){if(!this.mat)return this;for(var t in this.mat)switch(t){case"front":case"back":case"left":case"right":case"up":case"down":this[t].material({image:this.mat[t]}).updateM();break;default:this.front.material(this.mat).updateM(),this.back.material(this.mat).updateM(),this.left.material(this.mat).updateM(),this.right.material(this.mat).updateM(),this.up.material(this.mat).updateM(),this.down.material(this.mat).updateM()}return this},updateF:function(){return this.flt?(this.front.filter(this.flt).updateF(),this.back.filter(this.flt).updateF(),this.left.filter(this.flt).updateF(),this.right.filter(this.flt).updateF(),this.up.filter(this.flt).updateF(),this.down.filter(this.flt).updateF(),this):this},filter:function(t){return this.flt=t,this}}),i.create=function(t){var i;switch(typeof t){case"array":i={type:"sprite",children:t};break;case"object":i=t;break;default:return}return n(i)},i});
You can’t perform that action at this time.
