javascript - 我想用canvas画圆周运动的点动画,但是js代码没有反映,请问问题在哪?
巴扎黑
巴扎黑 2017-04-11 11:36:10
[JavaScript讨论组]

我想能让一个点按照圆周运动,可是写了没有反应,想了很久不知道问题在哪。
代码如下




    
    
    Title
    





求指教!

巴扎黑
巴扎黑

全部回复(2)
迷茫
  1. setInterval不能这样传参的。

    setInterval(function() {
     draw_cirlcle(ctx, vX, radius)
    }, 40);
    或者
    setInterval(draw_cirlcle, 40, ctx, vX, radius);
    
  2. 这里不需要传参,否则每次调用draw_cirlcle方法时vX值不是固定了嘛。

  3. 画圆周运动应该根据角度/弧度变化啊

巴扎黑
<!DOCTYPE html>
<html>
<head>
    <title>A basic HTML5 blog homepage</title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="jquery.min.js"></script>
    <style type="text/css">
        canvas{
            background: #ccc;
            display: block;
            /*margin:50px auto;*/
            /*border: solid 1px #ccc;*/
        }
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            height:100%;
            width:100%;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            var canvas = $('#canvas');
            var context = canvas.get(0).getContext('2d');
            var canvasWidth = canvas.width();
            var canvasHeight = canvas.height();
            var playAnimation = true;
            var startButton=$('#startAnimation');
            var stopButton=$('#stopAnimation');
            startButton.hide();
            startButton.click(function(){
                $(this).hide();
                stopButton.show();
                playAnimation = true;
                animate();
            });
            stopButton.click(function(){
                $(this).hide();
                startButton.show();
                playAnimation = false;
            });
            var shape = function(x,y,width,height){
                this.x = x;
                this.y = y;
                this.width = width;
                this.height = height;
                /*this.radius = Math.random()*30;*/
//                this.radius  = 30;
//                this.angle = 0;
                this.left = false;
                this.top = false;

            }
            var shapes = new Array();
//            shapes.push(new shape(50,50));
//            shapes.push(new shape(100,100));
//            shapes.push(new shape(150,150));
            for(var i = 0;i< 10;i++){
                var x = Math.random()*250;
                var y = Math.random()*250;
                var width = height = Math.random()*30;
                shapes.push(new shape(x,y,width,height));
            }
            function animate(){
                context.clearRect(0,0,canvasWidth,canvasHeight);
                for(var i=0;i<shapes.length;i++){
//                    shapes[i].x+=Math.random()*4-2;
//                    shapes[i].y+=Math.random()*4-2;
                    var temp = shapes[i];
//                    var x =  temp.x + (temp.radius * Math.cos(temp.angle));
//                    var y =  temp.x + (temp.radius * Math.sin(temp.angle));

//                    temp.angle +=30;
//                    if(temp.angle > 360){
//                        temp.angle = 0;
//                    }
                    context.fillRect(temp.x,temp.y,temp.width,temp.height);
                    if(temp.x < 0){
                        temp.left = false;
                    }else if(temp.x + temp.width > canvasWidth){
                        temp.left = true;
                    }
                    if(temp.y < 0){
                        temp.top = false;
                    }else if(temp.y + temp.height > canvasHeight){
                        temp.top = true;
                    }
                    if(!temp.left){
                        temp.x += 2;
                    }else{
                        temp.x -= 2;
                    };
                    if(!temp.top){
                        temp.y += 2;
                    }else{
                        temp.y -= 2;
                    };
                }
                if(playAnimation){
                    setTimeout(animate,33);
                }
            }
            animate();
        });
    </script>
</head>
<body>
<canvas width="500" height="500" id="canvas"></canvas>
<button id="startAnimation">Start</button>
<button id="stopAnimation">Stop</button>
</body>
</html>

可以参照下正方形沿着圆周运动

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号