• 随笔


    <canvas id="canvas"></canvas>
    <script>
        var WIDTH = 400,
            HEIGHT = 400,
            POINT = 40,
            canvas = document.getElementById('canvas'),
            ctx = canvas.getContext('2d');
        var circleArr = [];
        canvas.width = WIDTH;
        canvas.height = HEIGHT;
        
        ctx.strokeStyle = 'rgba(0,0,0,1)';
        ctx.strokeWidth = 1;
        ctx.fillStyle = 'rgba(0,0,180,0.2)';
        function Line (x, y, _x, _y, o) {
            this.x = x;
            this.y = y;
            this._x = _x;
            this._y = _y;
            this.o = o;
        }
        function Circle(x, y, r, mx, my) {
            this.x = x;
            this.y = y;
            this.r = r;
            this.mx = mx;
            this.my = my;
        }
        var Num = (max, min) => {
            min |= 0;
            return Math.floor(Math.random()*(max - min + 1) + min);
        }
        var drawLine = (x, y, _x, _y, o) => {
            var line = new Line(x, y, _x, _y, o);
            ctx.beginPath();
            ctx.strokeStyle = `rgba(0, 0, 0, ${line.o})`;
            ctx.moveTo(line.x, line.y);
            ctx.lineTo(line._x, line._y);
            ctx.closePath();
            ctx.stroke();
        }
        var drawCircle = (x, y, r, mx, my) => {
            var circle = new Circle(x, y, r, mx, my);
            ctx.beginPath();
            ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI*2);
            ctx.closePath();
            ctx.fill();
            return circle;
        }
        var init = () => {
            circleArr = [];
            for (var i = 0; i < POINT; i++) {
                circleArr.push(drawCircle(Num(WIDTH), Num(HEIGHT), Num(10, 1), Num(10, -10)/30, Num(10, -10)/40));
            }
            draw()
        }
        var draw = () => {
            ctx.clearRect(0, 0, WIDTH, HEIGHT);
            for (var i = 0; i < POINT; i++) {
                drawCircle(circleArr[i].x, circleArr[i].y, circleArr[i].r);
            }
            for (var i = 0; i < POINT; i++) {
                for (var j = 0; j < POINT; j++) {
                    if (i + j < POINT) {
                        var A = Math.abs(circleArr[i+j].x - circleArr[i].x),
                            B = Math.abs(circleArr[i+j].y - circleArr[i].y);
                        var lineLength = Math.sqrt(A*A + B*B);
    
                        var C = 1/lineLength*7-0.009;
                        var lineOpacity = C > 0.03 ? 0.2 : C;
                        if (lineOpacity > 0) {
                            drawLine(circleArr[i].x, circleArr[i].y, circleArr[i+j].x, circleArr[i+j].y, lineOpacity);
                        }
                    }
                }
            }
        }
    
        init();
        setInterval(function () {
            for (var i = 0; i < POINT; i++) {
                var cir = circleArr[i];
                cir.x += cir.mx;
                cir.y += cir.my;
                if (cir.x > WIDTH)  cir.mx = -cir.mx;
                else if (cir.x < 0) cir.mx = -cir.mx;
                if (cir.y > HEIGHT) cir.my = -cir.my;
                else if (cir.y < 0) cir.my = -cir.my;
            }
            draw();
        }, 100);
    </script>
    
  • 相关阅读:
    [转载]实战经验:IIS网站服务器性能优化攻略
    如何检测本页中的iframe是否“加载”完成
    悟透JavaScript读书笔记闭包与原型
    HttpConnection访问时ArrayIndexOutofBoundException的解释[javaME]
    [JavaME]手机同时播放两个音乐 探讨一
    封装MIDP 1.0 HttpConnection用于商业应用[javaME]
    Nokia S60真机的全屏getHeight()返回值BUG说明
    [JavaME]在高级UI上的keyPressed事件截获的说明
    手机同时播放两个音乐 探讨二[JavaME]
    Bloglines手机伴侣开发纪事[1][j2me]
  • 原文地址:https://www.cnblogs.com/luozhangshuai/p/7685466.html
Copyright © 2020-2023  润新知