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


Posted in HTML / CSS onAugust 22, 2014

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

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

源码直接贡献啦:

复制代码
代码如下:

<style>
.cuboid_side_div{
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;
//定义长方体六个面的div对象
var div_front;
var div_behind;
var div_left;
var div_right;
var div_top;
var div_bottom;

////////////////////////////////////////
//初始化长方体
///////////////////////////////////////
//根据初始位置构造六个面。
this.init = function() {
//创建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);
//创建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);
//创建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);
//创建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);
//创建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);
//创建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);
};
//旋转立方体
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; 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>

HTML / CSS 相关文章推荐
纯CSS3实现表单验证效果(非常不错)
Jan 18 HTML / CSS
使用CSS3制作一个简单的Chrome模拟器
Jul 15 HTML / CSS
浅谈css3中calc在less编译时被计算的解决办法
Dec 04 HTML / CSS
详解如何使用CSS3中的结构伪类选择器和伪元素选择器
Jan 06 HTML / CSS
Bootstrap 学习分享
Nov 12 HTML / CSS
利用HTML5画出一个坦克的形状具体实现代码
Jun 20 HTML / CSS
极简的HTML5模版
Jul 09 HTML / CSS
简述Html5 IphoneX 适配方法
Feb 08 HTML / CSS
HTML5中使用json对象的实例代码
Sep 10 HTML / CSS
使用canvas一步步实现图片打码功能的方法
Jun 17 HTML / CSS
前后端结合实现amazeUI分页效果
Aug 21 HTML / CSS
css3实现背景图片颜色修改的多种方式
Apr 13 HTML / CSS
html5 更新图片颜色示例代码
Jul 29 #HTML / CSS
Html5 语法与规则简要概述
Jul 29 #HTML / CSS
html5桌面通知(Web Notifications)实例解析
Jul 07 #HTML / CSS
HTML5实现WebSocket协议原理浅析
Jul 07 #HTML / CSS
HTML5新增的表单元素和属性实例解析
Jul 07 #HTML / CSS
HTML5+CSS3实现拖放(Drag and Drop)示例
Jul 07 #HTML / CSS
html5的自定义data-*属性与jquery的data()方法的使用
Jul 02 #HTML / CSS
You might like
地球防卫队:陪着奥特曼打小怪兽的人类力量 那些经典队服
2020/03/08 日漫
PHP 处理图片的类实现代码
2009/10/23 PHP
PHP写UltraEdit插件脚本实现方法
2011/12/26 PHP
PHP使用flock实现文件加锁的方法
2015/07/01 PHP
php开发工具有哪五款
2015/11/09 PHP
浅谈PHP中try{}catch{}的使用方法
2016/12/09 PHP
php实现数据库的增删改查
2017/02/26 PHP
PHP实现动态创建XML文档的方法
2018/03/30 PHP
PHPMailer ThinkPHP实现自动发送邮件功能
2018/06/10 PHP
JQuery AJAX实现目录浏览与编辑的代码
2008/10/21 Javascript
修改jquery里的dialog对话框插件为框架页(iframe) 的方法
2010/09/14 Javascript
JS弹出对话框返回值代码(asp.net后台)
2010/12/28 Javascript
Js+Flash实现访问剪切板操作
2012/11/20 Javascript
使用upstart把nodejs应用封装为系统服务实例
2014/06/01 NodeJs
jQuery判断一个元素是否可见的方法
2015/06/05 Javascript
基于jQuery的网页影音播放器jPlayer的基本使用教程
2016/03/08 Javascript
AngularJS实现按钮提示与点击变色效果
2016/09/07 Javascript
jQuery实现遮罩层登录对话框
2016/12/29 Javascript
详解在Vue中有条件地使用CSS类
2017/09/30 Javascript
解决jquery的ajax调取后端数据成功却渲染失败的问题
2018/08/08 jQuery
layui radio性别单选框赋值方法
2018/08/15 Javascript
js实现简单分页导航栏效果
2019/06/28 Javascript
[33:42]LGD vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[53:10]Secret vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python实现统计单词出现的个数
2015/05/28 Python
Python实现删除列表中满足一定条件的元素示例
2017/06/12 Python
Python实现从log日志中提取ip的方法【正则提取】
2018/03/31 Python
Python迭代器与生成器基本用法分析
2018/07/26 Python
python一键去抖音视频水印工具
2018/09/14 Python
python 多线程串行和并行的实例
2019/02/22 Python
python解压zip包中文乱码解决方法
2020/11/27 Python
matplotlib制作雷达图报错ValueError的实现
2021/01/05 Python
大学生毕业求职找工作的自我评价
2013/09/29 职场文书
百万英镑观后感
2015/06/09 职场文书
承诺书的内容有哪些,怎么写?
2019/06/21 职场文书
redis内存空间效率问题的深入探究
2021/05/17 Redis