HTML5画布旋转平面

直接看代码哈

window.requestAnimFrame = (function(callback){
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback){
        window.setTimeout(callback, 1000 / 60);
    };
})();
 
function animate(plane, lastTime){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
 
    // update
    var date = new Date();
    var time = date.getTime();
    var timeDiff = lastTime - time;
    plane.theta += timeDiff * plane.rotationSpeed / 1000;
    plane.tilt = Math.sin(plane.theta / 2);
 
    // clear
    context.clearRect(0, 0, canvas.width, canvas.height);
 
    // draw
    context.save();
    context.translate(canvas.width / 2, canvas.height / 2);
    context.scale(1, plane.tilt);
    context.rotate(plane.theta);
    var planeColor = plane.tilt >= 0 ? "red" : "blue";
 
    context.fillStyle = planeColor;
    context.fillRect(-plane.width / 2, -plane.height / 2, plane.width, plane.height);
    context.restore();
 
    // request new frame
    requestAnimFrame(function(){
        animate(plane, time);
    });
}
 
window.onload = function(){
    var plane = {
        width: 200,
        height: 100,
        theta: 0,
        tilt: 1,
        rotationSpeed: 2
    };
 
    var date = new Date();
    var time = date.getTime();
    animate(plane, time);
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在HTML文件中添加一个画布元素,并为其设置一个ID: ```html <canvas id="myCanvas"></canvas> ``` 然后,使用JavaScript获取该画布元素,并将其上下文设置为“3d”: ```javascript var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("3d"); ``` 接下来,定义一个立方体对象,包含八个顶点和十二个边。此处使用的是一个简单的立方体,可以根据需要进行修改: ```javascript var cube = { vertices: [ {x: -50, y: -50, z: -50}, {x: -50, y: 50, z: -50}, {x: 50, y: 50, z: -50}, {x: 50, y: -50, z: -50}, {x: -50, y: -50, z: 50}, {x: -50, y: 50, z: 50}, {x: 50, y: 50, z: 50}, {x: 50, y: -50, z: 50} ], edges: [ [0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6, 7], [7, 4], [0, 4], [1, 5], [2, 6], [3, 7] ] }; ``` 接下来,定义一个函数来绘制立方体。该函数将遍历立方体的每个顶点,并将其投影到2D平面上。然后,将每个顶点连接起来,形成立方体的边: ```javascript function drawCube(cube, angle) { // 清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 定义旋转角度 var rad = angle * Math.PI / 180; var cos = Math.cos(rad); var sin = Math.sin(rad); // 遍历每个顶点,并将其投影到2D平面上 var points = []; for (var i = 0; i < cube.vertices.length; i++) { var vertex = cube.vertices[i]; var x = vertex.x; var y = vertex.y * cos - vertex.z * sin; var z = vertex.y * sin + vertex.z * cos; points.push({x: x, y: y, z: z}); } // 绘制立方体的边 ctx.beginPath(); for (var i = 0; i < cube.edges.length; i++) { var edge = cube.edges[i]; var p1 = points[edge[0]]; var p2 = points[edge[1]]; ctx.moveTo(p1.x + canvas.width / 2, p1.y + canvas.height / 2); ctx.lineTo(p2.x + canvas.width / 2, p2.y + canvas.height / 2); } ctx.stroke(); } ``` 最后,在主函数中调用drawCube()函数,并使用setInterval()函数来连续旋转立方体: ```javascript var angle = 0; setInterval(function() { drawCube(cube, angle); angle += 1; }, 10); ``` 完整的代码如下: ```html <canvas id="myCanvas"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("3d"); var cube = { vertices: [ {x: -50, y: -50, z: -50}, {x: -50, y: 50, z: -50}, {x: 50, y: 50, z: -50}, {x: 50, y: -50, z: -50}, {x: -50, y: -50, z: 50}, {x: -50, y: 50, z: 50}, {x: 50, y: 50, z: 50}, {x: 50, y: -50, z: 50} ], edges: [ [0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6, 7], [7, 4], [0, 4], [1, 5], [2, 6], [3, 7] ] }; function drawCube(cube, angle) { ctx.clearRect(0, 0, canvas.width, canvas.height); var rad = angle * Math.PI / 180; var cos = Math.cos(rad); var sin = Math.sin(rad); var points = []; for (var i = 0; i < cube.vertices.length; i++) { var vertex = cube.vertices[i]; var x = vertex.x; var y = vertex.y * cos - vertex.z * sin; var z = vertex.y * sin + vertex.z * cos; points.push({x: x, y: y, z: z}); } ctx.beginPath(); for (var i = 0; i < cube.edges.length; i++) { var edge = cube.edges[i]; var p1 = points[edge[0]]; var p2 = points[edge[1]]; ctx.moveTo(p1.x + canvas.width / 2, p1.y + canvas.height / 2); ctx.lineTo(p2.x + canvas.width / 2, p2.y + canvas.height / 2); } ctx.stroke(); } var angle = 0; setInterval(function() { drawCube(cube, angle); angle += 1; }, 10); </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值