javaScript制作3D效果展示
2012-10-10 21:42
387 查看
这个例子主要展示一下3D效果,我特意在网站找了一个地球的图片进行观看,很有感觉。呵呵。下面是效果图。
注:这个例子主要有两个文件:
1:sanwei.html
2:images文件夹下面的一个rouxts2.png图片
sanwei.html:
rouxts2.png
注:鼠标右键把该图片另存到本地,然后新建一个文件夹images,然后放入该文件夹中,把文件夹和html文件放在同一目录中,然后双击打开那个html可以看到效果。
注:这个例子主要有两个文件:
1:sanwei.html
2:images文件夹下面的一个rouxts2.png图片
sanwei.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>三维效果展示</title> <meta name="Author" http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="imagetoolbar" content="no"> <style type="text/css"> html { overflow: hidden; } body { position: absolute; margin: 0px; padding: 0px; background: #fff; width: 100%; height: 100%; } #screen { position: absolute; left: 10%; top: 10%; width: 80%; height: 80%; background: #fff; } #screen img { position: absolute; cursor: pointer; width: 0px; height: 0px; -ms-interpolation-mode:nearest-neighbor; } #bankImages { visibility: hidden; } #FPS { position: absolute; right: 5px; bottom: 5px; font-size: 10px; color: #666; font-family: verdana; } </style> <script type="text/javascript"> /* ==== function ==== */ var Library = {}; Library.ease = function () { this.target = 0; this.position = 0; this.move = function (target, speed) { this.position += (target - this.position) * speed; } } var tv = { /* ==== variables ==== */ O : [], fps : 0, screen : {}, angle : { x : new Library.ease(), y : new Library.ease() }, camera : { x : new Library.ease(), y : new Library.ease() }, create3DHTML : function (i, x, y, z, sw, sh) { /* ==== create HTML image element ==== */ var o = document.createElement('img'); o.src = i.src; tv.screen.obj.appendChild(o); /* ==== 3D ==== */ o.point3D = { x : x, y : y, z : new Library.ease(), sw : sw, sh : sh, w : i.width, h : i.height }; o.point3D.z.target = z; /* ==== push object ==== */ o.point2D = {}; tv.O.push(o); /* ==== 鼠标移动事件 ==== */ o.onmouseover = function () { if (this != tv.o) { this.point3D.z.target = tv.mouseZ; tv.camera.x.target = this.point3D.x; tv.camera.y.target = this.point3D.y; if (tv.o) tv.o.point3D.z.target = 0; tv.o = this; } return false; } /* ==== 鼠标点击事件 ==== */ o.onmousedown = function () { if (this == tv.o) { if (this.point3D.z.target == tv.mouseZ) this.point3D.z.target = 0; else { tv.o = false; this.onmouseover(); } } } /* ==== 3D展示核心函数 ==== */ o.animate = function () { /* ==== 3D coordinates ==== */ var x = this.point3D.x - tv.camera.x.position; var y = this.point3D.y - tv.camera.y.position; this.point3D.z.move(this.point3D.z.target, this.point3D.z.target ? .15 : .08); /* ==== rotations ==== */ var xy = tv.angle.cx * y - tv.angle.sx * this.point3D.z.position; var xz = tv.angle.sx * y + tv.angle.cx * this.point3D.z.position; var yz = tv.angle.cy * xz - tv.angle.sy * x; var yx = tv.angle.sy * xz + tv.angle.cy * x; /* ==== 2D transform ==== */ var scale = tv.camera.focalLength / (tv.camera.focalLength + yz); x = yx * scale; y = xy * scale; var w = Math.round(Math.max(0, this.point3D.w * scale * this.point3D.sw)); var h = Math.round(Math.max(0, this.point3D.h * scale * this.point3D.sh)); /* ==== HTML rendering ==== */ var o = this.style; o.left = Math.round(x + tv.screen.w - w * .5) + 'px'; o.top = Math.round(y + tv.screen.h - h * .5) + 'px'; o.width = w + 'px'; o.height = h + 'px'; o.zIndex = 10000 + Math.round(scale * 1000); } }, /* ==== init script ==== */ init : function (structure, FL, mouseZ, rx, ry) { this.screen.obj = document.getElementById('screen'); this.screen.obj.onselectstart = function () { return false; } this.screen.obj.ondrag = function () { return false; } this.mouseZ = mouseZ; this.camera.focalLength = FL; this.angle.rx = rx; this.angle.ry = ry; /* ==== create objects ==== */ var i = 0, o; while( o = structure[i++] ) this.create3DHTML(o.img, o.x, o.y, o.z, o.sw, o.sh); /* ==== start script ==== */ this.resize(); mouse.y = this.screen.y + this.screen.h; mouse.x = this.screen.x + this.screen.w; /* ==== loop ==== */ setInterval(tv.run, 16); setInterval(tv.dFPS, 1000); }, /* ==== resize window ==== */ resize : function () { var o = tv.screen.obj; if (o) { tv.screen.w = o.offsetWidth / 2; tv.screen.h = o.offsetHeight / 2; for (tv.screen.x = 0, tv.screen.y = 0; o != null; o = o.offsetParent) { tv.screen.x += o.offsetLeft; tv.screen.y += o.offsetTop; } } }, /* ==== main loop ==== */ run : function () { tv.fps++; /* ==== motion ease ==== */ tv.angle.x.move(-(mouse.y - tv.screen.h - tv.screen.y) * tv.angle.rx, .1); tv.angle.y.move( (mouse.x - tv.screen.w - tv.screen.x) * tv.angle.ry, .1); tv.camera.x.move(tv.camera.x.target, .025); tv.camera.y.move(tv.camera.y.target, .025); /* ==== angles sin and cos ==== */ tv.angle.cx = Math.cos(tv.angle.x.position); tv.angle.sx = Math.sin(tv.angle.x.position); tv.angle.cy = Math.cos(tv.angle.y.position); tv.angle.sy = Math.sin(tv.angle.y.position); /* ==== loop through images ==== */ var i = 0, o; while( o = tv.O[i++] ) o.animate(); }, /* ==== trace frames per seconds ==== */ dFPS : function () { document.getElementById('FPS').innerHTML = tv.fps + ' FPS'; tv.fps = 0; } } /* ==== global mouse position ==== */ var mouse = { x : 0, y : 0 } document.onmousemove = function(e) { if (window.event) e = window.event; mouse.x = e.clientX; mouse.y = e.clientY; return false; } /* ==== starting script ==== */ onload = function() { onresize = tv.resize; /* ==== build grid ==== */ var img = document.getElementById('bankImages').getElementsByTagName('img'); var structure = []; for (var i = -300; i <= 300; i += 120) for (var j = -300; j <= 300; j += 120) structure.push({ img:img[0], x:i, y:j, z:0, sw:.5, sh:.5 }); /* ==== let's go ==== */ tv.init(structure, 350, -200, .005, .0025); } </script> </head> <body> <div id="screen"></div> <div id="bankImages"> <img alt="" src="images/rouxts2.png"> </div> <div id="FPS"></div> </body> </html>
rouxts2.png
注:鼠标右键把该图片另存到本地,然后新建一个文件夹images,然后放入该文件夹中,把文件夹和html文件放在同一目录中,然后双击打开那个html可以看到效果。
相关文章推荐
- 用css3,transform3d制作房屋全景展示效果(一)
- 原生javascript制作拖拽效果
- 9款非常实用的CSS3和Javascript 3D效果演示
- 网页制作用JavaScript实现不同风格的图片切换效果
- JavaScript 图片3D展示空间(3DRoom)
- JavaScript实现的图片3D展示空间(3DRoom)
- 叶孤城大神之--视频特效制作:如何给视频添加边框、水印、动画以及3D效果
- javascript中相互横向切换展示的窗口效果
- Javascript控制图片围成3D模型旋转展示
- 纯JS实现旋转图片3D展示效果
- 用pyplot在坐标系中画图像[制作虚幻效果],这样就可以进行图像变换了,如2d到3d
- 分享一款基于jqery的带3D切换效果的图片展示
- 3D图片展示效果练习
- JavaScript 图片3D展示空间(3DRoom)
- 用CATransform3D实现3D效果和制作简单3D动画
- 30个制作相册以及各种图片效果的Javascript脚本
- js实现3D图片展示效果
- js实现3D图片环展示效果
- javascript制作图片移动效果
- JavaScript 图片切换展示效果alibaba拓展版第1/3页