这几个demo上星期平安夜做的,感觉效果不错,就放在出来,分享给大家。

  要在二维空间模拟出三维的效果,就需要把三维的坐标转换成二维坐标。一个最基本依据是:东西越远,看到大小就越小,坐标越往消失点靠拢。

  透视公式:

   scale = fl / (fl + z);

   scale是大小的比例值,0.0到1.0之间,fl是观察点到成像面的距离,通常这个值是固定,z就是物件的三维空间中的z轴。

  在写这些代码之前,我喜欢用面向对象来描述我写的这些东西,比如我需要一个场景,场景是个空间,空间内是可以容纳各种物件的,物件是个对象,物件是是x,y,z三个维度的,场景可以插入任意多的物件,物件就会以它的坐标值,显示在场景的特定位置,由场景来负责物件的显示位置。

  一些demo,请使用鼠标移动及滚轮来控制。

效果1
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>伪3d透视效果</title> <style> html,body{ padding:0px; margin:0px; height:100%; width:100%;overflow:hidden;} #box{ background:#ccc; height:100%; border:1px solid #ccc;position:relative; overflow:hidden;} #debug{ width:200px; background:#fff; border:1px solid #ccc; position:absolute; left:10px; top:0px;} </style> </head> <body> <div id="box"> </div> <div id="debug"></div> </body> </html>

效果2
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>伪3d透视效果</title> <style> html,body{ padding:0px; margin:0px; height:100%; width:100%;overflow:hidden;} #box{ background:#ccc; height:100%; border:1px solid #ccc;position:relative; overflow:hidden;} #debug{ width:200px; background:#fff; border:1px solid #ccc; position:absolute; left:10px; top:0px;} </style> </head> <body> <div id="box"> </div> <div id="debug"></div> <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 = [ 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3s3bt1dt3naj1p3r1u2e7.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3sverp2e1j9l8rddbv6.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3sr3a15f2hd6trl1v632.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3s4e02mb3j01susuve5.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3sq8lo6puh5su1m4d4.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3s15nd1nrf1r8018un7jv3.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nbc3r2frhoe13gr1k6c11tp1.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 = [ 'http://img18.wal8.com/img18/227468_20111230225028/p16i7ncbsv12991dii1fjbif4esn4.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7ncbsv1652hgn13us1ob410423.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7ncbsv15i55d0meuru3obr2.jpg', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7ncbsvq3o143n1gdamcn13i21.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> </body> </html>

效果3
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>伪3d透视效果</title> <style> html,body{ padding:0px; margin:0px; height:100%; width:100%;overflow:hidden;} #box{ background:#fff; height:100%; border:1px solid #ccc;position:relative; overflow:hidden;} #debug{ width:200px; background:#fff; border:1px solid #ccc; position:absolute; left:10px; top:0px;} </style> </head> <body> <div id="box"> </div> <div id="debug"></div> <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 = [ 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1high41bbodrb18aboor8.gif', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1hiaeu6am10m7192a4pj7.gif', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1hi1te5gdb12p0mhp12lc6.gif', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1hi1fa51km41ofeh1219p25.gif', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1hiugi18uj1g5t1la4bd84.gif', 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1hi13fb1mjstfgo11pel3.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 = 'http://img18.wal8.com/img18/227468_20111230225028/p16i7nf1hijv3rtcqab1mbjr0g9.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> </body> </html>

效果4
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>伪3d透视效果</title> <style> html,body{ padding:0px; margin:0px; height:100%; width:100%;overflow:hidden;} #box{ background:#ccc; height:100%; border:1px solid #ccc;position:relative; overflow:hidden;} #debug{ width:200px; background:#fff; border:1px solid #ccc; position:absolute; left:10px; top:0px;} </style> </head> <body> <div id="box"> </div> <div id="debug"></div> </body> </html>

转载于:https://www.cnblogs.com/xingzhi/archive/2011/12/31/2308169.html

js模拟3D场景效果相关推荐

  1. JS模拟模式窗口效果

    <html> <head>     <meta http-equiv="Content-Type" content="text/html; ...

  2. JS 实现3D立体效果的首页轮播图(瞬间让你的网站高大上,逼格满满)

    还是那句话,废话少说,直接上源代码:http://download.csdn.net/detail/cometwo/9387901 <html> <head> <titl ...

  3. 如何用 JS 实现 3D 赛车效果

    本文将分享如何用 JS 写出一个 3D 赛车,我之前曾在技术交流会上也讲过一次(这里是当时用的 PPT),后来有同学反馈说讲得太深奥没听懂.其实 PPT 里说的更多的是三维图形的基础知识,实现原理体现 ...

  4. html5立体照片墙效果,js实现3D照片墙效果

    聊一下心得:CSS写得好,真的可以省很多js代码哈,写起来也简单很多,所以要好好掌握js哈,所以这里也提供了css代码,如果您觉得您的css写得不错,可以直接看js代码哦 效果: 1.点击Start ...

  5. vue + Three.js实现3d动画效果

    1.安装依赖 // 引入three.js相关插件npm install three --save// 安装轨道控件插件// npm install three-orbit-controls//安装加载 ...

  6. Seen.js – 使用 SVG 或者 Canvas 渲染 3D 场景

    Seen.js 渲染3D场景为 SVG 或者 HTML5 画布.Seen.js 包含对于 SVG 和 HTML5 Canvas 元素的图形功能的最简单的抽象.所有这个库的其它组件都是不用关心将要渲染的 ...

  7. cocos2dx使用TiledMap模拟3D地图场景----斜45度2D地图的靠墙直线移动

     基于cocos2dx引擎的第三人称射击游戏,角色使用3D模型,地图采用2.5D.  定制地图编辑系统抛开不谈,这里最大可能的挖掘现有工具TiledMap的潜力,完成超2.5D地图的实现.  使用2D ...

  8. JS实现最美的3D宇宙效果

    这里给各位同僚说一下,本人已经取消了之前所有分享的百度网盘的资料链接以及本人之前分享的AIRobot链接.之所以这样,是因为本人最近一段时间有个"大动作".就是将本人之前所有写的项 ...

  9. Three.js中的3D文字效果

    对于一些设计网页中经常会出现一些3D的文字效果,本文将利用Three.js实现各种动画WebGL文本输入效果. 示例效果 原文章 文本采样 通常情况下,文本网格是2D的平面形状,我们所要实现的3D文本 ...

最新文章

  1. FPGA(3)验证数字逻辑(与门、与非门、二选一数据选择器、2-4译码器、半加器、全加器)
  2. python哪一版好用-Python最好用的编辑器是哪款?北京老男孩教育
  3. 98.512X4位的芯片,要怎么组成4K的存储空间要用多少个芯片级联?具体用多少引脚?
  4. python如何封装成可调用的库_在python中如何以异步的方式调用第三方库提供的同步API...
  5. windows时间同步软件_有没有好用的windows时间管理软件?这款便签软件帮你
  6. python3主函数返回值_Python3
  7. 使用libjpeg进行JPEG图像解码
  8. 从把事做对到做对的事
  9. 系统软键盘Android在外接物理键盘时,如何强制调用系统软键盘?
  10. linux文件系统选哪种,linux下几种文件系统的测试比较
  11. python如何将数组里的数提取出来_python – 从数组数组中提取数组
  12. vlfeat python
  13. JSP从入门到精通_课堂实战视频教程
  14. 有趣好玩恶搞的C语言程序,有趣搞笑的整人VBS小脚本(整菜鸟专用)
  15. 西电c1级计算机应用测试题型,西电计算机应用基础测试题
  16. Pytorch深度学习笔记(02)--损失函数、交叉熵、过拟合与欠拟合
  17. CMStudio中出现‘$错误‘ is not a vaild integer value如何解决
  18. 日本药妆店扫货必备手册·收藏版
  19. jpg和rar格式转换
  20. .NET的数据库编程技术

热门文章

  1. java获取一个目录下所有字谜_Java 猜字谜游戏 - osc_4jkldo6l的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. C++ queue
  3. PsotgreSQL
  4. numpy 删除元素
  5. navicat导出查询结果原理_使用Navicat生成ER关系图并导出
  6. python中把输出结果写到一个文件中_如何将脚本输出写入文件和命令行?
  7. css3宽度变大动画_【动画演示】流量计的工作原理,真涨见识!
  8. 电脑尺寸大小在哪里看_科技资讯:电脑弹出本地计算机上的服务启动后停止的提示在哪里看...
  9. 银行核心系统之应用集成
  10. 什么是对象存储OSS-对象存储 OSS > 产品简介 > 什么是对象存储OSS