js シミュレーション 3D シーン エフェクト コード パッケージ化_javascript スキル
2 次元空間で 3 次元効果をシミュレートするには、3 次元座標を 2 次元座標に変換する必要があります。最も基本的な基礎の 1 つは、何かが遠くにあるほどサイズが小さくなり、座標が消失点に近づくというものです。
遠近法:
スケール = fl / (fl z);
スケールは 0.0 から 1.0 までのサイズの比率であり、fl は観測点から撮像面までの距離であり、通常この値は固定、z はオブジェクトの 3 次元空間の Z 軸です。
これらのコードを記述する前に、私はオブジェクト指向を使用して記述することを好みます。シーンはさまざまなオブジェクトを収容できる空間です。オブジェクトは x です。 、y、z の 3 次元では、任意の数のオブジェクトをシーンに挿入でき、オブジェクトはその座標値に基づいてシーン内の特定の位置に表示されます。オブジェクトの位置。
一部のデモでは、マウスの動きとスクロール ホイールを使用して制御してください。
効果 1
[Ctrl A すべて選択 注: 外部 Js を導入する必要がある場合は、更新して実行する必要があります
効果2 外部 Js を導入する必要がある場合は、更新して実行する必要があります
]
[Ctrl A すべて選択 注:
外部 Js を導入する必要がある場合は、更新して実行する必要があります
] <script> void function(window){ var document = window.document; var debug = document.getElementById('debug'); function ObjtoStr(obj){ var arr = []; for(var i in obj){ if(isNaN(obj[i])) continue; arr.push(i + ':' + obj[i]); } return arr.join('; '); } function getElementOffset(element){ var left = 0, top = 0; do{ left += element.offsetLeft; top += element.offsetTop; }while(element = element.offsetParent); return { left:left, top:top }; } function getMouseOffset(event){ return { x:(event.pageX || event.clientX + document.body.scrollLeft - document.body.clientLeft), y:(event.pageY || event.clientY + document.body.scrollTop - document.body.clientTop) }; } function addEventListener(element,type,fun){ if(element.addEventListener){ element.addEventListener(type,function(event){ fun(event); },false); }else{ element.attachEvent('on'+type,function(){ fun(window.event); }); } } function extend(subClass,supClass){ var fun = function(){}, prototype = subClass.prototype; fun.prototype = supClass.prototype; subClass.prototype = new fun(); for(var i in prototype){ subClass.prototype[i] = prototype[i]; } subClass.$supClass = supClass; subClass.prototype.$supClass = function(){ var supClass = arguments.callee.caller.$supClass; if(typeof supClass == 'function'){ supClass.apply(this,arguments); this.$supClass = supClass; } }; subClass.prototype.constructor = subClass; return subClass; } /** * WH类,高宽 */ function WH(w,h){ this.w = w; this.h = h; } WH.prototype = { clone:function(){ return new WH(this.w,this.h); } }; /** * xyz坐标类 * */ function XYZ(x,y,z){ this.x = x; this.y = y; this.z = z; } XYZ.prototype = { clone:function(){ return new XYZ(this.x,this.y,this.z); } }; /** * 场景类 */ function Scene(options){ //属性 //dom this.element = null; //场景距离 this.fl = 500; this.wh = null; //基准z轴 this.baseZ = 0; //中心消失点坐标 this.cX = 0; this.cY = 0; //中心消失点便宜 this.cXl = 0; this.cYl = 0; //偏移系数 this.ce = 1; this.ThingList = []; this.setOption(options); this.init(); } Scene.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'element': this[i] = typeof options[i] == 'string' ? document.getElementById(options[i]) : options[i]; break; } } }, init:function(){ if(!this.element) throw new Error(90,'not box'); this.wh = new WH(this.element.clientWidth,this.element.clientHeight); this.bindEvent(); }, addThing:function(/* Thing */ thing){ this.ThingList.push(thing); this.calcPosition(thing); this.element.appendChild(thing.getElement(this)); }, //计算位置及大小 calcPosition:function(/*Thing*/ thing){ this.cX = this.element.clientWidth/2; this.cY = this.element.clientHeight/2; scale = this.fl/(this.fl + thing.xyz.z+this.baseZ); if(scale <= 0){ thing.element.style.display = 'none'; return ; }else{ thing.element.style.display = ''; } thing.element.style.width = thing.wh.w * scale + 'px'; thing.element.style.height = thing.wh.h * scale + 'px'; thing.element.style.top = (this.cY + ((thing.xyz.y+this.cYl-this.cY) * scale)) + 'px'; thing.element.style.left = (this.cX + ((thing.xyz.x+this.cXl-this.cX) * scale)) + 'px'; thing.element.style.zIndex = Math.round(scale*1000); if(thing.isOpacity){ thing.element.style.opacity = Math.min(scale*4.5,1); thing.element.style.filter = 'alpha(opacity='+(Math.min(scale*4.5,1) * 100)+')'; } }, bindEvent:function(){ var self = this; addEventListener(this.element,'mousemove',function(event){ self.onMouseMove(event); }); var mousewheel = navigator.userAgent.indexOf('Firefox') > 0 ? 'DOMMouseScroll' : 'mousewheel'; addEventListener(this.element,mousewheel,function(event){ self.onMouseWheel(event); }); }, //在场景内移动事件 onMouseMove:function(event){ //场景的页面坐标 var po = getElementOffset(this.element); //鼠标光标的页面坐标 var ev = getMouseOffset(event); //场景内坐标 var x = ev.x-po.left; var y = ev.y-po.top; //中间消失点的坐标偏移差 this.cXl = (this.element.clientWidth/2 - x) * this.ce; this.cYl = (this.element.clientHeight/2 - y) * this.ce; this.reDraw(); }, onMouseWheel:function(event){ var code = event.wheelDelta || -event.detail; if(code > 0){ this.baseZ -= 200; }else{ this.baseZ += 200; } this.reDraw(); }, reDraw:function(){ for(var i=0 ; i<this.ThingList.length;i++){ this.calcPosition(this.ThingList[i]); } } }; /** * 物件抽象类 */ function Thing(options){ this.scene = null; this.wh = new WH(10,10); this.xyz = new XYZ(10,10,0); this.element = null; this.isOpacity = true; this.setOption(options); this.init(); } Thing.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'wh': case 'xyz': case 'isOpacity': this[i] = options[i]; break; default: break; } } }, init:function(){ this.element = this.draw(); this.element.style.position = 'absolute'; this.element.style.width = this.wh.w + 'px'; this.element.style.height = this.wh.h + 'px'; }, draw:function(){ throw new Error(998,'method do not realize!'); }, getElement:function(/*Scene*/ scene){ this.scene = scene; return this.element; } }; function Diam(options){ this.$supClass(options); } Diam.prototype = { draw:function(){ var img = document.createElement('img'); loadimg = img.cloneNode(true); loadimg.onload = function(){ self.wh = new WH(this.width,this.height); } img.src = [ '/upload/201201/20120105103758227.jpg', '/upload/201201/20120105103801969.jpg', '/upload/201201/20120105103801207.jpg', '/upload/201201/20120105103801956.jpg', '/upload/201201/20120105103801732.jpg', '/upload/201201/20120105103801346.jpg', '/upload/201201/20120105103801362.jpg' ][Math.round(Math.random()*6)]; return img; } }; extend(Diam,Thing); function Sky(options){ this.$supClass(options); } Sky.prototype = { draw:function(){ var img = document.createElement('img'); img.src = [ '/upload/201201/20120105103801314.jpg', '/upload/201201/20120105103803325.jpg', '/upload/201201/20120105103803314.jpg', '/upload/201201/20120105103803146.jpg' ][Math.round(Math.random()*3)]; return img; } }; extend(Sky,Thing); var scene = new Scene({ 'element':'box' }); for(var i= 0 ; i < 50 ;i++){ scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(Math.random() * document.body.clientWidth,Math.random() *document.body.clientHeight,Math.random() *8000) })); } scene.addThing(new Sky({ wh:new WH(160000,120000), xyz:new XYZ(-80000,-60000,54000), isOpacity:false })); }(window); </script>[Ctrl A すべて選択 注: <script> void function(window){ var document = window.document; var debug = document.getElementById('debug'); function ObjtoStr(obj){ var arr = []; for(var i in obj){ if(isNaN(obj[i])) continue; arr.push(i + ':' + obj[i]); } return arr.join('; '); } function getElementOffset(element){ var left = 0, top = 0; do{ left += element.offsetLeft; top += element.offsetTop; }while(element = element.offsetParent); return { left:left, top:top }; } function getMouseOffset(event){ return { x:(event.pageX || event.clientX + document.body.scrollLeft - document.body.clientLeft), y:(event.pageY || event.clientY + document.body.scrollTop - document.body.clientTop) }; } function addEventListener(element,type,fun){ if(element.addEventListener){ element.addEventListener(type,function(event){ fun(event); },false); }else{ element.attachEvent('on'+type,function(){ fun(window.event); }); } } function extend(subClass,supClass){ var fun = function(){}, prototype = subClass.prototype; fun.prototype = supClass.prototype; subClass.prototype = new fun(); for(var i in prototype){ subClass.prototype[i] = prototype[i]; } subClass.$supClass = supClass; subClass.prototype.$supClass = function(){ var supClass = arguments.callee.caller.$supClass; if(typeof supClass == 'function'){ supClass.apply(this,arguments); this.$supClass = supClass; } }; subClass.prototype.constructor = subClass; return subClass; } /** * WH类,高宽 */ function WH(w,h){ this.w = w; this.h = h; } WH.prototype = { clone:function(){ return new WH(this.w,this.h); } }; /** * xyz坐标类 * */ function XYZ(x,y,z){ this.x = x; this.y = y; this.z = z; } XYZ.prototype = { clone:function(){ return new XYZ(this.x,this.y,this.z); } }; /** * 场景类 */ function Scene(options){ //属性 //dom this.element = null; //场景距离 this.fl = 500; this.wh = null; //基准z轴 this.baseZ = 0; //中心消失点坐标 this.cX = 0; this.cY = 0; //中心消失点便宜 this.cXl = 0; this.cYl = 0; //偏移系数 this.ce = 5; this.ThingList = []; this.setOption(options); this.init(); } Scene.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'element': this[i] = typeof options[i] == 'string' ? document.getElementById(options[i]) : options[i]; break; } } }, init:function(){ if(!this.element) throw new Error(90,'not box'); this.wh = new WH(this.element.clientWidth,this.element.clientHeight); this.bindEvent(); }, addThing:function(/* Thing */ thing){ this.ThingList.push(thing); this.calcPosition(thing); this.element.appendChild(thing.getElement(this)); }, //计算位置及大小 calcPosition:function(/*Thing*/ thing){ this.cX = this.element.clientWidth/2; this.cY = this.element.clientHeight/2; scale = this.fl/(this.fl + thing.xyz.z+this.baseZ); if(scale <= 0){ thing.element.style.display = 'none'; return ; }else{ thing.element.style.display = ''; } thing.element.style.width = thing.wh.w * scale + 'px'; thing.element.style.height = thing.wh.h * scale + 'px'; thing.element.style.top = (this.cY + ((thing.xyz.y+this.cYl-this.cY) * scale)) + 'px'; thing.element.style.left = (this.cX + ((thing.xyz.x+this.cXl-this.cX) * scale)) + 'px'; thing.element.style.zIndex = Math.round(scale*1000); if(thing.isOpacity){ thing.element.style.opacity = Math.min(scale*4.5,1); thing.element.style.filter = 'alpha(opacity='+(Math.min(scale*4.5,1) * 100)+')'; } }, bindEvent:function(){ var self = this; addEventListener(this.element,'mousemove',function(event){ self.onMouseMove(event); }); var mousewheel = navigator.userAgent.indexOf('Firefox') > 0 ? 'DOMMouseScroll' : 'mousewheel'; addEventListener(this.element,mousewheel,function(event){ self.onMouseWheel(event); }); }, //在场景内移动事件 onMouseMove:function(event){ //场景的页面坐标 var po = getElementOffset(this.element); //鼠标光标的页面坐标 var ev = getMouseOffset(event); //场景内坐标 var x = ev.x-po.left; var y = ev.y-po.top; //中间消失点的坐标偏移差 this.cXl = (this.element.clientWidth/2 - x) * this.ce; this.cYl = (this.element.clientHeight/2 - y) * this.ce; this.reDraw(); }, onMouseWheel:function(event){ var code = event.wheelDelta || -event.detail; if(code > 0){ this.baseZ -= 200; }else{ this.baseZ += 200; } this.reDraw(); }, reDraw:function(){ for(var i=0 ; i<this.ThingList.length;i++){ this.calcPosition(this.ThingList[i]); } } }; /** * 物件抽象类 */ function Thing(options){ this.scene = null; this.wh = new WH(10,10); this.xyz = new XYZ(10,10,0); this.element = null; this.isOpacity = true; this.setOption(options); this.init(); } Thing.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'wh': case 'xyz': case 'isOpacity': this[i] = options[i]; break; default: break; } } }, init:function(){ this.element = this.draw(); this.element.style.position = 'absolute'; this.element.style.width = this.wh.w + 'px'; this.element.style.height = this.wh.h + 'px'; }, draw:function(){ throw new Error(998,'method do not realize!'); }, getElement:function(/*Scene*/ scene){ this.scene = scene; return this.element; } }; function Diam(options){ this.$supClass(options); } Diam.prototype = { draw:function(){ var img = document.createElement('img'); loadimg = img.cloneNode(true); loadimg.onload = function(){ self.wh = new WH(this.width,this.height); } img.src = [ '/upload/201201/20120105103758227.jpg', '/upload/201201/20120105103801969.jpg', '/upload/201201/20120105103801207.jpg', '/upload/201201/20120105103801956.jpg', '/upload/201201/20120105103801732.jpg', '/upload/201201/20120105103801346.jpg', '/upload/201201/20120105103801362.jpg' ][Math.round(Math.random()*6)]; return img; } }; extend(Diam,Thing); function Sky(options){ this.$supClass(options); } Sky.prototype = { draw:function(){ var img = document.createElement('img'); img.src = [ '/upload/201201/20120105103801314.jpg', '/upload/201201/20120105103803325.jpg', '/upload/201201/20120105103803314.jpg', '/upload/201201/20120105103803146.jpg' ][Math.round(Math.random()*3)]; return img; } }; extend(Sky,Thing); var scene = new Scene({ 'element':'box' }); for(var i= 0,x,z ; i < 50 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+500; y = (Math.cos(Math.PI*2*(i/50)) * 1000)+500; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,3000) })); } for(var i= 0,x,z ; i < 50 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+500; z = (Math.cos(Math.PI*2*(i/50)) * 1000)+3000; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,document.body.clientHeight/2+200,z) })); } scene.addThing(new Sky({ wh:new WH(160000,120000), xyz:new XYZ(-80000,-60000,54000), isOpacity:false })); }(window); </script>外部 Js を導入する必要がある場合は、<script> void function(window){ /** * by Od 2011/12/25 */ var document = window.document; var debug = document.getElementById('debug'); function ObjtoStr(obj){ var arr = []; for(var i in obj){ if(isNaN(obj[i])) continue; arr.push(i + ':' + obj[i]); } return arr.join('; '); } function getElementOffset(element){ var left = 0, top = 0; do{ left += element.offsetLeft; top += element.offsetTop; }while(element = element.offsetParent); return { left:left, top:top }; } function getMouseOffset(event){ return { x:(event.pageX || event.clientX + document.body.scrollLeft - document.body.clientLeft), y:(event.pageY || event.clientY + document.body.scrollTop - document.body.clientTop) }; } function addEventListener(element,type,fun){ if(element.addEventListener){ element.addEventListener(type,function(event){ fun(event); },false); }else{ element.attachEvent('on'+type,function(){ fun(window.event); }); } } function extend(subClass,supClass){ var fun = function(){}, prototype = subClass.prototype; fun.prototype = supClass.prototype; subClass.prototype = new fun(); for(var i in prototype){ subClass.prototype[i] = prototype[i]; } subClass.$supClass = supClass; subClass.prototype.$supClass = function(){ var supClass = arguments.callee.caller.$supClass; if(typeof supClass == 'function'){ supClass.apply(this,arguments); this.$supClass = supClass; } }; subClass.prototype.constructor = subClass; return subClass; } /** * WH类,高宽 */ function WH(w,h){ this.w = w; this.h = h; } WH.prototype = { clone:function(){ return new WH(this.w,this.h); } }; /** * xyz坐标类 * */ function XYZ(x,y,z){ this.x = x; this.y = y; this.z = z; } XYZ.prototype = { clone:function(){ return new XYZ(this.x,this.y,this.z); } }; /** * 场景类 */ function Scene(options){ //属性 //dom this.element = null; //场景距离 this.fl = 500; this.wh = null; //基准z轴 this.baseZ = 0; //中心消失点坐标 this.cX = 0; this.cY = 0; //中心消失点便宜 this.cXl = 0; this.cYl = 0; //偏移系数 this.ce = 1; this.ThingList = []; this.setOption(options); this.init(); } Scene.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'element': this[i] = typeof options[i] == 'string' ? document.getElementById(options[i]) : options[i]; break; } } }, init:function(){ if(!this.element) throw new Error(90,'not box'); this.wh = new WH(this.element.clientWidth,this.element.clientHeight); this.bindEvent(); }, addThing:function(/* Thing */ thing){ this.ThingList.push(thing); this.calcPosition(thing); this.element.appendChild(thing.getElement(this)); }, //计算位置及大小 calcPosition:function(/*Thing*/ thing){ this.cX = this.element.clientWidth/2; this.cY = this.element.clientHeight/2; scale = this.fl/(this.fl + thing.xyz.z+this.baseZ); if(scale <= 0){ thing.element.style.display = 'none'; return ; }else{ thing.element.style.display = ''; } thing.element.style.width = thing.wh.w * scale + 'px'; thing.element.style.height = thing.wh.h * scale + 'px'; thing.element.style.top = (this.cY + ((thing.xyz.y+this.cYl-this.cY) * scale)) + 'px'; thing.element.style.left = (this.cX + ((thing.xyz.x+this.cXl-this.cX) * scale)) + 'px'; thing.element.style.zIndex = Math.round(scale*1000); thing.element.style.opacity = Math.min(scale*4.5,1); thing.element.style.filter = 'alpha(opacity='+(Math.min(scale*4.5,1) * 100)+')'; }, bindEvent:function(){ var self = this; addEventListener(this.element,'mousemove',function(event){ self.onMouseMove(event); }); var mousewheel = navigator.userAgent.indexOf('Firefox') > 0 ? 'DOMMouseScroll' : 'mousewheel'; addEventListener(this.element,mousewheel,function(event){ self.onMouseWheel(event); }); setInterval(function(){ self.onEnterFrame(); },40); }, //在场景内移动事件 onMouseMove:function(event){ //场景的页面坐标 var po = getElementOffset(this.element); //鼠标光标的页面坐标 var ev = getMouseOffset(event); //场景内坐标 var x = ev.x-po.left; var y = ev.y-po.top; //中间消失点的坐标偏移差 this.cXl = (this.element.clientWidth/2 - x) * this.ce; this.cYl = (this.element.clientHeight/2 - y) * this.ce; this.reDraw(); }, onMouseWheel:function(event){ var code = event.wheelDelta || -event.detail; if(code > 0){ this.baseZ -= 200; }else{ this.baseZ += 200; } this.reDraw(); }, onEnterFrame:function(){ var thing; for(var i=0; i<this.ThingList.length;i++){ thing = this.ThingList[i]; if(thing.isstatic) continue; if(thing.xyz.y+1 >this.wh.h){ thing.xyz.y = 0; }else{ thing.xyz.y += 20; } this.calcPosition(thing); } }, reDraw:function(){ for(var i=0 ; i<this.ThingList.length;i++){ this.calcPosition(this.ThingList[i]); } } }; /** * 物件抽象类 */ function Thing(options){ this.scene = null; this.wh = null; this.xyz = new XYZ(10,10,0); this.element = null; this.isstatic = false; this.setOption(options); this.init(); } Thing.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'wh': case 'xyz': case 'isstatic': this[i] = options[i]; break; default: break; } } }, init:function(){ this.element = this.draw(); this.element.style.position = 'absolute'; this.element.style.width = this.wh.w + 'px'; this.element.style.height = this.wh.h + 'px'; }, draw:function(){ throw new Error(998,'method do not realize!'); }, getElement:function(/*Scene*/ scene){ this.scene = scene; return this.element; } }; function Snowflake(options){ this.$supClass(options); } Snowflake.prototype = { draw:function(){ var img = document.createElement('img'),self = this; loadimg = img.cloneNode(true); loadimg.onload = function(){ //self.wh = new WH(this.width,this.height); } img.src = loadimg.src = [ '/upload/201201/20120105103804884.gif', '/upload/201201/20120105103804792.gif', '/upload/201201/20120105103804222.gif', '/upload/201201/20120105103804213.gif', '/upload/201201/20120105103804180.gif', '/upload/201201/20120105103804588.gif' ][Math.round(Math.random()*5)]; return img; } }; extend(Snowflake,Thing); var scene = new Scene({ 'element':'box' }); function tree(options){ this.$supClass(options); } tree.prototype = { draw:function(){ var img = document.createElement('img'),self = this; img.src = '/upload/201201/20120105103804497.gif'; return img; } }; extend(tree,Thing); for(var i= 0,x,z ; i < 100 ;i++){ scene.addThing(new Snowflake({ wh:new WH(50,50), xyz:new XYZ(Math.round(Math.random()*document.body.clientWidth),Math.round(Math.random()*document.body.clientHeight),Math.round(Math.random()*6000-1000)) })); } for(var i= 0,x,z ; i < 80 ;i++){ scene.addThing(new tree({ wh:new WH(500,800), isstatic:true, xyz:new XYZ(Math.round(Math.random()*document.body.clientWidth),Math.round(document.body.clientHeight),Math.round(Math.random()*6000-1000)) })); } for(var i= 0,x,z ; i < 80 ;i++){ scene.addThing(new tree({ wh:new WH(500,800), isstatic:true, xyz:new XYZ(Math.round(Math.random()*document.body.clientWidth*20),Math.round(document.body.clientHeight),Math.round(Math.random()*4000+3000)) })); } for(var i= 0,x,z ; i < 80 ;i++){ scene.addThing(new tree({ wh:new WH(500,800), isstatic:true, xyz:new XYZ(Math.round(Math.random()*-document.body.clientWidth*20),Math.round(document.body.clientHeight),Math.round(Math.random()*4000+3000)) })); } for(var i= 0,x,z ; i < 80 ;i++){ scene.addThing(new tree({ wh:new WH(500,800), isstatic:true, xyz:new XYZ(Math.round(Math.random()*-document.body.clientWidth*10),Math.round(document.body.clientHeight),Math.round(Math.random()*4000+1000)) })); } }(window); </script> を実行するために更新する必要があります]<script> void function(window){ var document = window.document; var debug = document.getElementById('debug'); function ObjtoStr(obj){ var arr = []; for(var i in obj){ if(isNaN(obj[i])) continue; arr.push(i + ':' + obj[i]); } return arr.join('; '); } function getElementOffset(element){ var left = 0, top = 0; do{ left += element.offsetLeft; top += element.offsetTop; }while(element = element.offsetParent); return { left:left, top:top }; } function getMouseOffset(event){ return { x:(event.pageX || event.clientX + document.body.scrollLeft - document.body.clientLeft), y:(event.pageY || event.clientY + document.body.scrollTop - document.body.clientTop) }; } function addEventListener(element,type,fun){ if(element.addEventListener){ element.addEventListener(type,function(event){ fun(event); },false); }else{ element.attachEvent('on'+type,function(){ fun(window.event); }); } } function extend(subClass,supClass){ var fun = function(){}, prototype = subClass.prototype; fun.prototype = supClass.prototype; subClass.prototype = new fun(); for(var i in prototype){ subClass.prototype[i] = prototype[i]; } subClass.$supClass = supClass; subClass.prototype.$supClass = function(){ var supClass = arguments.callee.caller.$supClass; if(typeof supClass == 'function'){ supClass.apply(this,arguments); this.$supClass = supClass; } }; subClass.prototype.constructor = subClass; return subClass; } /** * WH类,高宽 */ function WH(w,h){ this.w = w; this.h = h; } WH.prototype = { clone:function(){ return new WH(this.w,this.h); } }; /** * xyz坐标类 * */ function XYZ(x,y,z){ this.x = x; this.y = y; this.z = z; } XYZ.prototype = { clone:function(){ return new XYZ(this.x,this.y,this.z); } }; /** * 场景类 */ function Scene(options){ //属性 //dom this.element = null; //场景距离 this.fl = 500; this.wh = null; //基准z轴 this.baseZ = 0; //中心消失点坐标 this.cX = 0; this.cY = 0; //中心消失点便宜 this.cXl = 0; this.cYl = 0; //偏移系数 this.ce = 9; this.ThingList = []; this.setOption(options); this.init(); } Scene.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'element': this[i] = typeof options[i] == 'string' ? document.getElementById(options[i]) : options[i]; break; } } }, init:function(){ if(!this.element) throw new Error(90,'not box'); this.wh = new WH(this.element.clientWidth,this.element.clientHeight); this.bindEvent(); }, addThing:function(/* Thing */ thing){ this.ThingList.push(thing); this.calcPosition(thing); this.element.appendChild(thing.getElement(this)); }, //计算位置及大小 calcPosition:function(/*Thing*/ thing){ this.cX = this.element.clientWidth/2; this.cY = this.element.clientHeight/2; scale = this.fl/(this.fl + thing.xyz.z+this.baseZ); if(scale <= 0){ thing.element.style.display = 'none'; return ; }else{ thing.element.style.display = ''; } thing.element.style.width = thing.wh.w * scale + 'px'; thing.element.style.height = thing.wh.h * scale + 'px'; thing.element.style.top = (this.cY + ((thing.xyz.y+this.cYl-this.cY) * scale)) + 'px'; thing.element.style.left = (this.cX + ((thing.xyz.x+this.cXl-this.cX) * scale)) + 'px'; thing.element.style.zIndex = Math.round(scale*1000); if(thing.isOpacity){ thing.element.style.opacity = Math.min(scale*4.5,1); thing.element.style.filter = 'alpha(opacity='+(Math.min(scale*4.5,1) * 100)+')'; } }, bindEvent:function(){ var self = this; addEventListener(this.element,'mousemove',function(event){ self.onMouseMove(event); }); var mousewheel = navigator.userAgent.indexOf('Firefox') > 0 ? 'DOMMouseScroll' : 'mousewheel'; addEventListener(this.element,mousewheel,function(event){ self.onMouseWheel(event); }); }, //在场景内移动事件 onMouseMove:function(event){ //场景的页面坐标 var po = getElementOffset(this.element); //鼠标光标的页面坐标 var ev = getMouseOffset(event); //场景内坐标 var x = ev.x-po.left; var y = ev.y-po.top; //中间消失点的坐标偏移差 this.cXl = (this.element.clientWidth/2 - x) * this.ce; this.cYl = (this.element.clientHeight/2 - y) * this.ce; this.reDraw(); }, onMouseWheel:function(event){ var code = event.wheelDelta || -event.detail; if(code > 0){ this.baseZ -= 200; }else{ this.baseZ += 200; } this.reDraw(); }, reDraw:function(){ for(var i=0 ; i<this.ThingList.length;i++){ this.calcPosition(this.ThingList[i]); } } }; /** * 物件抽象类 */ function Thing(options){ this.scene = null; this.wh = new WH(10,10); this.xyz = new XYZ(10,10,0); this.element = null; this.isstats = false; this.isOpacity = true; this.setOption(options); this.init(); } Thing.prototype = { setOption:function(options){ for(var i in options){ switch(i){ case 'wh': case 'xyz': case 'isOpacity': this[i] = options[i]; break; default: break; } } }, init:function(){ this.element = this.draw(); this.element.style.position = 'absolute'; this.element.style.width = this.wh.w + 'px'; this.element.style.height = this.wh.h + 'px'; }, draw:function(){ throw new Error(998,'method do not realize!'); }, getElement:function(/*Scene*/ scene){ this.scene = scene; return this.element; } }; function Diam(options){ this.$supClass(options); } Diam.prototype = { draw:function(){ var img = document.createElement('img'); loadimg = img.cloneNode(true); loadimg.onload = function(){ self.wh = new WH(this.width,this.height); } img.src = [ '/upload/201201/20120105103758227.jpg', '/upload/201201/20120105103801969.jpg', '/upload/201201/20120105103801207.jpg', '/upload/201201/20120105103801956.jpg', '/upload/201201/20120105103801732.jpg', '/upload/201201/20120105103801346.jpg', '/upload/201201/20120105103801362.jpg' ][Math.round(Math.random()*6)]; return img; } }; extend(Diam,Thing); function Sky(options){ this.$supClass(options); } Sky.prototype = { draw:function(){ var img = document.createElement('img'); img.src = [ '/upload/201201/20120105103801314.jpg', '/upload/201201/20120105103803325.jpg', '/upload/201201/20120105103803314.jpg', '/upload/201201/20120105103803146.jpg' ][Math.round(Math.random()*3)]; return img; } }; extend(Sky,Thing); var scene = new Scene({ 'element':'box' }); for(var i= 20,x,z ; i < 40 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+500; y = (Math.cos(Math.PI*2*(i/50)) * 1000)+500; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,3000) })); } for(var i=11,x,z ; i < 31 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+1680; y = (Math.cos(Math.PI*2*(i/50)) * 1000)+500; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,3000) })); } for(var i=9,x,z ; i < 40 ;i++){ x = i*50 -890; y = i*60 + 200; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,3000) })); } for(var i=9,x,z ; i < 40 ;i++){ x = i*-50 +3090; y = i*60 + 200; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,3000) })); } for(var i= 20,x,z ; i < 40 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+500; y = (Math.cos(Math.PI*2*(i/50)) * 1000)+500; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,5000) })); } for(var i=11,x,z ; i < 31 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+1680; y = (Math.cos(Math.PI*2*(i/50)) * 1000)+500; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,5000) })); } for(var i=9,x,z ; i < 40 ;i++){ x = i*50 -890; y = i*60 + 200; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,5000) })); } for(var i=9,x,z ; i < 40 ;i++){ x = i*-50 +3090; y = i*60 + 200; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,y,5000) })); } scene.addThing(new Sky({ wh:new WH(160000,120000), xyz:new XYZ(-80000,-60000,54000), isOpacity:false })); /* for(var i= 0,x,z ; i < 50 ;i++){ x = (Math.sin(Math.PI*2*(i/50)) * 1000)+500; z = (Math.cos(Math.PI*2*(i/50)) * 1000)+3000; scene.addThing(new Diam({ wh:new WH(100,100), xyz:new XYZ(x,document.body.clientHeight/2+200,z) })); }*/ }(window); </script>

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











JavaScript文字列置換法とFAQの詳細な説明 この記事では、javaScriptの文字列文字を置き換える2つの方法について説明します:内部JavaScriptコードとWebページの内部HTML。 JavaScriptコード内の文字列を交換します 最も直接的な方法は、置換()メソッドを使用することです。 str = str.replace( "find"、 "置換"); この方法は、最初の一致のみを置き換えます。すべての一致を置き換えるには、正規表現を使用して、グローバルフラグGを追加します。 str = str.replace(/fi

それで、あなたはここで、Ajaxと呼ばれるこのことについてすべてを学ぶ準備ができています。しかし、それは正確には何ですか? Ajaxという用語は、動的でインタラクティブなWebコンテンツを作成するために使用されるテクノロジーのゆるいグループ化を指します。 Ajaxという用語は、もともとJesse Jによって造られました

10の楽しいjQueryゲームプラグインして、あなたのウェブサイトをより魅力的にし、ユーザーの粘着性を高めます! Flashは依然としてカジュアルなWebゲームを開発するのに最適なソフトウェアですが、jQueryは驚くべき効果を生み出すこともできます。また、純粋なアクションフラッシュゲームに匹敵するものではありませんが、場合によってはブラウザで予期せぬ楽しみもできます。 jquery tic toeゲーム ゲームプログラミングの「Hello World」には、JQueryバージョンがあります。 ソースコード jQueryクレイジーワードコンポジションゲーム これは空白のゲームであり、単語の文脈を知らないために奇妙な結果を生み出すことができます。 ソースコード jquery鉱山の掃引ゲーム

記事では、JavaScriptライブラリの作成、公開、および維持について説明し、計画、開発、テスト、ドキュメント、およびプロモーション戦略に焦点を当てています。

このチュートリアルでは、jQueryを使用して魅惑的な視差の背景効果を作成する方法を示しています。 見事な視覚的な深さを作成するレイヤー画像を備えたヘッダーバナーを構築します。 更新されたプラグインは、jQuery 1.6.4以降で動作します。 ダウンロードしてください

この記事では、JQueryとAjaxを使用して5秒ごとにDivのコンテンツを自動的に更新する方法を示しています。 この例は、RSSフィードからの最新のブログ投稿と、最後の更新タイムスタンプを取得して表示します。 読み込み画像はオプションです

Matter.jsは、JavaScriptで書かれた2D Rigid Body Physics Engineです。このライブラリは、ブラウザで2D物理学を簡単にシミュレートするのに役立ちます。剛体を作成し、質量、面積、密度などの物理的特性を割り当てる機能など、多くの機能を提供します。また、重力摩擦など、さまざまな種類の衝突や力をシミュレートすることもできます。 Matter.jsは、すべての主流ブラウザをサポートしています。さらに、タッチを検出し、応答性が高いため、モバイルデバイスに適しています。これらの機能はすべて、物理ベースの2Dゲームまたはシミュレーションを簡単に作成できるため、エンジンの使用方法を学ぶために時間をかける価値があります。このチュートリアルでは、このライブラリのインストールや使用法を含むこのライブラリの基本を取り上げ、

この記事では、ブラウザでJavaScriptのパフォーマンスを最適化するための戦略について説明し、実行時間の短縮、ページの負荷速度への影響を最小限に抑えることに焦点を当てています。
