美文网首页WebGL
canvas绘制动态进度条

canvas绘制动态进度条

作者: 狂暴机甲 | 来源:发表于2018-09-12 23:49 被阅读5次
    image.png
    <body>
        <canvas id="mycanvas" width="400" height="400" style="background-color:rgb(214, 214, 214)"></canvas>
        <script>
            var canvas = document.getElementById('mycanvas');
            var width = canvas.width, height = canvas.height;
            var ctx = canvas.getContext('2d');
    
            function Drawrect(x, y, w, h) {
                this.x = x;
                this.y = y;
                this.w = 10;
                this.wconst = w;
                this.h = h;
                this.flag = true;
    
                this.run = function () {
                    if (this.flag) {
                        this.w++;
                        if (this.w >= this.wconst) {
                            this.flag = false;
                        }
                        ctx.fillStyle = "#F00";
                        ctx.fillRect(this.x, this.y, this.w, this.h);
                    }
                    if (!this.flag) {
                        this.w--;
                        if (this.w <= 1) {
                            this.flag = true;
                        }
                        ctx.clearRect(this.x, this.y, this.wconst, this.h);
                        ctx.fillStyle = "#F00";
                        ctx.fillRect(this.x, this.y, this.w, this.h);
                    }
                }
            }
    
            var d = new Drawrect(10, 10, 100, 10);
            var d2 = new Drawrect(10, 30, 150, 10); 
            var d3 = new Drawrect(10, 50, 200, 10);  
            var d4 = new Drawrect(10, 70, 250, 10);  
            var d5 = new Drawrect(10, 90, 300, 10);         
            run();
            function run() {
                //var t = setTimeout(run, 1);
                requestAnimationFrame(run);
                d.run();
                d2.run();
                d3.run();
                d4.run();
                d5.run();
            }
        </script>
    </body>
    

    相关文章

      网友评论

        本文标题:canvas绘制动态进度条

        本文链接:https://www.haomeiwen.com/subject/gxjigftx.html