模拟小球环绕运动(使用js的math对象)

image.png

如上图所示,这个demo主要模拟实现行星运动~~~
其中用到js的math对象 还有数学定理三角函数正弦、余弦

代码如下:

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>小球运动</title>
        <style type="text/css" media="screen">
            button{
                position: fixed;
                top: 30px;
                left: 20%;
            }
         .earth{
            position: relative;
            left: 50%;
            margin-left: -125px;
            top: 50%;
            margin-top: 190px;
            width: 250px;
            height: 250px;
            border-radius: 50%;
            background: linear-gradient(to bottom,royalblue,lightblue,skyblue);
        }
        .star{
            width: 60px;
            height: 60px;
            position: absolute;
            left: -120px;
            top: 0;
            border-radius: 50%;
            background: radial-gradient(darkred,red);
        }
    </style>
    <script src="../js/jquery.1.11.3.min.js"></script>
</head>
<body>
<button>停止运动</button>
<div class="earth">
    <div class="star"></div>
</div>
<script>
    
    var r = 200; // 半径
    var angle = 0; // 角度

    // 小球运动函数
    function move(){
        // 移动点的相对于原点的X轴的位置
        var x = r*Math.cos(angle*Math.PI/180) + 95;
        // 移动点的相对于原点的Y轴的位置
        var y = r*Math.sin(angle*Math.PI/180) + 95;

        $('.star').css({
            left: x + 'px',
            top: y + 'px'
        })
        angle++;
    }

    var timer = setInterval("move()",10);

    $('button').click(function() {
        if($(this).hasClass('has')){
            timer = setInterval("move()",10); //开启定时器

            $(this).removeClass('has');
            $(this).html("停止运动");

        }else{
            clearInterval(timer); //清除定时器

            $(this).addClass('has');
            $(this).html("开始运动")
        }
    });
  </script>
  </body>
</html>

其中用到的sin cos算法,看下图(一看就懂 )
因为博主数学渣渣,所以写下来辣 哈哈哈哈~~


算法原理.png

示例代码git地址: https://gitee.com/ClaraPing_admin/front_end_functions.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作 者:韩 昊 **知 乎:Heinrich微 博:@花生油工人 **知...
    zhwhong阅读 9,214评论 11 76
  • 要让读者在不看任何数学公式的情况下理解傅里叶分析。 傅里叶分析不仅仅是一个数学工具,更是一种可以彻底颠覆一个人以前...
    yalesaleng阅读 1,367评论 0 0
  • 今年的七月越发的炎热,一切都如往常一般。可我的心里不知怎的就落了些尘埃,怎么也拂不去........ 今近几天气...
    灼日灬阅读 311评论 0 0
  • 离开武汉时叫了车,边打字边应景地掉了两滴眼泪,到武汉站时刚刚写完,拉着行李站在车站前读两遍给他发了去: “其实这几...
    一只齐小落阅读 212评论 0 0
  • 看了《薛兆丰北大经济学课》,主要讲了关于公平和效率之间的关系。 通过马粪事件的举例,来分析是鼓励人们创造财富还是对...
    53fe4e8bddb6阅读 1,179评论 0 0