软件开发|编程技术|编程代码|编程入门先学什么—程序设计语言

HTML5 transform三维立方体实现360无死角三维旋转效

transform,三维,立方体为了更好得掌握transform的精髓,所以决定完成三维立方体的模型,可以实现360无死角的三维旋转效果。

但是旋转时判断每个面的视图顺序比较困难,仍未完美解决,希望有人能解答!

源码直接贡献啦:

复制代码

代码如下:


style
.cuboid_side_p{
position:absolute;
border:1px solid #333;
-webkit-transition:ease all 1s;
}
/style
script
/**
* 本版本存在以下问题:
* 三维旋转的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;
//初始化变换角度,默认为0
var rotateX = 0;
var rotateY = 0;
var rotateZ = 0;
var zIndex = 0;
//定义长方体六个面的p对象
var p_front;
var p_behind;
var p_left;
var p_right;
var p_top;
var p_bottom;

////////////////////////////////////////
//初始化长方体
///////////////////////////////////////
//根据初始位置构造六个面。
this.init = function() {
//创建front p
p_front = document.createElement("p");
p_front.className = "cuboid_side_p";
p_front.innerHTML = "p front";
p_front.style.backgroundColor="#f1b2b2";
document.body.appendChild(p_front);
//创建behind p
p_behind = document.createElement("p");
p_behind.className = "cuboid_side_p";
p_behind.innerHTML = "p behind";
p_behind.style.backgroundColor="#bd91eb";
document.body.appendChild(p_behind);
//创建left p
p_left = document.createElement("p");
p_left.className = "cuboid_side_p";
p_left.innerHTML = "p left";
p_left.style.backgroundColor="#64a3c3";
document.body.appendChild(p_left);
//创建right p
p_right = document.createElement("p");
p_right.className = "cuboid_side_p";
p_right.innerHTML = "p right";
p_right.style.backgroundColor="#78e797";
document.body.appendChild(p_right);
//创建top p
p_top = document.createElement("p");
p_top.className = "cuboid_side_p";
p_top.innerHTML = "p top";
p_top.style.backgroundColor="#e7db78";
document.body.appendChild(p_top);
//创建bottom p
p_bottom = document.createElement("p");
p_bottom.className = "cuboid_side_p";
p_bottom.innerHTML = "p bottom";
p_bottom.style.backgroundColor="#e79c78";
document.body.appendChild(p_bottom);
this.refresh();
};
//重绘
this.refresh = function()
{
//定义p_front样式
p_front.style.left = left+"px";
p_front.style.top = top+"px";
p_front.style.width = long +"px";
p_front.style.height = height +"px";
p_front.style.webkitTransformOrigin = "50% 50% "+((-1)*width / 2)+"px";
//定义p_behind样式
p_behind.style.left = left+"px";
p_behind.style.top = top+"px";
p_behind.style.width = p_front.style.width;
p_behind.style.height = p_front.style.height;
p_behind.style.webkitTransformOrigin = "50% 50% "+((-1)*width / 2)+"px";
//定义p_left样式
p_left.style.left = left+((long - height) / 2)+"px";
p_left.style.top = top + ((height - width) / 2)+"px";
p_left.style.width = height +"px";
p_left.style.height = width +"px";
p_left.style.webkitTransformOrigin = "50% 50% "+((-1) * long /2 )+"px";
//定义p_right样式
p_right.style.left = p_left.style.left;
p_right.style.top = p_left.style.top;
p_right.style.width = p_left.style.width;
p_right.style.height = p_left.style.height;
p_right.style.webkitTransformOrigin = "50% 50% "+((-1) * long /2 )+"px";
//定义p_top样式
p_top.style.left = left+"px";
p_top.style.top = top+((height - width)/ 2)+"px";
p_top.style.width = long +"px";
p_top.style.height = width +"px";
p_top.style.webkitTransformOrigin = "50% 50% "+((-1) * height /2 )+"px";
//定义p_bottom样式
p_bottom.style.left = p_top.style.left;
p_bottom.style.top = p_top.style.top;
p_bottom.style.width = p_top.style.width;
p_bottom.style.height = p_top.style.height;
p_bottom.style.webkitTransformOrigin = "50% 50% "+((-1) * height /2 )+"px";
this.rotate(rotateX,rotateY,rotateZ);
};
//旋转立方体
this.rotate = function(x,y,z) {
rotateX = x;
rotateY = y;
rotateZ = z;
var rotateX_front = rotateX;
var rotateY_front = rotateY;
var rotateZ_front = rotateZ;
//判断各个面旋转角度
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; i3;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 = 0xI1) ||(xI = -4xI-3)) {
} else if((xI = 1xI2) ||(xI = -3xI-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 = 2xI3) ||(xI = -2xI-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 = 3xI4) ||(xI = -1xI0)) {
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((yI0yI = 1) ||(yI-4yI = -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((yI1yI = 2) ||(yI-3yI = -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((yI2yI = 3) ||(yI-2yI = -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((yI3yI = 4) ||(yI-1yI = 0)) {
}

if((zI0zI = 1) ||(zI-4zI = -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((zI1zI = 2) ||(zI-3zI = -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((zI2zI = 3) ||(zI-2zI = -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((zI3zI = 4) ||(zI-1zI = 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)";
p_front.style.webkitTransform = transform_rotate_front;
p_front.style.zIndex = zIndex_front;
//behind
var transform_rotate_behind = "perspective(500px) rotateX("+rotateX_behind+
"deg) rotateY("+rotateY_behind+
"deg) rotateZ("+rotateZ_behind+"deg)";
p_behind.style.webkitTransform = transform_rotate_behind;
p_behind.style.zIndex = zIndex_behind;
//left
var transform_rotate_left = "perspective(500px) rotateX("+rotateX_left+
"deg) rotateZ("+rotateZ_left+
"deg) rotateY("+rotateY_left+"deg)";
p_left.style.webkitTransform = transform_rotate_left;
p_left.style.zIndex = zIndex_left;
//right
var transform_rotate_right = "perspective(500px) rotateX("+rotateX_right+
"deg) rotateZ("+rotateZ_right+
"deg) rotateY("+rotateY_right+"deg)";
p_right.style.webkitTransform = transform_rotate_right;
p_right.style.zIndex = zIndex_right;
//top
var transform_rotate_top = "perspective(500px) rotateX("+rotateX_top+
"deg) rotateZ("+rotateZ_top+
"deg) rotateY("+rotateY_top+"deg)";
p_top.style.webkitTransform = transform_rotate_top;
p_top.style.zIndex = zIndex_top;
//bottom
var transform_rotate_bottom = "perspective(500px) rotateX("+rotateX_bottom+
"deg) rotateZ("+rotateZ_bottom+
"deg) rotateY("+rotateY_bottom+"deg)";
p_bottom.style.webkitTransform = transform_rotate_bottom;
p_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
p style="position:absolute;border:1px solid #333;top:240px;left:100px;width:1000px;height: 360px;"
left:input id="left" value="100"/inputpx

top:input id="top" value="50"/inputpx

long:input id="long" value="100"/inputpx

width:input id="width" value="60"/inputpx

height:input id="height" value="80"/inputpx

rotateX: input id="rotatex" value="0"/inputdeg

rotateY: input id="rotatey" value="0"/inputdeg

rotateZ: input id="rotatez" value="0"/inputdeg

input type="button" value="确定" onclick="transform()"/input

label id="status"/label
/p
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