-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
181 lines (181 loc) · 6.63 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3d饼图</title>
<style>
body {
background: #000;
margin: 0;
padding: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas">您的浏览器不支持canvas</canvas>
<script>
var cvs = document.getElementById("canvas");
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
var ctx = cvs.getContext("2d");
var fallLength = 50000;//焦距
var centerX = cvs.width / 2;
var centerY = cvs.height / 2;
/**
* @desc 构造一个顶点(三维坐标系下)
* @param x
* @param y
* @param z
* @constructor
*/
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};
}
};
/**
* @desc 构造一个3d饼图
* @param size
* @param width
* @param height
* @constructor
*/
var Pie = function (size, width, height) {
this.oRadius = size / 2;
this.iRadius = size / 2 > width ? size / 2 - width : 0;
this.height = height;
this.faces = [];
};
//将圆均分为180个点
var length = 180;
var increment = Math.PI * 2 / length;
Pie.prototype = {
initVector: function () {
this.oUnderVertors = [];
this.iUnderVertors = [];
this.oTopVertors = [];
this.iTopVertors = [];
var angle = 0;
while (angle <= Math.PI * 2) {
this.oUnderVertors.push(new Vector(this.oRadius * Math.cos(angle), this.oRadius * Math.sin(angle), -this.height / 2));
this.iUnderVertors.push(new Vector(this.iRadius * Math.cos(angle), this.iRadius * Math.sin(angle), -this.height / 2));
this.oTopVertors.push(new Vector(this.oRadius * Math.cos(angle), this.oRadius * Math.sin(angle), this.height / 2));
this.iTopVertors.push(new Vector(this.iRadius * Math.cos(angle), this.iRadius * Math.sin(angle), this.height / 2));
angle += increment;
}
this.oUnderVertors.push(new Vector(this.oRadius, 0, -this.height / 2));
this.iUnderVertors.push(new Vector(this.iRadius, 0, -this.height / 2));
this.oTopVertors.push(new Vector(this.oRadius, 0, this.height / 2));
this.iTopVertors.push(new Vector(this.iRadius, 0, this.height / 2));
},
_draw: function () {
this.faces[0] = new Face(this.oUnderVertors, this.iUnderVertors, 0, Math.PI / 2, "#6c6");
this.faces[1] = new Face(this.oTopVertors, this.iTopVertors, 0, Math.PI / 2, "#6c6");
this.faces[2] = new Face(this.oUnderVertors, this.iUnderVertors, Math.PI / 2, Math.PI / 1.3, "#ff5809");
this.faces[3] = new Face(this.oTopVertors, this.iTopVertors, Math.PI / 2, Math.PI / 1.3, "#ff5809");
this.faces[4] = new Face(this.oUnderVertors, this.iUnderVertors, Math.PI / 1.3, Math.PI * 2, "#d200d2");
this.faces[5] = new Face(this.oTopVertors, this.iTopVertors, Math.PI / 1.3, Math.PI * 2, "#d200d2");
this.faces.sort(function (a, b) {
return b.zIndex - a.zIndex;
});
this.faces.forEach(function (face) {
face.draw();
});
}
};
var Face = function (oVectors, iVectors, startAngle, endAngle, color) {
this.oVectors = oVectors;
this.iVectors = iVectors;
this.startIndex = ~~(startAngle / increment);
this.endIndex = ~~(endAngle / increment);
//this.color = "rgba(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ",0.6)";
this.color = color;
this.zIndex = 0;
for (var i = this.startIndex; i < this.endIndex; i++) {
this.zIndex += this.oVectors[i].z;
this.zIndex += this.iVectors[i].z;
}
this.draw = function () {
ctx.globalAlpha = 0.8;
ctx.beginPath();
ctx.moveTo(this.oVectors[this.startIndex].get2d().x, this.oVectors[this.startIndex].get2d().y);
for (var i = this.startIndex + 1; i < this.endIndex; i++) {
ctx.lineTo(this.oVectors[i].get2d().x, this.oVectors[i].get2d().y);
}
for (var i = this.endIndex; i > this.startIndex; i--) {
ctx.lineTo(this.iVectors[i].get2d().x, this.iVectors[i].get2d().y);
}
ctx.closePath();
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 - cvs.offsetLeft - centerX;
var y = event.clientY - cvs.offsetTop - centerY;
angleY = x * 0.0001;
angleX = y * 0.0001;
});
} else {
window.attachEvent("onmousemove", function (event) {
var x = event.clientX - cvs.offsetLeft - centerX;
var y = event.clientY - cvs.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 (vector) {
var y1 = vector.y * cos - vector.z * sin;
var z1 = vector.z * cos + vector.y * sin;
vector.y = y1;
vector.z = z1;
});
}
function rotateY(vectors) {
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
vectors.forEach(function (vector) {
var x1 = vector.x * cos - vector.z * sin;
var z1 = vector.z * cos + vector.x * sin;
vector.x = x1;
vector.z = z1;
});
}
var pie = new Pie(500, 100, 50);
pie.initVector();
function initAnimate() {
pie._draw();
animate();
}
function animate() {
ctx.clearRect(0, 0, cvs.width, cvs.height);
rotateY(pie.oUnderVertors);
rotateY(pie.iUnderVertors);
rotateY(pie.oTopVertors);
rotateY(pie.iTopVertors);
rotateX(pie.oUnderVertors);
rotateX(pie.iUnderVertors);
rotateX(pie.oTopVertors);
rotateX(pie.iTopVertors);
pie._draw();
requestAnimationFrame(animate);
}
initAnimate();
</script>
</body>
</html>