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>
  • 相关阅读:
    monkeyrunner1
    也来复习一下数据库的一些知识1
    Monkey原理
    总结一下app客户端的测试点
    从侧计--mongkeyScript问题
    从侧计----monkeyScript实例----开启墨迹天气并添加城市,最后关闭app
    求助:关于sql如何统计时间的问题
    虚拟机无法分配内存 virtual memory exhausted: Cannot allocate memory
    Ubuntu14.04安装libusb
    E: 软件包 ffmpeg 没有可供安装的候选者
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2586120.html
Copyright © 2011-2022 走看看