<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
body {
margin: 0;
padding: 0;
}
#cas {
margin: 100px auto;
display: block;
}
</style>
<title>3d立方体</title>
</head>
<body>
<canvas id="cas" width="400" height="400">您的浏览器不支持canvas</canvas>
<script>
var canvas = document.getElementById("cas");
var ctx = canvas.getContext("2d");
var fallLength = 500, centerX = canvas.width / 2, centerY = canvas.height / 2;
Array.prototype.foreach = function(callback) {
for (var i = 0; i < this.length; i++) {
callback.apply(this[i], [i])
}
}
var Vector = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this._get2d = function() {
var scale = fallLength / (fallLength + this.z);
var x = centerX + this.x * scale;
var y = centerY + this.y * scale;
return { x: x, y: y };
}
}
var Cube = function(length) {
this.length = length;
this.faces = [];
}
Cube.prototype = {
_initVector: function() {
this.vectors = [];
this.vectors.push(new Vector(-this.length / 2, -this.length / 2, this.length / 2)); //0
this.vectors.push(new Vector(-this.length / 2, this.length / 2, this.length / 2)); //1
this.vectors.push(new Vector(this.length / 2, -this.length / 2, this.length / 2)); //2
this.vectors.push(new Vector(this.length / 2, this.length / 2, this.length / 2)); //3
this.vectors.push(new Vector(this.length / 2, -this.length / 2, -this.length / 2)); //4
this.vectors.push(new Vector(this.length / 2, this.length / 2, -this.length / 2)); //5
this.vectors.push(new Vector(-this.length / 2, -this.length / 2, -this.length / 2)); //6
this.vectors.push(new Vector(-this.length / 2, this.length / 2, -this.length / 2)); //7
},
_draw: function() {
this.faces[0] = new Face(this.vectors[0], this.vectors[1], this.vectors[3], this.vectors[2], "#6c6");
this.faces[1] = new Face(this.vectors[2], this.vectors[3], this.vectors[5], this.vectors[4], "#6cc");
this.faces[2] = new Face(this.vectors[4], this.vectors[5], this.vectors[7], this.vectors[6], "#cc6");
this.faces[3] = new Face(this.vectors[6], this.vectors[7], this.vectors[1], this.vectors[0], "#c6c");
this.faces[4] = new Face(this.vectors[1], this.vectors[3], this.vectors[5], this.vectors[7], "#666");
this.faces[5] = new Face(this.vectors[0], this.vectors[2], this.vectors[4], this.vectors[6], "#ccc");
this.faces.sort(function(a, b) {
return b.zIndex - a.zIndex;
});
this.faces.foreach(function() {
this.draw();
})
}
}
var Face = function(vector1, vector2, vector3, vector4, color) {
this.v1 = vector1;
this.v2 = vector2;
this.v3 = vector3;
this.v4 = vector4;
this.color = color;
this.zIndex = this.v1.z + this.v2.z + this.v3.z + this.v4.z;
this.draw = function() {
ctx.save();
ctx.beginPath();
ctx.moveTo(this.v1._get2d().x, this.v1._get2d().y);
ctx.lineTo(this.v2._get2d().x, this.v2._get2d().y);
ctx.lineTo(this.v3._get2d().x, this.v3._get2d().y);
ctx.lineTo(this.v4._get2d().x, this.v4._get2d().y);
ctx.closePath();
// ctx.fillStyle = "rgba("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+",0.2)";
ctx.fillStyle = this.color;
ctx.fill();
}
}
var angleX = 0.05;
var angleY = 0.05;
if ("addEventListener" in window) {
window.addEventListener("mousemove", function(event) {
var x = event.clientX - canvas.offsetLeft - centerX;
var y = event.clientY - canvas.offsetTop - centerY;
angleY = x * 0.0001;
angleX = y * 0.0001;
});
}
else {
window.attachEvent("onmousemove", function(event) {
var x = event.clientX - canvas.offsetLeft - centerX;
var y = event.clientY - canvas.offsetTop - centerY;
angleY = x * 0.0001;
angleX = y * 0.0001;
});
}
function rotateX(vectors) {
var cos = Math.cos(angleX);
var sin = Math.sin(angleX);
vectors.foreach(function() {
var y1 = this.y * cos - this.z * sin;
var z1 = this.z * cos + this.y * sin;
this.y = y1;
this.z = z1;
});
}
function rotateY(vectors) {
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
vectors.foreach(function() {
var x1 = this.x * cos - this.z * sin;
var z1 = this.z * cos + this.x * sin;
this.x = x1;
this.z = z1;
})
}
cube = new Cube(80);
cube._initVector();
function initAnimate() {
cube._draw();
animate();
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
rotateY(cube.vectors);
rotateX(cube.vectors);
cube._draw();
requestAnimationFrame(animate);
}
initAnimate();
</script>
</body>
</html>
没有合适的资源?快使用搜索试试~ 我知道了~
HTML(超文本标记语言,HyperText Markup Language)是一种标记语言,用于描述网页的结构和内容。它是构建万维网上信息的基础工具之一。 HTML使用标签(tag)来标记文本中的不同部分,并定义其在网页中的结构。每个标签由尖括号`< >`包围,通常成对出现,即有一个开标签和一个闭标签。标签可以包含属性,属性为标签提供附加的信息,以进一步控制或描述标签的内容。 通过使用不同的HTML标签和属性,可以创建各种元素,如标题、段落、图像、链接、表格、表单等,以组织和展示网页的内容。同时,可以使用CSS(层叠样式表)来为HTML元素应用样式和布局,在视觉上美化网页。 需要注意的是,HTML只关注网页的结构和内容,不负责实现网页的交互和动态效果。如果需要添加交互或动态功能,可以使用JavaScript来实现。
资源推荐
资源详情
资源评论
收起资源包目录
3Dcube.rar (1个子文件)
3Dcube.html 5KB
共 1 条
- 1
资源评论
专家-百锦再@新空间代码工作室
- 粉丝: 9066
- 资源: 731
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功