原生js实现绘制canvas博客背景,巨简单!(星际背景图)

372 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第15天,点击查看活动详情

前言:

掘友们,大家好!明天就是1024了,这里小编提前祝大家节日快乐,顺便在节前给大家奉上一个小功能。很快我们的项目取证系统马上迎来大更新,主要的就是针对前台,更换主题,更换页面。当我不经意间听到,前台有16个页面的时候,我就已经震惊了。不过还好,这种大场面谁没有见过呢,咱有那个能力,也做好了心里准备。来吧!come on!

博客背景图?

产品发来第一张图片的时候,我已经蒙了,这不是某某某博客的背景图吗?我心想,你怎么那么low,搬人家的东西。这时候,产品问我能实现么?我天,简简单单,给我五分钟。

上代码

CSS

这里小编只是创建了HTML给大家做演示,并不是在项目中使用,大家根据自己的项目,调节样式,文件类型和属性。

考虑做背景图,首先最重要的就是去除,html的默认属性margin和padding属性,让标签铺满整个页面。

* {
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
    overflow: hidden;
    background-color: #1B1D1F;
}

HTML

html部分小编只设置了canvas标签。

<canvas></canvas>

JS

JS是绘制canvas的重点,和操作canvas上元素内容的方法。

var canvas = document.querySelector('canvas'),  // 获取canvas标签
            ctx = canvas.getContext('2d')
        canvas.width = window.innerWidth;  // 设置canvas宽高
        canvas.height = window.innerHeight;
        ctx.lineWidth = .3;
        ctx.strokeStyle = (new Color(150)).style;

        var mousePosition = {
            x: 30 * canvas.width / 100,
            y: 30 * canvas.height / 100
        };

        var dots = {
            nb: 150,
            distance: 50,
            d_radius: 100,
            array: []
        };

        function colorValue(min) {   // 设置随机色
            return Math.floor(Math.random() * 255 + min);
        }

        function createColorStyle(r, g, b) {
            return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
        }

        function mixComponents(comp1, weight1, comp2, weight2) {
            return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
        }

        function averageColorStyles(dot1, dot2) {
            var color1 = dot1.color,
                color2 = dot2.color;

            var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
                g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
                b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
            return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
        }

        function Color(min) {
            min = min || 0;
            this.r = colorValue(min);
            this.g = colorValue(min);
            this.b = colorValue(min);
            this.style = createColorStyle(this.r, this.g, this.b);
        }

        function Dot() {
            this.x = Math.random() * canvas.width;
            this.y = Math.random() * canvas.height;

            this.vx = -.5 + Math.random();
            this.vy = -.5 + Math.random();

            this.radius = Math.random() * 2;

            this.color = new Color();
            //console.log(this);
        }

        Dot.prototype = {
            draw: function () {
                ctx.beginPath();
                ctx.fillStyle = this.color.style;
                ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
                ctx.fill();
            }
        };

        function createDots() {
            for (i = 0; i < dots.nb; i++) {
                dots.array.push(new Dot());
            }
        }

        function moveDots() {
            for (i = 0; i < dots.nb; i++) {

                var dot = dots.array[i];

                if (dot.y < 0 || dot.y > canvas.height) {
                    dot.vx = dot.vx;
                    dot.vy = - dot.vy;
                }
                else if (dot.x < 0 || dot.x > canvas.width) {
                    dot.vx = - dot.vx;
                    dot.vy = dot.vy;
                }
                dot.x += dot.vx;
                dot.y += dot.vy;
            }
        }

        function connectDots() {
            for (i = 0; i < dots.nb; i++) {
                for (j = 0; j < dots.nb; j++) {
                    i_dot = dots.array[i];
                    j_dot = dots.array[j];

                    if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance) {
                        if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius) {
                            ctx.beginPath();
                            ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
                            ctx.moveTo(i_dot.x, i_dot.y);
                            ctx.lineTo(j_dot.x, j_dot.y);
                            ctx.stroke();
                            ctx.closePath();
                        }
                    }
                }
            }
        }

        function drawDots() {
            for (i = 0; i < dots.nb; i++) {
                var dot = dots.array[i];
                dot.draw();
            }
        }

        function animateDots() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            moveDots();
            connectDots();
            drawDots();

            requestAnimationFrame(animateDots);
        }

        createDots();
        requestAnimationFrame(animateDots);	

总结:

canvas还有许多内置的方法,可以去探讨,增加鼠标触及,离开的操作等。使用场景也非常的多,比如说,许许多多的网站登录页面,都需需要验证码登录,验证码就是使用canvas绘制,而成的。最后,祝大家啊明天玩的开心,工作顺利。