HTML5 transform三維立方體實現(xiàn)360無死角三維旋轉(zhuǎn)效果
來源:易賢網(wǎng) 閱讀:1311 次 日期:2014-11-20 15:12:19
溫馨提示:易賢網(wǎng)小編為您整理了“HTML5 transform三維立方體實現(xiàn)360無死角三維旋轉(zhuǎn)效果”,方便廣大網(wǎng)友查閱!

為了更好得掌握transform的精髓,所以決定完成三維立方體的模型,可以實現(xiàn)360無死角的三維旋轉(zhuǎn)效果。

但是旋轉(zhuǎn)時判斷每個面的視圖順序比較困難,仍未完美解決,希望有人能解答!

源碼直接貢獻(xiàn)啦:

代碼如下:

<style>

.cuboid_side_div{

position:absolute;

border:1px solid #333;

-webkit-transition:ease all 1s;

}

</style>

<script>

/**

* 本版本存在以下問題:

* 三維旋轉(zhuǎn)的zIndex計算有問題

* 還欠缺多種模型,常見的包括:線、面、椎體、球體、橢球體等。

*/

function cuboidModel(left_init,top_init,long_init,width_init,height_init)

{

////////////////////////////////////////

//初始化私有變量

///////////////////////////////////////

//初始化長方體位置、大小

var left = left_init;

var top = top_init;

var long = long_init;

var width = width_init;

var height = height_init;

//初始化變換角度,默認(rèn)為0

var rotateX = 0;

var rotateY = 0;

var rotateZ = 0;

var zIndex = 0;

//定義長方體六個面的div對象

var div_front;

var div_behind;

var div_left;

var div_right;

var div_top;

var div_bottom;

////////////////////////////////////////

//初始化長方體

///////////////////////////////////////

//根據(jù)初始位置構(gòu)造六個面。

this.init = function() {

//創(chuàng)建front div

div_front = document.createElement("div");

div_front.className = "cuboid_side_div";

div_front.innerHTML = "div front";

div_front.style.backgroundColor="#f1b2b2";

document.body.appendChild(div_front);

//創(chuàng)建behind div

div_behind = document.createElement("div");

div_behind.className = "cuboid_side_div";

div_behind.innerHTML = "div behind";

div_behind.style.backgroundColor="#bd91eb";

document.body.appendChild(div_behind);

//創(chuàng)建left div

div_left = document.createElement("div");

div_left.className = "cuboid_side_div";

div_left.innerHTML = "div left";

div_left.style.backgroundColor="#64a3c3";

document.body.appendChild(div_left);

//創(chuàng)建right div

div_right = document.createElement("div");

div_right.className = "cuboid_side_div";

div_right.innerHTML = "div right";

div_right.style.backgroundColor="#78e797";

document.body.appendChild(div_right);

//創(chuàng)建top div

div_top = document.createElement("div");

div_top.className = "cuboid_side_div";

div_top.innerHTML = "div top";

div_top.style.backgroundColor="#e7db78";

document.body.appendChild(div_top);

//創(chuàng)建bottom div

div_bottom = document.createElement("div");

div_bottom.className = "cuboid_side_div";

div_bottom.innerHTML = "div bottom";

div_bottom.style.backgroundColor="#e79c78";

document.body.appendChild(div_bottom);

this.refresh();

};

//重繪

this.refresh = function()

{

//定義div_front樣式

div_front.style.left = left+"px";

div_front.style.top = top+"px";

div_front.style.width = long +"px";

div_front.style.height = height +"px";

div_front.style.webkitTransformOrigin = "50% 50% "+((-1)*width / 2)+"px";

//定義div_behind樣式

div_behind.style.left = left+"px";

div_behind.style.top = top+"px";

div_behind.style.width = div_front.style.width;

div_behind.style.height = div_front.style.height;

div_behind.style.webkitTransformOrigin = "50% 50% "+((-1)*width / 2)+"px";

//定義div_left樣式

div_left.style.left = left+((long - height) / 2)+"px";

div_left.style.top = top + ((height - width) / 2)+"px";

div_left.style.width = height +"px";

div_left.style.height = width +"px";

div_left.style.webkitTransformOrigin = "50% 50% "+((-1) * long /2 )+"px";

//定義div_right樣式

div_right.style.left = div_left.style.left;

div_right.style.top = div_left.style.top;

div_right.style.width = div_left.style.width;

div_right.style.height = div_left.style.height;

div_right.style.webkitTransformOrigin = "50% 50% "+((-1) * long /2 )+"px";

//定義div_top樣式

div_top.style.left = left+"px";

div_top.style.top = top+((height - width)/ 2)+"px";

div_top.style.width = long +"px";

div_top.style.height = width +"px";

div_top.style.webkitTransformOrigin = "50% 50% "+((-1) * height /2 )+"px";

//定義div_bottom樣式

div_bottom.style.left = div_top.style.left;

div_bottom.style.top = div_top.style.top;

div_bottom.style.width = div_top.style.width;

div_bottom.style.height = div_top.style.height;

div_bottom.style.webkitTransformOrigin = "50% 50% "+((-1) * height /2 )+"px";

this.rotate(rotateX,rotateY,rotateZ);

};

//旋轉(zhuǎn)立方體

this.rotate = function(x,y,z) {

rotateX = x;

rotateY = y;

rotateZ = z;

var rotateX_front = rotateX;

var rotateY_front = rotateY;

var rotateZ_front = rotateZ;

//判斷各個面旋轉(zhuǎn)角度

var rotateX_behind = rotateX_front+180;

var rotateY_behind = rotateY_front * (-1);

var rotateZ_behind = rotateZ_front * (-1);

var rotateX_top = rotateX_front+90;

var rotateY_top = rotateZ_front;

var rotateZ_top = rotateY_front * (-1);

var rotateX_bottom = rotateX_front-90;

var rotateY_bottom = rotateZ_front * (-1);

var rotateZ_bottom = rotateY_front;

var rotateX_left = rotateX_front + 90;

var rotateY_left = rotateZ_front - 90;

var rotateZ_left = rotateY_front * (-1);

var rotateX_right = rotateX_front + 90;

var rotateY_right = rotateZ_front + 90;

var rotateZ_right = rotateY_front * (-1);

//判斷各個面的z軸顯示順序

var zIndex_front_default = -1;

var zIndex_behind_default = -6;

var zIndex_top_default = -5;

var zIndex_bottom_default = -2;

var zIndex_left_default = -4;

var zIndex_right_default = -3;

var xI = (rotateX_front / 90) % 4;

var yI = (rotateY_front / 90) % 4;

var zI = (rotateZ_front / 90) % 4;

var zIndex_matrix = new Array();

for(var i = 0; i < 3;i++) {

zIndex_matrix.push(new Array());

}

zIndex_matrix = [["","zIndex_top",""],

["zIndex_left","zIndex_front","zIndex_right"],

["","zIndex_bottom",""]];

var zIndex_matrix_behind = "zIndex_behind";

//計算zIndex

if((xI >= 0 && xI < 1) ||(xI >= -4 && xI < -3)) {

} else if((xI >= 1 && xI < 2) ||(xI >= -3 && xI < -2)) {

var zIndex_matrix_tmp = zIndex_matrix[0][1];

zIndex_matrix[0][1] = zIndex_matrix[1][1];

zIndex_matrix[1][1] = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix_behind;

zIndex_matrix_behind = zIndex_matrix_tmp;

} else if((xI >= 2 && xI < 3) ||(xI >= -2 && xI < -1)) {

var zIndex_matrix_tmp = zIndex_matrix[0][1];

zIndex_matrix[0][1] = zIndex_matrix[2][1];

zIndex_matrix[2][1] = zIndex_matrix_tmp;

zIndex_matrix_tmp = zIndex_matrix[1][1];

zIndex_matrix[1][1] = zIndex_matrix_behind;

zIndex_matrix_behind = zIndex_matrix_tmp;

} else if((xI >= 3 && xI < 4) ||(xI >= -1 && xI < 0)) {

var zIndex_matrix_tmp = zIndex_matrix[0][1];

zIndex_matrix[0][1] = zIndex_matrix_behind;

zIndex_matrix_behind = zIndex_matrix[2][1];

zIndex_matrix[2][1] = zIndex_matrix[1][1];

zIndex_matrix[1][1] = zIndex_matrix_tmp;

}

if((yI > 0 && yI <= 1) ||(yI > -4 && yI <= -3)) {

var zIndex_matrix_tmp = zIndex_matrix[1][0];

zIndex_matrix[1][0] = zIndex_matrix_behind;

zIndex_matrix_behind = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix[1][1];

zIndex_matrix[1][1] = zIndex_matrix_tmp;

} else if((yI > 1 && yI <= 2) ||(yI > -3 && yI <= -2)) {

var zIndex_matrix_tmp = zIndex_matrix[1][0];

zIndex_matrix[1][0] = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix_tmp;

zIndex_matrix_tmp = zIndex_matrix[1][1];

zIndex_matrix[1][1] = zIndex_matrix_behind;

zIndex_matrix_behind = zIndex_matrix_tmp;

} else if((yI > 2 && yI <= 3) ||(yI > -2 && yI <= -1)) {

var zIndex_matrix_tmp = zIndex_matrix[1][0];

zIndex_matrix[1][0] = zIndex_matrix[1][1];

zIndex_matrix[1][1] = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix_behind;

zIndex_matrix_behind = zIndex_matrix_tmp;

} else if((yI > 3 && yI <= 4) ||(yI > -1 && yI <= 0)) {

}

if((zI > 0 && zI <= 1) ||(zI > -4 && zI <= -3)) {

var zIndex_matrix_tmp = zIndex_matrix[0][1];

zIndex_matrix[0][1] = zIndex_matrix[1][0];

zIndex_matrix[1][0] = zIndex_matrix[2][1];

zIndex_matrix[2][1] = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix_tmp;

} else if((zI > 1 && zI <= 2) ||(zI > -3 && zI <= -2)) {

var zIndex_matrix_tmp = zIndex_matrix[0][1];

zIndex_matrix[0][1] = zIndex_matrix[2][1];

zIndex_matrix[2][1] = zIndex_matrix_tmp;

zIndex_matrix_tmp = zIndex_matrix[1][0];

zIndex_matrix[1][0] = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix_tmp;

} else if((zI > 2 && zI <= 3) ||(zI > -2 && zI <= -1)) {

var zIndex_matrix_tmp = zIndex_matrix[0][1];

zIndex_matrix[0][1] = zIndex_matrix[1][2];

zIndex_matrix[1][2] = zIndex_matrix[2][1];

zIndex_matrix[2][1] = zIndex_matrix[1][0];

zIndex_matrix[1][0] = zIndex_matrix_tmp;

} else if((zI > 3 && zI <= 4) ||(zI > -1 && zI <= 0)) {

}

//賦值zIndex

eval(zIndex_matrix[0][1]+"="+zIndex_top_default);

eval(zIndex_matrix[1][0]+"="+zIndex_left_default);

eval(zIndex_matrix[1][1]+"="+zIndex_front_default);

eval(zIndex_matrix[1][2]+"="+zIndex_right_default);

eval(zIndex_matrix[2][1]+"="+zIndex_bottom_default);

eval(zIndex_matrix_behind+"="+zIndex_behind_default);

//front

var transform_rotate_front = "perspective(500px) rotateX("+rotateX_front+

"deg) rotateY("+rotateY_front+

"deg) rotateZ("+rotateZ_front+"deg)";

div_front.style.webkitTransform = transform_rotate_front;

div_front.style.zIndex = zIndex_front;

//behind

var transform_rotate_behind = "perspective(500px) rotateX("+rotateX_behind+

"deg) rotateY("+rotateY_behind+

"deg) rotateZ("+rotateZ_behind+"deg)";

div_behind.style.webkitTransform = transform_rotate_behind;

div_behind.style.zIndex = zIndex_behind;

//left

var transform_rotate_left = "perspective(500px) rotateX("+rotateX_left+

"deg) rotateZ("+rotateZ_left+

"deg) rotateY("+rotateY_left+"deg)";

div_left.style.webkitTransform = transform_rotate_left;

div_left.style.zIndex = zIndex_left;

//right

var transform_rotate_right = "perspective(500px) rotateX("+rotateX_right+

"deg) rotateZ("+rotateZ_right+

"deg) rotateY("+rotateY_right+"deg)";

div_right.style.webkitTransform = transform_rotate_right;

div_right.style.zIndex = zIndex_right;

//top

var transform_rotate_top = "perspective(500px) rotateX("+rotateX_top+

"deg) rotateZ("+rotateZ_top+

"deg) rotateY("+rotateY_top+"deg)";

div_top.style.webkitTransform = transform_rotate_top;

div_top.style.zIndex = zIndex_top;

//bottom

var transform_rotate_bottom = "perspective(500px) rotateX("+rotateX_bottom+

"deg) rotateZ("+rotateZ_bottom+

"deg) rotateY("+rotateY_bottom+"deg)";

div_bottom.style.webkitTransform = transform_rotate_bottom;

div_bottom.style.zIndex = zIndex_bottom;

};

//重置長方體的長、寬、高

this.resize = function(new_long, new_width, new_height)

{

long = new_long;

width = new_width;

height = new_height;

this.refresh();

};

//重置長方體的位置

this.move = function(new_left,new_top) {

top = new_top;

left = new_left;

this.refresh();

};

}

function transform() {

cuboid.resize(parseInt(document.getElementById("long").value),

parseInt(document.getElementById("width").value),

parseInt(document.getElementById("height").value));

cuboid.move(parseInt(document.getElementById("left").value),

parseInt(document.getElementById("top").value));

cuboid.rotate(parseInt(document.getElementById("rotatex").value),

parseInt(document.getElementById("rotatey").value),

parseInt(document.getElementById("rotatez").value));

//cuboid.refresh();

}

</script>

<div style="position:absolute;border:1px solid #333;top:240px;left:100px;width:1000px;height: 360px;">

left:<input id="left" value="100"></input>px

top:<input id="top" value="50"></input>px

long:<input id="long" value="100"></input>px

width:<input id="width" value="60"></input>px

height:<input id="height" value="80"></input>px

rotateX: <input id="rotatex" value="0"></input>deg

rotateY: <input id="rotatey" value="0"></input>deg

rotateZ: <input id="rotatez" value="0"></input>deg

<input type="button" value="確定" onclick="transform()"></input>

<label id="status"></label>

</div>

<script>

var cuboid = new cuboidModel(parseInt(document.getElementById("left").value),

parseInt(document.getElementById("top").value),

parseInt(document.getElementById("long").value),

parseInt(document.getElementById("width").value),

parseInt(document.getElementById("height").value));

cuboid.init();

</script>

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)頁制作
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機(jī)號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)