• HTML5 Cavans(10) 简单动画:摆动


    <!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, width, height) {
                    this.x = x;
                    this.y = y;
                    this.width = width;
                    this.height = height;
                }
    
                var shapes = [];
                //随即产生形状
                for (var i = 0; i < 10; i++) {
                    var x = Math.random() * 250;
                    var y = Math.random() * 250;
                    var width = Math.random() * 50;
                    shapes.push(new Shape(x, y, width, width));
                }
    
                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];
                        //产生摆动效果
                        tmpShape.x += Math.random() * 4 - 2;
                        tmpShape.y += Math.random() * 4 - 2;
                        context.fillRect(tmpShape.x, tmpShape.y, tmpShape.width,tmpShape.height);
    
                    }
    
                    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>
  • 相关阅读:
    android 图片加载库 Glide 的使用介绍
    Android Glide数据更新及内存缓存、硬盘缓存清理
    Android中<meta-data>的使用
    分层,工厂模式,依赖注入控制反转
    Asp.Net_Web身份验证
    aspx后缀映射成html
    网站跳舞人代码
    Sqlerver_各类函数
    Sqlserver_时间用法
    Sqlserver_视图
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2586017.html
Copyright © 2020-2023  润新知