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>
  • 相关阅读:
    异常处理的设计和重构学习一
    设计模式之禅之六大设计原则-里氏替换原则
    设计模式之禅之六大设计原则-单一职责原则
    swagger-ui生成api文档并进行测试
    功能强大的swagger-editor的介绍与使用
    swagger-codegen自动生成代码工具的介绍与使用
    Swagger使用教程大全,从入门到精通
    Linux下MySQL的数据文件存放位置
    JUC组件扩展(三):BlockingQueue(阻塞队列)详解
    http_load的安装及使用方法
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2586120.html
Copyright © 2011-2022 走看看