zoukankan      html  css  js  c++  java
  • canvas背景效果

    canvas背景效果:

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    
    <body>
    <canvas id="canvas" width="800" height="800"></canvas>
    </body>
    <script>
        let canvas = document.getElementById('canvas');
        let ctx = canvas.getContext('2d');
        let w = canvas.width = canvas.offsetWidth;
        let h = canvas.height = canvas.offsetHeight;
        let circles = [];
    
        function rand(min, max) {
            return parseInt(Math.random() * (max - min + 1) + min);
        }
    
        class Circle {
            constructor() {
                this.r = rand(5, 15);
                let x = rand(0, canvas.width - this.r);
                let y = rand(0, canvas.height - this.r);
                this.x = x < this.r ? this.r : x;
                this.y = y < this.r ? this.r : y;
    
                let speed = rand(1, 3);
                this.speedX = rand(0, 4) > 2 ? speed : -speed;
                this.speedY = rand(0, 4) > 2 ? speed : -speed;
            }
    
            drawCircle(ctx) {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.r, 0, 360);
                ctx.closePath();
                ctx.fillStyle = 'rgba(204,204,204,0.3)';
                ctx.fill();
            }
    
            drawLine(ctx, _circle) {
                let dx = this.x - _circle.x;
                let dy = this.y - _circle.y;
                let d = Math.sqrt(dx * dx + dy * dy);
                if (d < 150) {
                    ctx.beginPath();
                    ctx.moveTo(this.x, this.y);
                    ctx.lineTo(_circle.x, _circle.y);
                    ctx.closePath();
                    ctx.strokeStyle = 'rgba(204,204,204,0.3)';
                    ctx.stroke();
                }
            }
    
            move(w, h) {
                this.x += this.speedX / 10;
                if (this.x > w || this.x < 0) {
                    this.speedX *= -1;
                }
    
                this.y += this.speedY / 10;
                if (this.y > h || this.y < 0) {
                    this.speedY *= -1;
                }
            }
        }
    
        class curCircle extends Circle {
            constructor() {
                super();
            }
    
            drawCircle(ctx) {
                ctx.beginPath();
                this.r = 5;
                ctx.arc(this.x, this.y, this.r, 0, 360);
                ctx.closePath();
                ctx.fillStyle = 'rgba(255,77,54,0.6)';
                ctx.fill();
            }
        }
    
        let circle = new curCircle();
        let draw = function () {
            ctx.clearRect(0, 0, w, h);
            for (let i = 0; i < circles.length; i++) {
                circles[i].drawCircle(ctx);
                for (j = i + 1; j < circles.length; j++) {
                    circles[i].drawLine(ctx, circles[j]);
                }
                circles[i].move(w, h);
            }
            if (circle.x) {
                circle.drawCircle(ctx);
                for (let k = 1; k < circles.length; k++) {
                    circle.drawLine(ctx, circles[k])
                }
            }
            requestAnimationFrame(draw);
        };
    
        let init = function (num) {
            for (let i = 0; i < num; i++) {
                circles.push(new Circle());
            }
            draw();
        };
    
        window.addEventListener('load', init(20));
    
        canvas.addEventListener('mousemove', function (e) {
            e = e || window.event;
            circle.x = e.offsetX;
            circle.y = e.offsetY;
        });
    
        canvas.addEventListener('mouseout', function (e) {
            circle.x = null;
            circle.y = null;
        });
    </script>
    
    </html>
  • 相关阅读:
    Echarts markPoint 动态数据添加,选择性查询
    echarts timeline点击以后 蓝色的checkpoint位置不跟当前点击的节点重合
    Echarts 动态添加到map显示
    tomcat 下不在tomcat发布项目,引用外部链接
    SQL Server 2008安装
    eclipce 安装 svn插件(百度知道)
    迅为IMX6ULL开发板Linux蜂鸣器实验
    4412开发板-Android4.4典型功能相关源码修改及定制
    IMX6ULL开发平台Linux-LED实验
    迅为iTOP4418开发板运行Android7.1/Qt5.7/Ubuntu12.04系统源码开源
  • 原文地址:https://www.cnblogs.com/cxzhijia/p/7308844.html
Copyright © 2011-2022 走看看