0
Q
3D动画位图
A
回答
0
这里是一个旋转3d立方体的2D模拟。
希望我能说我做到了我自己,但我原本来到这里的代码:http://codentronix.com/2011/05/10/html5-experiment-a-rotating-solid-cube/
这里是代码和一个小提琴:http://jsfiddle.net/m1erickson/3PwYm/
<!DOCTYPE html>
<html>
<head>
<title>HTML5 Experiment: A Rotating Solid Cube</title>
<script type="text/javascript">
window.onload = startDemo;
function Point3D(x,y,z) {
this.x = x;
this.y = y;
this.z = z;
this.rotateX = function(angle) {
var rad, cosa, sina, y, z
rad = angle * Math.PI/180
cosa = Math.cos(rad)
sina = Math.sin(rad)
y = this.y * cosa - this.z * sina
z = this.y * sina + this.z * cosa
return new Point3D(this.x, y, z)
}
this.rotateY = function(angle) {
var rad, cosa, sina, x, z
rad = angle * Math.PI/180
cosa = Math.cos(rad)
sina = Math.sin(rad)
z = this.z * cosa - this.x * sina
x = this.z * sina + this.x * cosa
return new Point3D(x,this.y, z)
}
this.rotateZ = function(angle) {
var rad, cosa, sina, x, y
rad = angle * Math.PI/180
cosa = Math.cos(rad)
sina = Math.sin(rad)
x = this.x * cosa - this.y * sina
y = this.x * sina + this.y * cosa
return new Point3D(x, y, this.z)
}
this.project = function(viewWidth, viewHeight, fov, viewDistance) {
var factor, x, y
factor = fov/(viewDistance + this.z)
x = this.x * factor + viewWidth/2
y = this.y * factor + viewHeight/2
return new Point3D(x, y, this.z)
}
}
var vertices = [
new Point3D(-1,1,-1),
new Point3D(1,1,-1),
new Point3D(1,-1,-1),
new Point3D(-1,-1,-1),
new Point3D(-1,1,1),
new Point3D(1,1,1),
new Point3D(1,-1,1),
new Point3D(-1,-1,1)
];
// Define the vertices that compose each of the 6 faces. These numbers are
// indices to the vertex list defined above.
var faces = [[0,1,2,3],[1,5,6,2],[5,4,7,6],[4,0,3,7],[0,4,5,1],[3,2,6,7]];
// Define the colors for each face.
var colors = [[255,0,0],[0,255,0],[0,0,255],[255,255,0],[0,255,255],[255,0,255]];
var angle = 0;
/* Constructs a CSS RGB value from an array of 3 elements. */
function arrayToRGB(arr) {
if(arr.length == 3) {
return "rgb(" + arr[0] + "," + arr[1] + "," + arr[2] + ")";
}
return "rgb(0,0,0)";
}
function startDemo() {
canvas = document.getElementById("thecanvas");
if(canvas && canvas.getContext) {
ctx = canvas.getContext("2d");
setInterval(loop,33);
}
}
function loop() {
var t = new Array();
ctx.fillStyle = "rgb(0,0,0)";
ctx.fillRect(0,0,400,250);
for(var i = 0; i < vertices.length; i++) {
var v = vertices[i];
var r = v.rotateX(angle).rotateY(angle);
var p = r.project(400,250,200,4);
t.push(p)
}
var avg_z = new Array();
for(var i = 0; i < faces.length; i++) {
var f = faces[i];
avg_z[i] = {"index":i, "z":(t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z)/4.0};
}
avg_z.sort(function(a,b) {
return b.z - a.z;
});
for(var i = 0; i < faces.length; i++) {
var f = faces[avg_z[i].index]
ctx.fillStyle = arrayToRGB(colors[avg_z[i].index]);
ctx.beginPath()
ctx.moveTo(t[f[0]].x,t[f[0]].y)
ctx.lineTo(t[f[1]].x,t[f[1]].y)
ctx.lineTo(t[f[2]].x,t[f[2]].y)
ctx.lineTo(t[f[3]].x,t[f[3]].y)
ctx.closePath()
ctx.fill()
}
angle += 2
}
</script>
</head>
<body>
<canvas id="thecanvas" width="400" height="250">
Your browser does not support the HTML5 canvas element. <a href=#>Click here</a> to watch the video.
</canvas>
</body>
</html>
相关问题
- 1. Android 3D动画
- 2. wpf 3D动画
- 3. 团结3D:动画
- 4. JavaFX 8 3D动画
- 5. 3D动画 - 观点
- 6. 画布3d图形
- 7. Matlab中的动画3D散点图
- 8. Python:动画3D散点图变慢
- 9. 使用Storyboard动画3D对象动画
- 10. WPF 3d旋转动画
- 11. 3D动画中的Android
- 12. Android 3d翻转动画
- 13. 如何动画3D角色?
- 14. C++ 3D模型动画库?
- 15. android中的3d动画
- 16. 在matplotlib动画3d scatterplot
- 17. Java/J2ME中的3D动画?
- 18. Android的3D动画Imageview
- 19. 如何计算从3D旋转木马动画的视图的位置?
- 20. 将画布定位到3D游戏对象(Unity 3D)
- 21. 在android中动画位图图像
- 22. 沿着曲线的移动点(3D-动画图)
- 23. WPF 3D挤出 “位图”
- 24. 3D等效位图(bmp)
- 25. Android动画 - 将视图动画到特定位置
- 26. AFNetworking占位符图像动画?(下载时动画)
- 27. 我怎会闪式3D动画,Flash只支持2D动画?
- 28. Css背景图像位置动画
- 29. 画布/位图滚动问题
- 30. 绘制位图动画不起作用