js模拟3D场景效果代码打包

要在二维空间模拟出三维的效果,就需要把三维的坐标转换成二维坐标。一个最基本依据是:东西越远,看到大小就越小,坐标越往消失点靠拢。
  透视公式:
   scale = fl / (fl + z);
   scale是大小的比例值,0.0到1.0之间,fl是观察点到成像面的距离,通常这个值是固定,z就是物件的三维空间中的z轴。
  在写这些代码之前,我喜欢用面向对象来描述我写的这些东西,比如我需要一个场景,场景是个空间,空间内是可以容纳各种物件的,物件是个对象,物件是是x,y,z三个维度的,场景可以插入任意多的物件,物件就会以它的坐标值,显示在场景的特定位置,由场景来负责物件的显示位置。
  一些demo,请使用鼠标移动及滚轮来控制。
效果1

伪3d透视效果

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;}

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 ? '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

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

效果2

伪3d透视效果

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;}

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 ? '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

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

效果3

伪3d透视效果

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;}

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 ? '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; ithis.wh.h){
thing.xyz.y = 0;
}else{
thing.xyz.y += 20;
}
this.calcPosition(thing);
}
},
reDraw:function(){
for(var i=0 ; i

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

效果4

伪3d透视效果

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;}

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 ? '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

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

(0)

相关推荐

  • JS实现的3D拖拽翻页效果代码

    本文实例讲述了JS实现的3D拖拽翻页效果.分享给大家供大家参考,具体如下: 以前看到一个很火的帖子,拖拽实现的翻页效果,非常的有创意,自己也很喜欢,于是乎就萌发了用自己的方法模仿的想法.感谢原创作者的创意,结果一样,但过程不一样哦.奉上代码,供大家参考. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-3d-drag-page-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DT

  • 纯JS实现旋转图片3D展示效果

    CSS: <style type="text/css"> #show{position:relative;margin:20px auto;width:800px;} .item{position:absolute;height:40px;width:60px;background:#999999;border:1px solid #eeeeee;cursor:pointer;}</style> Html: input id="l" type

  • js实现3D图片环展示效果

    可对整体进行拖拽 效果图: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{padding:0;margin:0; font-family: "Proxima Nova","proxima-nova&qu

  • JS实现3D图片旋转展示效果代码

    本文实例讲述了JS实现3D图片旋转展示效果代码.分享给大家供大家参考.具体如下: 这是一段JavaScript代码,围绕成3D模型样式的JavaScript图片旋转展示代码,这里为了演示方便,将图片替换成了数字,预留出了图片的位置,这样速度快些,会HTML的朋友都知道用时候该怎么做.本图片旋转需要手功控制,每点击一下,图片旋转一次,很方便. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-3d-pic-scroll-show-style-c

  • 原生javascript+css3编写的3D魔方动画旋扭特效

    一直从事于后端编程工作,工作中也经常接触和使用一些前端技术,但大多数还是用于操作和控制数据与客户端交互工作.随着互联网技术的发展以及硬件性能的不断提升,人们对于交互式体验已变得越来越重视,进而前端技术已经越来越突显出它的重要性,特别是一些炫酷的特效,精美的UI设计,都使人眼前一亮,顿时觉得网站平台都高大上不少,很博人们眼球,给人们以很好的视觉冲击,特别是现在的css3,HTML5技术的更新,使得以更小的代价就可以轻松实现这些效果,故此,顿时膜拜起前端技术,悠然起了兴趣,跃跃欲试,随机利用css3

  • js实现3D图片逐张轮播幻灯片特效代码分享

    本文实例讲述了javascript实现3D图片逐张轮播幻灯片特效.分享给大家供大家参考.具体如下: 这是一款基于javascript实现3D图片逐张轮播幻灯片特效代码,实现过程很简单. 运行效果图:-------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的js实现3D图片逐张轮播幻灯片特效代码如下 <head> <meta http-equiv="Content-Type

  • 基于javascript html5实现3D翻书特效

    这是一款十分炫酷的HTML5 3D书本翻页动画,效果相对比较简单,拖拽鼠标模拟用手翻页,更漂亮的是翻页过程中,呈现出逼真的3D立体效果.书本中的文字和图片也会3D展示,非常酷. 在线演示 源码下载 HTML代码 <div class="book p3d"> <div class="back-cover p3d"> <div class="page back flip"></div> <div

  • js实现3D图片展示效果

    点击左上角的按钮前后切换 效果图: 代码如下: <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{margin:0; padding:0; list-style:none;} ul{ width:300px; height:300px; margin:100px auto; positi

  • js+vml创建3D页面效果代码

    v\:* { Behavior: url(#default#VML) } body{margin:0px} 向左旋转 暂停旋转 向右旋转 var q=40; var r=(q*3.1415926)/180 var v=new Array() var l=new Array() v[0]="0,400,0" v[1]="100,350,15" v[2]="200,400,0" v[3]="0,400,300" v[4]=&quo

  • 基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转

    通过原生JS,点击事件,鼠标按下.鼠标抬起和鼠标移动事件,实现3d立方体的拖动旋转,并将旋转角度实时的反应至界面上显示. 实现原理:通过获取鼠标点击屏幕时的坐标和鼠标移动时的坐标,来获得鼠标在X轴.Y轴移动的距离,将距离实时赋值给transform属性 从而通过改变transform:rotate属性值来达到3d立方体旋转的效果 HTML代码块: <body> <input type="button" class="open" value=&quo

随机推荐