首頁 > web前端 > H5教程 > HTML5 transform三維立方體實現360無死角三維旋轉效果_html5教學技巧

HTML5 transform三維立方體實現360無死角三維旋轉效果_html5教學技巧

WBOY
發布: 2016-05-16 15:47:28
原創
2299 人瀏覽過

為了更好得掌握transform的精髓,所以決定完成三維立方體的模型,可以達到360無死角的立體旋轉效果。

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

原始碼直接貢獻啦:

複製程式碼
程式碼如下:


<script> <br />/**<br />* 本版本有下列問題: <br />* 三維旋轉的zIndex計算有問題 <br />* 仍欠缺多種模型,常見的包括:線、面、椎體、球體、橢球體等。 <br />*/ <br />function cuboidModel(left_init,top_init,long_init,width_init,height_init) <br />{ <br />{ <br />////// /////////////////////////////////// <br />//初始化私有變數<br />//// /////////////////////////////////// <br />//初始化長方體位置、大小<br />var left = left_init; <br />var top = top_init; <br />var long = long_init; <br />var width = width_init; <br />var height = height_init; <br />//初始化變換角度,認為var rotateX = 0; <br />var rotateY = 0; <br />var rotateZ = 0; <br />var zIndex = 0; <br />//定義長方體六個面的div對象<br />var div_front; >var div_behind; <br />var div_left; <br />var div_right; <br />var div_top; <br />var div_bottom; <br /><br />//////////////////////// //////////////////////// <br />//初始化長方體<br />///////////////// /////////////////////// <br />//依照初始位置建構六個面。 <br />this.init = function() { <br />//建立front div <br />div_front = document.createElement("div"); <br />div_front.className = "cuboid_side_div"; <br />divdiv_RML. = "div front"; <br />div_front.style.backgroundColor="#f1b2b2"; <br />document.body.appendChild(div_front); <br />//建立behind div <br />div_behind = document.createElement(" div"); <br />div_behind.className = "cuboid_side_div"; <br />div_behind.innerHTML = "div behind"; <br />div_behind.style.backgroundor="#bd91eb"; <br />div_behind.style.backgroundor="#bd91eb"; <br />> div_behind); <br />//建立left div <br />div_left = document.createElement("div"); <br />div_left.className = "cuboid_side_div"; <br />div_left.innerHTML = "divleftinnerHTML"; >div_left.style.backgroundColor="#64a3c3"; <br />document.body.appendChild(div_left); <br />//建立right div <br />div_right = document.createElement("div"); <br />div_right .className = "cuboid_side_div"; <br />div_right.innerHTML = "div right"; <br />div_right.style.backgroundColor="#78e797"; <br />document.body.appendChild(div_right);建立top div <br />div_top = document.createElement("div"); <br />div_top.className = "cuboid_side_div"; <br />div_top.innerHTML = "Col top"; <br />div_top.style.backgroundor="style. #e7db78"; <br />document.body.appendChild(div_top); <br />//建立bottom div <br />div_bottom = document.createElement("div"); <br />div_bottom.className = "cuboid_side_divcuboid <Ddiv""; 🎜>div_bottom.innerHTML = "div bottom"; <br />div_bottom.style.backgroundColor="#e79c78"; <br />document.body.appendChild(div_bottom); <br />this.refresh(); ; <br />//重繪<br />this.refresh = function() <br />{ <br />//定義div_front樣式<br />div_front.style.left = left "px"; <br />div_front.stylestyle .top = top "px"; <br />div_front.style.width = long "px"; <br />div_front.style.height = height "px"; <br />div_front.style.webkitTransformOrigin = "50% 50% " ((-1)*width / 2) "px"; <br />//定義div_behind樣式<br />div_behind.style.left = left "px"; <br />div_behind.style.top = top "px" ; <br />div_behind.style.width = div_front.style.width; <br />div_behind.style.height = div_front.style.height; <br />div_behind.style.webkitTransformOrigin = "50%; <br />div_behind.style.webkitTransformOrigin = "50% 50% "(-1150% "(-1 )*width / 2) "px"; <br />//定義div_left樣式<br />div_left.style.left = left ((long - height) / 2) "px"; <br />div_left.style.top = top ((height - width) / 2) "px"; <br />div_left.style.width = height "px"; <br />div_left.style.height = width "px"; <br />divkit_left.style.webkitTransformOinrigin = "50% 50% " ((-1) * long /2 ) "px"; <br />//定義div_right樣式<br />div_right.style.left = div_left.style.left; <br />div_right.style .top = div_left.style.top; <br />div_right.style.width = div_left.style.width; <br />div_right.style.height = div_left.style.height; <br />div_right.style.webkitTransformOrigin = "50>div_right.style.webkitTransformOrigin = "50 % 50% " ((-1) * long /2 ) "px"; <br />//定義div_top樣式<br />div_top.style.left = left "px"; <br />div_top.style.top = top ((height - width)/ 2) "px"; <br />div_top.style.width = long "px"; <br />div_top.style.height = width "px"; <br />div_top.style.webkitTransformOrigin = "50% 50% " ((-1) * height /2 ) "px"; <br />//定義div_bottom樣式<br />div_bottom.style.left = div_top.style.left; <br />div_bottom.style. top = div_top.style.top; <br />div_bottom.style.width = div_top.style.width; <br />div_bottom.style.height = div_top.style.height; <br />div_bottom.style.webkitTransformOrigin = "50% 50% " ((-1) * height /2 ) "px"; <br />this.rotate(rotateX,rotateY,rotateZ); <br />}; <br />//旋轉立方體<br />this.rotate = function(x,y,z) { <br />rotateX = x; <br />rotateY = y; <br />rotateZ = z; <br />var rotateX_front = rotateX; <br />varrotateY_front = 電影Y; <br />varrotateZ_front=rotateZ; <br />//可以這個面手機最地 <br />varrotateX_behind =rotateX_front 180; <br />varrotateY_behind=rotateY_front*(-1); <br />varrotateZ_behind=rotateZ_front*(-1); <br />varrotateX_top=rotateX_front 90; <br />varrotateY_top=rotateZ_front; <br />varrotateZ_top=rotateY_front*(-1); <br />varrotateX_bottom=rotateX_front-90; <br />varrotateY_bottom=rotateZ_front*(-1); <br />varrotateZ_bottom=rotateY_front; <br />varrotateX_left=rotateX_front 90; <br />varrotateY_left=rotateZ_front - 90; <br />varrotateZ_left=rotateY_front*(-1); <br />varrotateX_right=rotateX_front 90; <br />varrotateY_right=rotateZ_front 90; <br />varrotateZ_right=rotateY_front*(-1); <br />// 可以下載面的z長電視劇<br />var zIndex_front_default = -1; <br />var zIndex_behind_default = -6; <br />var zIndex_top_default = -5; <br />var zIndex_bottom_default = -2; <br />var zIndex_left_default = -4; <br />var zIndex_right_default = -3; <br />var xI = (rotateX_front / 90) % 4; <br />var yI = (rotateY_front / 90) % 4; <br />var zI = (rotateZ_front / 90) % 4; <br />var zIndex_matrix = new Array(); <br />for(var i = 0; i < 3;i ) { <br />zIndex_matrix.push(new Array()); <br />} <br />zIndex_matrix = [["","zIndex_top",""], <br />["zIndex_left","zIndex_front","zIndex_right"], <br />["zIndex_front","zIndex_right"], <br />[" “”]]; <br />var zIndex_matrix_behind = "zIndex_behind"; <br />//計算zIndex <br />if((xI >= 0 && xI < 1) ||(xI >= -4 && xI < -3)) { <br />} else if(( xI >= 1 && xI < 2) ||(xI >= -3 && xI < -2)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1]; <br />zIndex_matrix[0][1] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((xI >= 2 && xI < 3) ||(xI >= -2 && xI < -1)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1] ; <br />zIndex_matrix[0][1] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix_tmp; <br />zIndex_matrix_tmp = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((xI >= 3 && xI < 4) ||(xI >= -1 && xI < 0)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1]; <br />zIndex_matrix[0][1] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_tmp; <br />} <br />if((yI > 0 && yI <= 1) ||(yI > -4 && yI <= -3)) { <br />var zIndex_matrix_tmp = zIndex_matrix[1][ 0]; <br />zIndex_matrix[1][0] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_tmp; <br />} else if((yI > 1 && yI <= 2) ||(yI > -3 && yI <= -2)) { <br />var zIndex_matrix_tmp = zIndex_matrix[1][0] ; <br />zIndex_matrix[1][0] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_tmp; <br />zIndex_matrix_tmp = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((yI > 2 && yI <= 3) ||(yI > -2 && yI <= -1)) { <br />var zIndex_matrix_tmp = zIndex_matrix[1][0] ; <br />zIndex_matrix[1][0] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((yI > 3 && yI <= 4) ||(yI > -1 && yI <= 0)) { <br />} <br /><br />if((zI > 0 && zI <= 1) ||(zI > -4 && zI <= -3)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1]; <br />zIndex_matrix[0][1] = zIndex_matrix[1][0]; <br />zIndex_matrix[1][0] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_tmp; <br />} else if((zI > 1 && zI <= 2) ||(zI > -3 && zI <= -2)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1] ; <br />zIndex_matrix[0][1] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix_tmp; <br />zIndex_matrix_tmp = zIndex_matrix[1][0]; <br />zIndex_matrix[1][0] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_tmp; <br />} else if((zI > 2 && zI <= 3) ||(zI > -2 && zI <= -1)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1] ; <br />zIndex_matrix[0][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix[1][0]; <🎜>zIndex_matrix[1][0] = zIndex_matrix_tmp; <br />} else if((zI > 3 && zI <= 4) ||(zI > -1 && zI <= 0)) { <br />} <br />//赋值zIndex <br /> eval(zIndex_matrix[0][1] "= zIndex_top_default); <br />eval(zIndex_matrix[1][0] "= zIndex_left_default); <br />eval(zIndex_matrix[1][1] "= zIndex_front_default); <br />eval(zIndex_matrix[1][2] "= zIndex_right_default); <br />eval(zIndex_matrix[2][1] "= zIndex_bottom_default); <br />eval(zIndex_matrix_behind "= zIndex_behind_default); <br />//front <br />vartransform_rotate_front = "perspective(500px) 回転X(" 回転X_フロント <br />"度) 回転Y(" 回転Y_フロント <br />"度) 回転Z(" 回転Z_フロント "度)"; <br />div_front.style.webkitTransform =transform_rotate_front; <br />div_front.style.zIndex = zIndex_front; <br />//behind <br />vartransform_rotate_behind = "perspective(500px)rotateX("rotateX_behind<br />"度)rotateY("rotateY_behind <br />"度)rotateZ("rotateZ_behind "度)"; <br />div_behind.style.webkitTransform =transform_rotate_behind; <br />div_behind.style.zIndex = zIndex_behind; <br />//left <br />vartransform_rotate_left = "perspective(500px)rotateX("rotateX_left <br />"度)rotateZ("rotateZ_left <br />"度)rotateY("rotateY_left "度)"; <br />div_left.style.webkitTransform =transform_rotate_left; <br />div_left.style.zIndex = zIndex_left; <br />//right <br />vartransform_rotate_right = "perspective(500px)rotateX("rotateX_right <br />"度)rotateZ("rotateZ_right <br />"度)rotateY("rotateY_right "度)"; <br />div_right.style.webkitTransform =transform_rotate_right; <br />div_right.style.zIndex = zIndex_right; <br />//top <br />vartransform_rotate_top = "perspective(500px) 回転X(" 回転X_トップ <br />"度) 回転Z(" 回転Z_トップ <br />"度) 回転Y(" 回転Y_トップ "度)"; <br />div_top.style.webkitTransform =transform_rotate_top; <br />div_top.style.zIndex = zIndex_top; <br />//bottom <br />vartransform_rotate_bottom = "perspective(500px)rotateX("rotateX_bottom <br />"度)rotateZ("rotateZ_bottom <br />"度)rotateY("rotateY_bottom "度)"; <br />div_bottom.style.webkitTransform =transform_rotate_bottom; <br />div_bottom.style.zIndex = zIndex_bottom; <br />}; <br />//长方体の长、宽、高 <br />this.resize = function(new_long, new_width, new_height) <br />{ <br />long = new_long; <br />幅 = 新しい幅; <br />高さ = 新しい高さ; <br />this.refresh(); <br />}; <br />//长方体の位置を重ね置きします <br />this.move = function(new_left,new_top) { <br />top = new_top; <br />left = new_left; <br />this.refresh(); <br />}; <br />} <br /><br />function transform() { <br />cuboid.resize(parseInt(document.getElementById("long").value), <br />parseInt(document.getElementById("width"). value)、<br />parseInt(document.getElementById("height").value)); <br />cuboid.move(parseInt(document.getElementById("left").value), <br />parseInt(document.getElementById("top").value)); <br />cuboid.rotate(parseInt(document.getElementById("rotatex").value), <br />parseInt(document.getElementById("rotatey").value), <br />parseInt(document.getElementById("rotatez" )。価値)); <br />//cuboid.refresh(); <br />} <br /></script>

左:px

上:< ;/input>px

long:px

width:px

高さ:px

rotateX: deg

rotateY: deg

rotateZ: deg






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();

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板