HTML5 transform三维立方体(带旋转效果)

为了更好得掌握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<br>
	top:<input id="top" value="50"></input>px<br>
	long:<input id="long" value="100"></input>px<br>
	width:<input id="width" value="60"></input>px<br>
	height:<input id="height" value="80"></input>px<br>
	rotateX: <input id="rotatex" value="0"></input>deg<br>
	rotateY: <input id="rotatey" value="0"></input>deg<br>
	rotateZ: <input id="rotatez" value="0"></input>deg<br>
	<input type="button" value="确定" onclick="transform()"></input><br>
	<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>

HTML5 transform三维立方体(带旋转效果)

时间: 2024-10-05 08:41:18

HTML5 transform三维立方体(带旋转效果)的相关文章

CSS3景深、三维变换属性及旋转三维立方体的实现

浏览器坐标系 在讲正式语法之前,首先需要了解浏览器坐标系 这需要我们把浏览器界面想象成一个立体的场景 这是网上流传很广的浏览器坐标系图片 从左到右的方向是浏览器x轴的正方向 从上到下的方向是浏览器y轴的正方向 而z轴正方向是面对于我们的 了解这个很重要,因为下面我们旋转元素需要借助它来理解 3D旋转 我们在平面中使用的旋转只是单纯的让元素在平面旋转一定角度 在三维旋转中稍微要复杂一下 属性当然还是用我们的transform 三维旋转有下面三个函数分别对应三个维度的旋转 rotateX(xxdeg

精美的HTML5/CSS3表单 带小图标

今天我们要来分享一款非常精美的HTML5/CSS3表单,准备地说,这是一款经过美化的input输入表单,每一个输入表单都可以定义其两侧的小图标,非常华丽.另外,这款表单应用还采用了3种不同的风格主题,你可以在演示页的菜单栏中选择一种样式.需要高版本的浏览器才能支持. 你也可以在这里在线演示 下面我们来简单分析一下这款表单的源代码,源代码由HTML代码.CSS代码及Javascript代码组成.一共3组样式,我们只对其中一组作解说. HTML代码: <ul data-for="prefix&

【HTML5】3D立方体旋转

http://www.787866.com/2083.html 要想实现3D的效果,其实非常简单,只需指定一个元素为容器并设置transform-style:preserve-3d,那么它的后代元素便会有3D效果.不过有很多需要注意的地方,这里把我学习的方法,过程分享给大家.再讲知识点之前,还是先弄清楚3D的坐标系吧,从网上搜了一张经典坐标系图,供大家回顾一下. 1.3D试图 transform-style:flat(默认,二维效果) / preserve-3d(三维效果).设置一个元素的tra

css3 div实现三维立体圆环旋转效果

圆环最好设计为扫描特效,转起来会很有感觉 1.首先创建一个div <div class="companydiV">            <img class="imgcompany" src="../../static/img/centerroll.png" > </div> 2.css样式 .companydiV{    position: absolute;    top: -130px;    /* l

简单的HTML5音乐播放器(带歌词滚动)

首先需要整理好lrc格式的歌词放到script标签中以供程序处理.然后把音乐链接放到audio的src属性里就可以了. 源码: HTML部分 1 <div class="container"> 2 <audio id="player" src="test.mp3" loop controls preload></audio> 3 <div id="lrcArea"></di

html5 表单 自带验证

required="required" 必填 placeholder="xxx-xxxx-xxxx" // 提示 pattern="\d{3}-\d{4}-\d{4} //正则匹配 autofocus // 指针 <fieldset> <legend>Expectations:</legend> // 提示:<legend> 标签为 <fieldset> 元素定义标题. </fieldset

html5 文件上传 带进度条

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

CSS3D变换/立方体旋转效果

3D变换基于几个比较重要的属性,perspective,translateZ,preserve-3d; transform-style(preserve-3d)建立3D空间 perspective视镜 perspective-origin视镜基点 x:left/center/right/length/% y:top/center/bottom/length/% transform新增函数 rotateX():如果值为正值,元素围绕X轴顺时针旋转:反之,如果值为负值,元素围绕X轴逆时针旋转. ro

HTML5多图片拖拽上传带进度条

前言 昨天利用css2的clip属性实现了网页进度条觉得还不错,但是很多情况下,我们在那些时候用进度条呢,一般网页加载的时候如果有需要可以用,那么问题就来了,怎么才算整个加载完毕呢,是页面主要模块加载完毕,还是window.onload之后算呢,对这些方面,我真不敢随意回答,因业务需求而定,本文想要说的是在图片上传的时候用到的进度条. 效果展示 详细参考请移步至html5demo HTML5 新增的拖拽相关事件说明 1.ondragover 效果图演示是所看见的可以将文件不仅仅是图片拖拽到该di