美文网首页
封装一个模仿知乎登录背景的面向对象代码

封装一个模仿知乎登录背景的面向对象代码

作者: 忽如寄 | 来源:发表于2016-08-22 23:12 被阅读843次

我已经将其封装,就不写教程了,使用如下:
参数:

options={
    canvasContainerID:运用背景的盒子的ID名,默认为“canvas-container”,
    canvasOpacity:canvas背景的透明度,默认为0.8,
    circleNum:页面中随机产生的圆点的数量,默认为40,
    circleColor:页面的颜色,可接受rgb、rgba、十六进制、关键字形式,默认为rgba(180,180,180,1),
    lineColor:线条的颜色,只接受rgba形式,默认为rgba(180,180,180,1),
    circleMovemaxX:圆点每次X轴移动最大的距离,整数为向右移动,负数为向左移动,默认为2,
    circleMoveminX:圆点每次X轴移动最小的距离,整数为向右移动,负数为向左移动,默认为-2,
    circleMovemaxY:圆点每次Y轴移动最大的距离,整数为向下移动,负数为向上移动,默认为2,
    circleMoveminY:圆点每次Y轴移动最小的距离,整数为向下移动,负数为向上移动,默认为-2,
    canvasWidth:canvas背景的宽度,默认为整个窗口可视区宽度,
    canvasHeight:canvas背景的高度,默认为整个窗口可视区高度,
}

使用方法:直接实例化,将参数提供即可,不提供参数将使用默认参数,如 :

var bg=new LogBackground({
      canvasContainerID:"mycanvas"
})

原生js版源代码如下:

function LogBackground(opt){
        this.init(opt);
    }
    LogBackground.prototype.init=function(opt){
        var self=this;
        self._initDom(opt);
        self.canvas.width=this.options.canvasWidth;
        self.canvas.height=this.options.canvasHeight;
        self._initCir(self.context);
        self.render();
    }
    LogBackground.prototype._initDom=function(opt){
        this.options={
            canvasContainerID:"canvas-container",
            canvasOpacity:0.8,
            circleNum:40,
            circleColor:"rgba(180,180,180,1)",
            lineColor:"rgba(180,180,180,1)",
            circleMovemaxX:2,
            circleMoveminX:-2,
            circleMovemaxY:2,
            circleMoveminY:-2,
            canvasWidth:document.documentElement.clientWidth||document.body.Width,
            canvasHeight:document.documentElement.clientHeight||document.body.client
        }
        if(opt){
            for(var key in opt){
                this.options[key]=opt[key];
            }
        }
        var canvasEle=document.createElement("canvas");
        var canvasContainer=document.getElementById(this.options.canvasContainerID);
        canvasContainer.appendChild(canvasEle);
        canvasContainer.style.position="relative";
        canvasEle.style.cssText="z-index:-20;position:absolute;left:0;top:0";
        canvasEle.style.opacity=this.options.canvasOpacity;
        this.canvas=canvasEle;
        this.drawMaxWidth=this.options.canvasWidth+30;
        this.drawMinWidth=-30;
        this.drawMaxHeight=this.options.canvasHeight+30;
        this.drawMinHeight=-30;
        this.context=this.canvas.getContext("2d");
        this.circleArr=[];
        this.moveArr=[];
    }
    LogBackground.prototype.random=function(max,_min){
        var minNum=arguments[1]||0;
        return Math.floor(Math.random()*(max-minNum+1)+minNum);
    }
    LogBackground.prototype._initCir=function(context){
        var self=this;
        for(var i=0;i<self.options.circleNum;i++){
            x=self.random(self.drawMaxWidth,self.drawMinWidth);
            y=self.random(self.drawMaxHeight,self.drawMinHeight);
            r=self.random(10);
            var newCircle=self.drawCircle(context,x,y,r);
            self.circleArr.push(newCircle);
            var move={
                x:Math.random()*(self.options.circleMovemaxX-self.options.circleMoveminX)+self.options.circleMoveminX,
                y:Math.random()*(self.options.circleMovemaxY-self.options.circleMoveminY)+self.options.circleMoveminY
            }
            self.moveArr.push(move);
        }
    }
    LogBackground.prototype._initLine=function(ctx,bx,by,cx,cy,opacity){
        var self=this;
        function Line(bx,by,cx,cy){
            this.beginX=bx;
            this.beginY=by;
            this.closeX=cx;
            this.closeY=cy;
        }
        var line=new Line(bx,by,cx,cy);
            ctx.beginPath();
            ctx.moveTo(line.beginX,line.beginY);
            ctx.lineTo(line.closeX,line.closeY);
            ctx.stroke();
            var colorArr=self.options.lineColor.split(",");
            ctx.strokeStyle=colorArr[0]+","+colorArr[1]+","+colorArr[2]+","+opacity+")";
            ctx.closePath();

    }
    LogBackground.prototype.render=function(){
        var self=this;
        self.context.clearRect(0,0,self.options.canvasWidth,self.options.canvasHeight);
            for(var i=0;i<40;i++){
                var changeCircle=self.circleArr[i];
                changeCircle.centerX+=self.moveArr[i].x;
                changeCircle.centerY+=self.moveArr[i].y;
                self.drawCircle(self.context,changeCircle.centerX,changeCircle.centerY,changeCircle.radius);
                if(changeCircle.centerX<self.drawMinWidth){
                    changeCircle.centerX=self.drawMaxWidth;
                    changeCircle.centerY=self.random(self.drawMaxHeight,self.drawMinHeight);
                }else if(changeCircle.centerX>self.drawMaxWidth){
                    changeCircle.centerX=self.drawMinWidth;
                    changeCircle.centerY=self.random(self.drawMaxHeight,self.drawMinHeight);
                }else if(changeCircle.centerY<self.drawMinHeight){
                    changeCircle.centerY=self.random(self.drawMaxWidth,self.drawMinWidth);
                    changeCircle.centerY=self.drawMaxHeight;
                }else if(changeCircle.centerY>self.drawMaxHeight){
                    changeCircle.centerY=self.random(self.drawMaxWidth,self.drawMinWidth);
                    changeCircle.centerY=self.drawMinWidth;
                }
                
            }
            for(var j=0;j<40;j++){
                for(var k=0;k<40;k++){
                    var bx=self.circleArr[j].centerX;
                    var by=self.circleArr[j].centerY;
                    var cx=self.circleArr[k].centerX;
                    var cy=self.circleArr[k].centerY;
                    var dis=Math.sqrt(Math.abs(cx-bx)*Math.abs(cx-bx)+Math.abs(by-cy)*Math.abs(by-cy));
                    if(dis<0.3*this.options.canvasWidth||dis>this.options.canvasWidth*1.2){
                    var lineOpacity=1;
                    lineOpacity=lineOpacity>0.3?0.3:lineOpacity;
                    self._initLine(self.context,bx,by,cx,cy,lineOpacity);
                    }
                }
            }
        var timer=setTimeout(function(){
            self.render();
        },30);
    }
    LogBackground.prototype.drawCircle=function(ctx,x,y,r){
        var self=this;
        function Circle(x,y,r){
            this.centerX=x;
            this.centerY=y;
            this.radius=r;
        }
        var circle=new Circle(x,y,r);
        ctx.beginPath();
        ctx.arc(circle.centerX,circle.centerY,circle.radius,0,Math.PI*2,true);
        ctx.fillStyle=self.options.circleColor;
        ctx.fill();
        ctx.closePath();
        return circle;
    }

jquery版源代码如下:

function LogBackground(opt){
        this.init(opt);
    }
    $.extend(LogBackground.prototype,{
        init:function(opt){
            var self=this;
            self.options={
                canvasContainerID:"#canvas-container",
                canvasOpacity:0.8,
                circleNum:40,
                circleColor:"rgba(180,180,180,1)",
                lineColor:"rgba(180,180,180,1)",
                circleMovemaxX:2,
                circleMoveminX:-2,
                circleMovemaxY:2,
                circleMoveminY:-2,
                canvasWidth:$(window).width(),
                canvasHeight:$(window).height()
            }
            $.extend(true,self.options,opt||{});
            self._initDom();
            self.canvas.width=this.options.canvasWidth;
            self.canvas.height=this.options.canvasHeight;
            self._initCir(self.context);
            self.render();
        },
        _initDom:function(){
            var self=this;
            var $canvas=$("<canvas></canvas>")
            $(self.options.canvasContainerID).append($canvas);
            $canvas.css({"z-index":"-20","position":"absolute","left":0,"top":0,"opacity":self.options.canvasOpacity});
            this.canvas=$canvas[0];
            this.drawMaxWidth=this.options.canvasWidth+30;
            this.drawMinWidth=-30;
            this.drawMaxHeight=this.options.canvasHeight+30;
            this.drawMinHeight=-30;
            this.context=this.canvas.getContext("2d");
            this.circleArr=[];
            this.moveArr=[];
        },
        random:function(max,_min){
            var minNum=arguments[1]||0;
            return Math.floor(Math.random()*(max-minNum+1)+minNum);
        },
        _initCir:function(context){
            var self=this;
            for(var i=0;i<self.options.circleNum;i++){
                x=self.random(self.drawMaxWidth,self.drawMinWidth);
                y=self.random(self.drawMaxHeight,self.drawMinHeight);
                r=self.random(10);
                var newCircle=self.drawCircle(context,x,y,r);
                self.circleArr.push(newCircle);
                var move={
                    x:Math.random()*(self.options.circleMovemaxX-self.options.circleMoveminX)+self.options.circleMoveminX,
                    y:Math.random()*(self.options.circleMovemaxY-self.options.circleMoveminY)+self.options.circleMoveminY
                }
                self.moveArr.push(move);
            }
        },
        _initLine:function(ctx,bx,by,cx,cy,opacity){
            var self=this;
            function Line(bx,by,cx,cy){
                this.beginX=bx;
                this.beginY=by;
                this.closeX=cx;
                this.closeY=cy;
            }
            var line=new Line(bx,by,cx,cy);
            ctx.beginPath();
            ctx.moveTo(line.beginX,line.beginY);
            ctx.lineTo(line.closeX,line.closeY);
            ctx.stroke();
            var colorArr=self.options.lineColor.split(",");
            ctx.strokeStyle=colorArr[0]+","+colorArr[1]+","+colorArr[2]+","+opacity+")";
            ctx.closePath();
        },
        render:function(){
            var self=this;
            self.context.clearRect(0,0,self.options.canvasWidth,self.options.canvasHeight);
            for(var i=0;i<40;i++){
                var changeCircle=self.circleArr[i];
                changeCircle.centerX+=self.moveArr[i].x;
                changeCircle.centerY+=self.moveArr[i].y;
                self.drawCircle(self.context,changeCircle.centerX,changeCircle.centerY,changeCircle.radius);
                if(changeCircle.centerX<self.drawMinWidth){
                    changeCircle.centerX=self.drawMaxWidth;
                    changeCircle.centerY=self.random(self.drawMaxHeight,self.drawMinHeight);
                }else if(changeCircle.centerX>self.drawMaxWidth){
                    changeCircle.centerX=self.drawMinWidth;
                    changeCircle.centerY=self.random(self.drawMaxHeight,self.drawMinHeight);
                }else if(changeCircle.centerY<self.drawMinHeight){
                    changeCircle.centerY=self.random(self.drawMaxWidth,self.drawMinWidth);
                    changeCircle.centerY=self.drawMaxHeight;
                }else if(changeCircle.centerY>self.drawMaxHeight){
                    changeCircle.centerY=self.random(self.drawMaxWidth,self.drawMinWidth);
                    changeCircle.centerY=self.drawMinWidth;
                }
                
            }
            for(var j=0;j<40;j++){
                for(var k=0;k<40;k++){
                    var bx=self.circleArr[j].centerX;
                    var by=self.circleArr[j].centerY;
                    var cx=self.circleArr[k].centerX;
                    var cy=self.circleArr[k].centerY;
                    var dis=Math.sqrt(Math.abs(cx-bx)*Math.abs(cx-bx)+Math.abs(by-cy)*Math.abs(by-cy));
                    if(dis<0.3*this.options.canvasWidth||dis>this.options.canvasWidth*1.2){
                    var lineOpacity=1;
                    lineOpacity=lineOpacity>0.3?0.3:lineOpacity;
                    self._initLine(self.context,bx,by,cx,cy,lineOpacity);
                    }
                }
            }
            var timer=setTimeout(function(){
                self.render();
            },30);
        },
        drawCircle:function(ctx,x,y,r){
            var self=this;
            function Circle(x,y,r){
                this.centerX=x;
                this.centerY=y;
                this.radius=r;
            }
            var circle=new Circle(x,y,r);
            ctx.beginPath();
            ctx.arc(circle.centerX,circle.centerY,circle.radius,0,Math.PI*2,true);
            ctx.fillStyle=self.options.circleColor;
            ctx.fill();
            ctx.closePath();
            return circle;
        }
    });

hope登录页使用默认参数效果如下:


传入部分参数如下:

var bg=new LogBackground({
    circleColor:"rgba(49,210,142,0.8)",
    lineColor:"rgba(49,210,142,1)",
    canvasOpacity:0.2
    });

效果如下:

相关文章

  • 封装一个模仿知乎登录背景的面向对象代码

    我已经将其封装,就不写教程了,使用如下:参数: 使用方法:直接实例化,将参数提供即可,不提供参数将使用默认参数,如...

  • 理解面向对象

    理解面向对象 阅读知乎大牛对面向对象的讨论后,写下自己的总结和理解。知乎 面向对象的原本特性 面向对象原本的特性(...

  • javaScript-详细解答-面向对象-及-面试相关问题

    什么是面向对象? 一、 首先面向对象有三大特点: 什么是封装? 1. 封装:减少代码冗余,提高代码重复利用率 什么...

  • 2021-12-05面向对象与面向过程总结

    面向对象 面向对象编程(OOP) 面向对象编程:是一种编程范式,它以类和对象作为组织代码的单元,以封装、抽象、继承...

  • 设计模式:面向对象

    面向对象编程:以类或对象作为组织代码的基本单元,将封装、抽象、继承、多态,作为代码设计和实现的基石。面向对象编程语...

  • 面向对象的三大特性

    面向对象的三大特性:{ 封装、继承、多态 } 封装复用|信息隐蔽 代码示例 继承** 获取已经存在的对象...

  • 05.哪些代码设计看似是面向对象,实际是面向过程的?

    哪些代码看似是面向对象,实际上是面向过程的? 滥用getter、setter方法这种做法违反了面向对象编程的封装特...

  • python 面向对象封装案例

    面向对象封装案例 目标 封装 小明爱跑步 存放家具 01. 封装 封装 是面向对象编程的一大特点 面向对象编程的 ...

  • 14.Python对象封装

    面向对象封装案例 目标 封装 小明爱跑步 存放家具 01. 封装 封装 是面向对象编程的一大特点 面向对象编程的 ...

  • Java面试之基础篇

    面向对象的特性 面向对象的三大特性是:封装、继承、多态。 封装 封装就是将一个对象的属性和方法进行封装。同时私有化...

网友评论

      本文标题:封装一个模仿知乎登录背景的面向对象代码

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