javascript - HTML5 Canvas - 添加更多圆圈以圆周运动旋转

标签 javascript html canvas rotation geometry

我尝试使用 HTML5 Canvas 在循环中添加更多圆圈,但似乎不起作用。我希望其他圆圈能够跟随该圆圈已经在那里旋转。我还想知道如何使圆周运动非线性(也就是说,它以不同的速度移动,就像它有缓动一样)。

你们能帮忙吗? :/非常感谢。下面是我的代码。

<canvas id="canvas" width="450" height="450"></canvas>


            <script type="text/javascript">

        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        var w = canvas.width;
        var h = canvas.height;
        var dd = 4;
        var angle = 0;
        /*var cx = 197;

        var cy = 199;
        var radius = 200;*/
        var cx = w/2;

        var cy = h/2;
        var radius = 200;



        function draw(x, y) {
            ctx.fillStyle = "rgb(38,161,220)";
            ctx.strokeStyle = "rgb(38,161,220)";
            ctx.clearRect(0, 0, w, h);
            ctx.save();
            ctx.beginPath();
            ctx.beginPath();
            //ctx.rect(x - 30 / 2, y - 30 / 2, 50, 30);

            // Circle 1
            ctx.arc(x-1/2, y-1/2, 10, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();


            ctx.restore();
        };



            /**  context.beginPath();

              context.fillStyle = 'green';
              context.fill();
              context.lineWidth = 5;
              context.strokeStyle = '#003300';
              context.stroke(); **/


        var fps = 120;

        window.requestAnimFrame = (function (callback) {
            return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
                window.setTimeout(callback, 1000 / fps);
            };
        })();


        function animate() {
            setTimeout(function () {
                requestAnimFrame(animate);

                // increase the angle of rotation
                //angle += Math.acos(1-Math.pow(dd/radius,2)/2);

                angle += Math.acos(1-Math.pow(dd/radius,2)/2);


                // calculate the new ball.x / ball.y
                var newX = cx + radius * Math.cos(angle);
                var newY = cy + radius * Math.sin(angle);
                // draw
                draw(newX, newY);


                // draw the centerpoint 
                ctx.beginPath();
                ctx.arc(cx, cy, radius, 0, Math.PI * 2, false);
                ctx.closePath();
                ctx.stroke();


            }, 1000 / fps );
        }
        animate();

        </script>

最佳答案

在哪里和哪里之间缓和?

这里有一些非线性 Angular 速度:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<canvas id="canvas" width="450" height="450"></canvas>


            <script type="text/javascript">

        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        var w = canvas.width;
        var h = canvas.height;
        var dd = 4;
        var angle = 0;
        var cx = w/2;
        var t = 0;
        var velocity = 0.01;
        var cy = h/2;
        var radius = 200;



        function draw(x, y) {

            ctx.fillStyle = "rgb(38,161,220)";
            ctx.strokeStyle = "rgb(38,161,220)";
            ctx.clearRect(0, 0, w, h);
            ctx.save();
            ctx.beginPath();
            ctx.beginPath();
            ctx.arc(x-1/2, y-1/2, 10, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
            ctx.restore();

        };

        var fps = 120;

        window.requestAnimFrame = (function (callback) {
            return window.requestAnimationFrame || window.webkitRequestAnimationFrame ||                                  window.mozRequestAnimationFrame || window.oRequestAnimationFrame ||  window.msRequestAnimationFrame || function (callback) {
                window.setTimeout(callback, 1000 / fps);
            };
        })();


        function animate() {
            setTimeout(function () {

                // increase the angle of rotation

                angle += velocity;

                //sinusoidal velocity

                velocity += 0.005 * (Math.sin(t));
                t += 0.1;

                 // randomzed velocity:

                //velocity += 0.001 * (Math.random() - 1);


                // draw
                // calculate the new ball.x / ball.y
                var newX = cx + radius * Math.cos(angle);
                var newY = cy + radius * Math.sin(angle);


                draw(newX, newY);


                // draw the centerpoint
                ctx.beginPath();
                ctx.arc(cx, cy, radius, 0, Math.PI * 2, false);
                ctx.closePath();
                ctx.stroke();

                requestAnimFrame(animate);


            }, 1000 / fps );
        }
        animate();

        </script>
</body>
</html>

你可以像这样创建一个圆圈类:

var Circle = function(radius,velocity,etc){
    this.radius = radius
    this.velocity = velocity
    this.etc = etc

    // and whatever other properties you think you need

}

然后

var circleArray = []

for(var i = 0; i < circleCount; i++){
    circleArray.push(new Circle(2,0.1,"some_property"))    
}

然后在 animate() 中:

circleArray.forEach(function(circle){

    //drawing code

})

除非您提出更具体的问题,否则我只能告诉您

关于javascript - HTML5 Canvas - 添加更多圆圈以圆周运动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20533405/

相关文章:

HTML 输入字段自定义

javascript - 如何在 fabric.js 中获取目标元素的属性值

Android:如何在 Canvas 顶部的 x,y 位置放置按钮

javascript - 计算平均数为 10

JavaScript 替换多个 if 语句

javascript - .ejs 模板中的 Bootstrap 分页

php - 我需要从我的网站上的表单中保存电子邮件,但出于某种原因我无法保存它

html - 当最大宽度阈值起作用时,按钮内的图标向右移动

java - 使用字符串作为值名称和输入来查找文件

javascript - document.RemoveChild 在 JavaScript 中不起作用