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>
  • 相关阅读:
    第2天 栈和寄存器
    第1天 工作计划和开端
    贯穿实例(1)
    闹心的变量
    开启懒人模式
    前言
    python基础学习7-网络编程、异常处理、面向对象
    python基础学习6-mongodb、sys、接口开发、操作excel
    python基础学习5-常用函数模块、操作数据库、发邮件、写日志、写excel
    python基础学习4-函数、内置函数、os模块、time模块
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2586120.html
Copyright © 2011-2022 走看看