zoukankan      html  css  js  c++  java
  • HTML5 Cavans(11) 简单动画:圆周运动

    <!DOCTYPE html >
    <html>
    <head>
        <title></title>
        <script type="text/javascript">
            window.onload = function () {
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                var btnStart = document.getElementById("btnStart");
                var btnStop = document.getElementById("btnStop");
    
                var isPlay = true;
                btnStart.style.display = "none";
    
                btnStart.onclick = function () {
                    isPlay = true;
                    this.style.display = "none";
                    btnStop.style.display = "";
                    animate();
                }
                btnStop.onclick = function () {
                    isPlay = false;
                    this.style.display = "none";
                    btnStart.style.display = "";
                }
    
                //形状类,构造函数传入起始坐标,半径,起始角度
                function Shape(x, y, radius, angle) {
                    this.x = x;
                    this.y = y;
                    this.radius = radius;
                    if (angle == undefined) {
                        this.angle = 0;
                    }
                    else {
                        this.angle = angle;
                    }
                }
    
                var shapes = [];
                //随即产生形状
                for (var i = 0; i < 10; i++) {
                    var x = Math.random() * 250;
                    var y = Math.random() * 250;
                    var radius = Math.random() * 50;
                    shapes.push(new Shape(x, y, 30));
                }
    
                function animate() {
                    context.clearRect(0, 0, canvas.width, canvas.height);
                    var len = shapes.length;
                    for (var i = 0; i < len; i++) {
                        var tmpShape = shapes[i];
    
                        var angle = tmpShape.angle + Math.PI / 100;
                        if (angle > Math.PI * 2)
                            angle = 0;
                        tmpShape.angle = angle;
                        //半径是直角三角形斜边,根据角度计算x和y的偏移
                        var x = tmpShape.radius * Math.cos(angle);
                        var y = tmpShape.radius * Math.sin(angle);
                        //画出半径轨迹
                        context.beginPath();
                        context.moveTo(tmpShape.x, tmpShape.y);
                        context.lineTo(tmpShape.x + x, tmpShape.y + y);
                        context.stroke();
                        context.fillRect(tmpShape.x + x-5, tmpShape.y + y-5, 10, 10);
    
                    }
    
                    if (isPlay)
                        setTimeout(animate, 33);
                }
                animate();
            }
           
        </script>
    </head>
    <body>
        <canvas id="myCanvas" height="500px" width="500px" style="border:1px black solid">
        </canvas>
        <input id="btnStart" type="button" value="start" >
        <input id="btnStop" type="button" value="stop" >
    </body>
    </html>
  • 相关阅读:
    JS鼠标滚动事件
    [Harbor]Harbor简要介绍
    [Kubernetes]yaml文件详解
    [Kubernetes]安装和配置kubectl
    [Docker]如何批量删除镜像
    [Kubernetes]如何使用yaml文件使得可以向外暴露服务
    [Jenkins]CentOS7下Jenkins搭建
    [Docker]Docker拉取,上传镜像到Harbor仓库
    [Harbor]Docker登录Harbor仓库(HTTP方式)
    [Docker]CentOS7通过rpm包安装Docker
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2586120.html
Copyright © 2011-2022 走看看