[Javascript][Vector3D类]旋转效果(四)
使用Vector3D类来完成鼠标移动旋转元素,元素将同时绕着X轴和Y轴旋转,看多了眼很花。
用Vector和Vector3D完成的这些效果执行效率都不高,而且视觉效果也不算好,写了这些东西出来,只是为了好玩而已,另外也可以用来熟悉如何将常见的视觉效果用代码来实现。
旋转效果示例
用Vector和Vector3D完成的这些效果执行效率都不高,而且视觉效果也不算好,写了这些东西出来,只是为了好玩而已,另外也可以用来熟悉如何将常见的视觉效果用代码来实现。
旋转效果示例
Code
1 /**
2 * Galley 3D
3 * @classDescription create a rotating galley with 3 dimensions.
4 * @dependence Mootools.js(http://www.mootools.net), Math.js, Vector3D.js
5 * @author Aken.li (http://www.kxbd.com)
6 * @version 1.0
7 * @created 2009.01.16
8 */
9
10 var Galley3D = new Class({
11 Implements:Options,
12
13 options:{
14 eW:60,//元素的长
15 eH:45,//元素的宽
16 rows:5,//行数
17 cols:4,//列数
18 gapX:120,//列间距
19 gapY:100,//行间距
20 mA:0.0002,//转动角度
21 mD:20,//转动时间,单位毫秒
22 pers: 400//透视比例
23 },
24
25 initialize:function(elements,options){
26 this.setOptions(options);
27 this.elements = $$(elements);
28 this.amount = this.elements.length;
29 this.vectors = [];
30 this.xposFix = ((this.options.rows-1)*this.options.gapX+this.options.eW)*0.5;
31 this.yposFix = ((this.options.cols-1)*this.options.gapY+this.options.eH)*0.5;
32 this.mousePos = {x:0,y:0};
33 this.fps = 0;
34
35 this.adjustZero();
36 this.createVectors();
37 this.attach();
38 this.mvId = this.setMv.periodical(this.options.mD,this);
39 },
40
41 attach:function(){
42 document.addEvent('mousemove',function(e){
43 this.mousePos.x = e.client.x;
44 this.mousePos.y = e.client.y;
45 }.bind(this));
46 window.addEvent('resize',this.adjustZero.bind(this));
47 },
48
49 //调整转动原点
50 adjustZero:function(){
51 var dimsWin = Document.getSize();
52 this.zero = {x:dimsWin.x*0.5, y:dimsWin.y*0.5}
53 },
54
55 //改变vectors,再渲染Galley
56 setMv:function(){
57 this.fps++;
58 var eX = this.mousePos.x, eY = this.mousePos.y;
59 var angleA = 0, angleB = 0;
60 var diffX = eX - this.zero.x;
61 var diffY = eY - this.zero.y;
62 angleA = Math.radiansToDegrees(diffY)*this.options.mA;
63 angleB = Math.radiansToDegrees(diffX)*this.options.mA;
64
65 var ca = Math.cosD(angleA);
66 var sa = Math.sinD(angleA);
67 var cb = Math.cosD(angleB);
68 var sb = Math.sinD(angleB);
69 this.vectors.each(function(v){
70 //v.rotateXTrig(ca,sa);
71 //v.rotateYTrig(cb,sb);
72 v.rotateXYTrig(ca,sa,cb,sb);
73 });
74 this.render();
75 },
76
77 //渲染Galley
78 render:function(){
79 this.elements.each(function(o,i){
80 var v = this.vectors[i];
81 o.setStyles({
82 left:v.x+this.zero.x,
83 top:v.y+this.zero.y,
84 width:v.getPerspective(this.options.pers)*this.options.eW,
85 zIndex:10000-v.z*10
86 });
87 }, this);
88 },
89
90 //创建vectors数组
91 createVectors:function() {
92 for(var i=0;i<this.options.cols;i++){
93 for(var j=0; j<this.options.rows;j++){
94 this.vectors.push(new Vector3D(j*this.options.gapX-this.xposFix,i*this.options.gapY-this.yposFix,0));
95 }
96 }
97 }
98 });
1 /**
2 * Galley 3D
3 * @classDescription create a rotating galley with 3 dimensions.
4 * @dependence Mootools.js(http://www.mootools.net), Math.js, Vector3D.js
5 * @author Aken.li (http://www.kxbd.com)
6 * @version 1.0
7 * @created 2009.01.16
8 */
9
10 var Galley3D = new Class({
11 Implements:Options,
12
13 options:{
14 eW:60,//元素的长
15 eH:45,//元素的宽
16 rows:5,//行数
17 cols:4,//列数
18 gapX:120,//列间距
19 gapY:100,//行间距
20 mA:0.0002,//转动角度
21 mD:20,//转动时间,单位毫秒
22 pers: 400//透视比例
23 },
24
25 initialize:function(elements,options){
26 this.setOptions(options);
27 this.elements = $$(elements);
28 this.amount = this.elements.length;
29 this.vectors = [];
30 this.xposFix = ((this.options.rows-1)*this.options.gapX+this.options.eW)*0.5;
31 this.yposFix = ((this.options.cols-1)*this.options.gapY+this.options.eH)*0.5;
32 this.mousePos = {x:0,y:0};
33 this.fps = 0;
34
35 this.adjustZero();
36 this.createVectors();
37 this.attach();
38 this.mvId = this.setMv.periodical(this.options.mD,this);
39 },
40
41 attach:function(){
42 document.addEvent('mousemove',function(e){
43 this.mousePos.x = e.client.x;
44 this.mousePos.y = e.client.y;
45 }.bind(this));
46 window.addEvent('resize',this.adjustZero.bind(this));
47 },
48
49 //调整转动原点
50 adjustZero:function(){
51 var dimsWin = Document.getSize();
52 this.zero = {x:dimsWin.x*0.5, y:dimsWin.y*0.5}
53 },
54
55 //改变vectors,再渲染Galley
56 setMv:function(){
57 this.fps++;
58 var eX = this.mousePos.x, eY = this.mousePos.y;
59 var angleA = 0, angleB = 0;
60 var diffX = eX - this.zero.x;
61 var diffY = eY - this.zero.y;
62 angleA = Math.radiansToDegrees(diffY)*this.options.mA;
63 angleB = Math.radiansToDegrees(diffX)*this.options.mA;
64
65 var ca = Math.cosD(angleA);
66 var sa = Math.sinD(angleA);
67 var cb = Math.cosD(angleB);
68 var sb = Math.sinD(angleB);
69 this.vectors.each(function(v){
70 //v.rotateXTrig(ca,sa);
71 //v.rotateYTrig(cb,sb);
72 v.rotateXYTrig(ca,sa,cb,sb);
73 });
74 this.render();
75 },
76
77 //渲染Galley
78 render:function(){
79 this.elements.each(function(o,i){
80 var v = this.vectors[i];
81 o.setStyles({
82 left:v.x+this.zero.x,
83 top:v.y+this.zero.y,
84 width:v.getPerspective(this.options.pers)*this.options.eW,
85 zIndex:10000-v.z*10
86 });
87 }, this);
88 },
89
90 //创建vectors数组
91 createVectors:function() {
92 for(var i=0;i<this.options.cols;i++){
93 for(var j=0; j<this.options.rows;j++){
94 this.vectors.push(new Vector3D(j*this.options.gapX-this.xposFix,i*this.options.gapY-this.yposFix,0));
95 }
96 }
97 }
98 });