原始js

<script type="text/javascript" size="150" alpha='0.3' zIndex="-2">
(function (name, factory)
{
    if (typeof window === "object")
    {
        window[name] = factory();
    }

})("Ribbons", function ()
{
    var _w = window,
    _b = document.body,
    _d = document.documentElement;

    // random helper
    var random = function ()
    {
        if (arguments.length === 1) // only 1 argument
            {
                if (Array.isArray(arguments[0])) // extract index from array
                    {
                        var index = Math.round(random(0, arguments[0].length - 1));
                        return arguments[0][index];
                    }
                return random(0, arguments[0]); // assume numeric
            } else
        if (arguments.length === 2) // two arguments range
            {
                return Math.random() * (arguments[1] - arguments[0]) + arguments[0];
            }
        return 0; // default
    };

    // screen helper
    var screenInfo = function (e)
    {
        var width = Math.max(0, _w.innerWidth || _d.clientWidth || _b.clientWidth || 0),
        height = Math.max(0, _w.innerHeight || _d.clientHeight || _b.clientHeight || 0),
        scrollx = Math.max(0, _w.pageXOffset || _d.scrollLeft || _b.scrollLeft || 0) - (_d.clientLeft || 0),
        scrolly = Math.max(0, _w.pageYOffset || _d.scrollTop || _b.scrollTop || 0) - (_d.clientTop || 0);

        return {
            width: width,
            height: height,
            ratio: width / height,
            centerx: width / 2,
            centery: height / 2,
            scrollx: scrollx,
            scrolly: scrolly };

    };

    // mouse/input helper
    var mouseInfo = function (e)
    {
        var screen = screenInfo(e),
        mousex = e ? Math.max(0, e.pageX || e.clientX || 0) : 0,
        mousey = e ? Math.max(0, e.pageY || e.clientY || 0) : 0;

        return {
            mousex: mousex,
            mousey: mousey,
            centerx: mousex - screen.width / 2,
            centery: mousey - screen.height / 2 };

    };

    // point object
    var Point = function (x, y)
    {
        this.x = 0;
        this.y = 0;
        this.set(x, y);
    };
    Point.prototype = {
        constructor: Point,

        set: function (x, y)
        {
            this.x = x || 0;
            this.y = y || 0;
        },
        copy: function (point)
        {
            this.x = point.x || 0;
            this.y = point.y || 0;
            return this;
        },
        multiply: function (x, y)
        {
            this.x *= x || 1;
            this.y *= y || 1;
            return this;
        },
        divide: function (x, y)
        {
            this.x /= x || 1;
            this.y /= y || 1;
            return this;
        },
        add: function (x, y)
        {
            this.x += x || 0;
            this.y += y || 0;
            return this;
        },
        subtract: function (x, y)
        {
            this.x -= x || 0;
            this.y -= y || 0;
            return this;
        },
        clampX: function (min, max)
        {
            this.x = Math.max(min, Math.min(this.x, max));
            return this;
        },
        clampY: function (min, max)
        {
            this.y = Math.max(min, Math.min(this.y, max));
            return this;
        },
        flipX: function ()
        {
            this.x *= -1;
            return this;
        },
        flipY: function ()
        {
            this.y *= -1;
            return this;
        } };


    // class constructor
    var Factory = function (options)
    {
        this._canvas = null;
        this._context = null;
        this._sto = null;
        this._width = 0;
        this._height = 0;
        this._scroll = 0;
        this._ribbons = [];
        this._options = {
            // ribbon color HSL saturation amount
            colorSaturation: "80%",
            // ribbon color HSL brightness amount
            colorBrightness: "60%",
            // ribbon color opacity amount
            colorAlpha: 0.65,
            // how fast to cycle through colors in the HSL color space
            colorCycleSpeed: 6,
            // where to start from on the Y axis on each side (top|min, middle|center, bottom|max, random)
            verticalPosition: "center",
            // how fast to get to the other side of the screen
            horizontalSpeed: 150,
            // how many ribbons to keep on screen at any given time
            ribbonCount: 3,
            // add stroke along with ribbon fill colors
            strokeSize: 0,
            // move ribbons vertically by a factor on page scroll
            parallaxAmount: -0.5,
            // add animation effect to each ribbon section over time
            animateSections: true };

        this._onDraw = this._onDraw.bind(this);
        this._onResize = this._onResize.bind(this);
        this._onScroll = this._onScroll.bind(this);
        this.setOptions(options);
        this.init();
    };

    // class prototype
    Factory.prototype = {
        constructor: Factory,

        // Set and merge local options
        setOptions: function (options)
        {
            if (typeof options === "object")
            {
                for (var key in options)
                {
                    if (options.hasOwnProperty(key))
                    {
                        this._options[key] = options[key];
                    }
                }
            }
        },

        // Initialize the ribbons effect
        init: function ()
        {
            try
            {
                this._canvas = document.createElement("canvas");
                this._canvas.style["display"] = "block";
                this._canvas.style["position"] = "fixed";
                this._canvas.style["margin"] = "0";
                this._canvas.style["padding"] = "0";
                this._canvas.style["border"] = "0";
                this._canvas.style["outline"] = "0";
                this._canvas.style["left"] = "0";
                this._canvas.style["top"] = "0";
                this._canvas.style["width"] = "100%";
                this._canvas.style["height"] = "100%";
                this._canvas.style["z-index"] = "-1";
                this._canvas.id = "bgCanvas";
                this._onResize();

                this._context = this._canvas.getContext("2d");
                this._context.clearRect(0, 0, this._width, this._height);
                this._context.globalAlpha = this._options.colorAlpha;

                window.addEventListener("resize", this._onResize);
                window.addEventListener("scroll", this._onScroll);
                document.body.appendChild(this._canvas);
            }
            catch (e) {
                console.warn("Canvas Context Error: " + e.toString());
                return;
            }
            this._onDraw();
        },

        // Create a new random ribbon and to the list
        addRibbon: function ()
        {
            // movement data
            var dir = Math.round(random(1, 9)) > 5 ? "right" : "left",
            stop = 1000,
            hide = 200,
            min = 0 - hide,
            max = this._width + hide,
            movex = 0,
            movey = 0,
            startx = dir === "right" ? min : max,
            starty = Math.round(random(0, this._height));

            // asjust starty based on options
            if (/^(top|min)$/i.test(this._options.verticalPosition))
            {
                starty = 0 + hide;
            } else
            if (/^(middle|center)$/i.test(this._options.verticalPosition))
            {
                starty = this._height / 2;
            } else
            if (/^(bottom|max)$/i.test(this._options.verticalPosition))
            {
                starty = this._height - hide;
            }

            // ribbon sections data
            var ribbon = [],
            point1 = new Point(startx, starty),
            point2 = new Point(startx, starty),
            point3 = null,
            color = Math.round(random(0, 360)),
            delay = 0;

            // buils ribbon sections
            while (true)
            {
                if (stop <= 0) break;stop--;

                movex = Math.round((Math.random() * 1 - 0.2) * this._options.horizontalSpeed);
                movey = Math.round((Math.random() * 1 - 0.5) * (this._height * 0.25));

                point3 = new Point();
                point3.copy(point2);

                if (dir === "right")
                {
                    point3.add(movex, movey);
                    if (point2.x >= max) break;
                } else
                if (dir === "left")
                {
                    point3.subtract(movex, movey);
                    if (point2.x <= min) break;
                }
                // point3.clampY( 0, this._height );

                ribbon.push({ // single ribbon section
                    point1: new Point(point1.x, point1.y),
                    point2: new Point(point2.x, point2.y),
                    point3: point3,
                    color: color,
                    delay: delay,
                    dir: dir,
                    alpha: 0,
                    phase: 0 });


                point1.copy(point2);
                point2.copy(point3);

                delay += 4;
                color += this._options.colorCycleSpeed;
            }
            this._ribbons.push(ribbon);
        },

        // Draw single section
        _drawRibbonSection: function (section)
        {
            if (section)
            {
                if (section.phase >= 1 && section.alpha <= 0)
                {
                    return true; // done
                }
                if (section.delay <= 0)
                {
                    section.phase += 0.02;
                    section.alpha = Math.sin(section.phase) * 1;
                    section.alpha = section.alpha <= 0 ? 0 : section.alpha;
                    section.alpha = section.alpha >= 1 ? 1 : section.alpha;

                    if (this._options.animateSections)
                    {
                        var mod = Math.sin(1 + section.phase * Math.PI / 2) * 0.1;

                        if (section.dir === "right")
                        {
                            section.point1.add(mod, 0);
                            section.point2.add(mod, 0);
                            section.point3.add(mod, 0);
                        } else {
                            section.point1.subtract(mod, 0);
                            section.point2.subtract(mod, 0);
                            section.point3.subtract(mod, 0);
                        }
                        section.point1.add(0, mod);
                        section.point2.add(0, mod);
                        section.point3.add(0, mod);
                    }
                } else
                {section.delay -= 0.5;}

                var s = this._options.colorSaturation,
                l = this._options.colorBrightness,
                c = "hsla(" + section.color + ", " + s + ", " + l + ", " + section.alpha + " )";

                this._context.save();

                if (this._options.parallaxAmount !== 0)
                {
                    this._context.translate(0, this._scroll * this._options.parallaxAmount);
                }
                this._context.beginPath();
                this._context.moveTo(section.point1.x, section.point1.y);
                this._context.lineTo(section.point2.x, section.point2.y);
                this._context.lineTo(section.point3.x, section.point3.y);
                this._context.fillStyle = c;
                this._context.fill();

                if (this._options.strokeSize > 0)
                {
                    this._context.lineWidth = this._options.strokeSize;
                    this._context.strokeStyle = c;
                    this._context.lineCap = "round";
                    this._context.stroke();
                }
                this._context.restore();
            }
            return false; // not done yet
        },

        // Draw ribbons
        _onDraw: function ()
        {
            // cleanup on ribbons list to rtemoved finished ribbons
            for (var i = 0, t = this._ribbons.length; i < t; ++i)
            {
                if (!this._ribbons[i])
                {
                    this._ribbons.splice(i, 1);
                }
            }

            // draw new ribbons 
            this._context.clearRect(0, 0, this._width, this._height);

            for (var a = 0; a < this._ribbons.length; ++a) // single ribbon
            {
                var ribbon = this._ribbons[a],
                numSections = ribbon.length,
                numDone = 0;

                for (var b = 0; b < numSections; ++b) // ribbon section
                {
                    if (this._drawRibbonSection(ribbon[b]))
                    {
                        numDone++; // section done
                    }
                }
                if (numDone >= numSections) // ribbon done
                    {
                        this._ribbons[a] = null;
                    }
            }
            // maintain optional number of ribbons on canvas
            if (this._ribbons.length < this._options.ribbonCount)
            {
                this.addRibbon();
            }
            requestAnimationFrame(this._onDraw);
        },

        // Update container size info
        _onResize: function (e)
        {
            var screen = screenInfo(e);
            this._width = screen.width;
            this._height = screen.height;

            if (this._canvas)
            {
                this._canvas.width = this._width;
                this._canvas.height = this._height;

                if (this._context)
                {
                    this._context.globalAlpha = this._options.colorAlpha;
                }
            }
        },

        // Update container size info
        _onScroll: function (e)
        {
            var screen = screenInfo(e);
            this._scroll = screen.scrolly;
        } };



    // export
    return Factory;
});
const config = {
animateSections: true,
colorAlpha: 0.5,
colorBrightness: "50%",
colorCycleSpeed: 5,
colorSaturation: "60%",
horizontalSpeed: 200,
parallaxAmount: -0.2,
ribbonCount: 3,
strokeSize: 0,
verticalPosition: "random",
};

new Ribbons();
</script>

预览:

预览

配置

  • size:功能区的大小,默认值:90。
  • alpha:行的不透明度(0~1),默认值:0.6。
  • zIndex:z空间的索引,默认值:-1。

例:

<script type="text/javascript" size="150" alpha='0.3' zIndex="-2">js内容</script>

js中参数设置1:

this._options = {
    // 色带HSL饱和度
    colorSaturation: "80%",
    // 色带HSL亮度量
    colorBrightness: "60%",
    // 带状颜色不透明度
    colorAlpha: 0.65,
    // 在HSL颜色空间中循环显示颜色的速度有多快
    colorCycleSpeed: 6,
    // 从哪一侧开始Y轴 (top|min, middle|center, bottom|max, random)
    verticalPosition: "center",
    // 到达屏幕另一侧的速度有多快
    horizontalSpeed: 200,
    // 在任何给定时间,屏幕上会保留多少条带
    ribbonCount: 3,
    // 添加笔划以及色带填充颜色
    strokeSize: 0,
    // 通过页面滚动上的因子垂直移动色带
    parallaxAmount: -0.5,
    // 随着时间的推移,为每个功能区添加动画效果
    animateSections: true
};

参数设置2:

this._canvas = document.createElement("canvas");
this._canvas.style["display"] = "block";
this._canvas.style["position"] = "fixed";
this._canvas.style["margin"] = "0";
this._canvas.style["padding"] = "0";
this._canvas.style["border"] = "0";
this._canvas.style["outline"] = "0";
this._canvas.style["left"] = "0";
this._canvas.style["top"] = "0";
this._canvas.style["width"] = "100%";
this._canvas.style["height"] = "100%";
this._canvas.style["z-index"] = "-1";
this._canvas.id = "bgCanvas";
this._canvas.style["background-color"]="#1f1f1f";
this._onResize();

在typecho中使用

配置好并且最小化的懒人js'

配置好并且最小化的懒人js

<script type="text/javascript" size="150" alpha='0.3' zIndex="-2">"object"==typeof window&&(window.Ribbons=function(){var t=window,i=document.body,n=document.documentElement,o=function(){if(1===arguments.length){if(Array.isArray(arguments[0])){var t=Math.round(o(0,arguments[0].length-1));return arguments[0][t]}return o(0,arguments[0])}return 2===arguments.length?Math.random()*(arguments[1]-arguments[0])+arguments[0]:0},s=function(o){var s=Math.max(0,t.innerWidth||n.clientWidth||i.clientWidth||0),h=Math.max(0,t.innerHeight||n.clientHeight||i.clientHeight||0),e=Math.max(0,t.pageXOffset||n.scrollLeft||i.scrollLeft||0)-(n.clientLeft||0),a=Math.max(0,t.pageYOffset||n.scrollTop||i.scrollTop||0)-(n.clientTop||0);return{width:s,height:h,ratio:s/h,centerx:s/2,centery:h/2,scrollx:e,scrolly:a}},h=function(t,i){this.x=0,this.y=0,this.set(t,i)};h.prototype={constructor:h,set:function(t,i){this.x=t||0,this.y=i||0},copy:function(t){return this.x=t.x||0,this.y=t.y||0,this},multiply:function(t,i){return this.x*=t||1,this.y*=i||1,this},divide:function(t,i){return this.x/=t||1,this.y/=i||1,this},add:function(t,i){return this.x+=t||0,this.y+=i||0,this},subtract:function(t,i){return this.x-=t||0,this.y-=i||0,this},clampX:function(t,i){return this.x=Math.max(t,Math.min(this.x,i)),this},clampY:function(t,i){return this.y=Math.max(t,Math.min(this.y,i)),this},flipX:function(){return this.x*=-1,this},flipY:function(){return this.y*=-1,this}};var e=function(t){this._canvas=null,this._context=null,this._sto=null,this._width=0,this._height=0,this._scroll=0,this._ribbons=[],this._options={colorSaturation:"80%",colorBrightness:"60%",colorAlpha:.65,colorCycleSpeed:6,verticalPosition:"center",horizontalSpeed:150,ribbonCount:3,strokeSize:0,parallaxAmount:-.5,animateSections:!0},this._onDraw=this._onDraw.bind(this),this._onResize=this._onResize.bind(this),this._onScroll=this._onScroll.bind(this),this.setOptions(t),this.init()};return e.prototype={constructor:e,setOptions:function(t){if("object"==typeof t)for(var i in t)t.hasOwnProperty(i)&&(this._options[i]=t[i])},init:function(){try{this._canvas=document.createElement("canvas"),this._canvas.style.display="block",this._canvas.style.position="fixed",this._canvas.style.margin="0",this._canvas.style.padding="0",this._canvas.style.border="0",this._canvas.style.outline="0",this._canvas.style.left="0",this._canvas.style.top="0",this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style["z-index"]="-1",this._canvas.id="bgCanvas",this._onResize(),this._context=this._canvas.getContext("2d"),this._context.clearRect(0,0,this._width,this._height),this._context.globalAlpha=this._options.colorAlpha,window.addEventListener("resize",this._onResize),window.addEventListener("scroll",this._onScroll),document.body.appendChild(this._canvas)}catch(t){return void console.warn("Canvas Context Error: "+t.toString())}this._onDraw()},addRibbon:function(){var t=Math.round(o(1,9))>5?"right":"left",i=1e3,n=this._width+200,s=0,e=0,a="right"===t?-200:n,r=Math.round(o(0,this._height));/^(top|min)$/i.test(this._options.verticalPosition)?r=200:/^(middle|center)$/i.test(this._options.verticalPosition)?r=this._height/2:/^(bottom|max)$/i.test(this._options.verticalPosition)&&(r=this._height-200);for(var l=[],c=new h(a,r),p=new h(a,r),_=null,d=Math.round(o(0,360)),u=0;!(i<=0);){if(i--,s=Math.round((1*Math.random()-.2)*this._options.horizontalSpeed),e=Math.round((1*Math.random()-.5)*(.25*this._height)),(_=new h).copy(p),"right"===t){if(_.add(s,e),p.x>=n)break}else if("left"===t&&(_.subtract(s,e),p.x<=-200))break;l.push({point1:new h(c.x,c.y),point2:new h(p.x,p.y),point3:_,color:d,delay:u,dir:t,alpha:0,phase:0}),c.copy(p),p.copy(_),u+=4,d+=this._options.colorCycleSpeed}this._ribbons.push(l)},_drawRibbonSection:function(t){if(t){if(t.phase>=1&&t.alpha<=0)return!0;if(t.delay<=0){if(t.phase+=.02,t.alpha=1*Math.sin(t.phase),t.alpha=t.alpha<=0?0:t.alpha,t.alpha=t.alpha>=1?1:t.alpha,this._options.animateSections){var i=.1*Math.sin(1+t.phase*Math.PI/2);"right"===t.dir?(t.point1.add(i,0),t.point2.add(i,0),t.point3.add(i,0)):(t.point1.subtract(i,0),t.point2.subtract(i,0),t.point3.subtract(i,0)),t.point1.add(0,i),t.point2.add(0,i),t.point3.add(0,i)}}else t.delay-=.5;var n=this._options.colorSaturation,o=this._options.colorBrightness,s="hsla("+t.color+", "+n+", "+o+", "+t.alpha+" )";this._context.save(),0!==this._options.parallaxAmount&&this._context.translate(0,this._scroll*this._options.parallaxAmount),this._context.beginPath(),this._context.moveTo(t.point1.x,t.point1.y),this._context.lineTo(t.point2.x,t.point2.y),this._context.lineTo(t.point3.x,t.point3.y),this._context.fillStyle=s,this._context.fill(),this._options.strokeSize>0&&(this._context.lineWidth=this._options.strokeSize,this._context.strokeStyle=s,this._context.lineCap="round",this._context.stroke()),this._context.restore()}return!1},_onDraw:function(){for(var t=0,i=this._ribbons.length;t<i;++t)this._ribbons[t]||this._ribbons.splice(t,1);this._context.clearRect(0,0,this._width,this._height);for(var n=0;n<this._ribbons.length;++n){for(var o=this._ribbons[n],s=o.length,h=0,e=0;e<s;++e)this._drawRibbonSection(o[e])&&h++;h>=s&&(this._ribbons[n]=null)}this._ribbons.length<this._options.ribbonCount&&this.addRibbon(),requestAnimationFrame(this._onDraw)},_onResize:function(t){var i=s();this._width=i.width,this._height=i.height,this._canvas&&(this._canvas.width=this._width,this._canvas.height=this._height,this._context&&(this._context.globalAlpha=this._options.colorAlpha))},_onScroll:function(t){var i=s();this._scroll=i.scrolly}},e}());const config={animateSections:!0,colorAlpha:.5,colorBrightness:"50%",colorCycleSpeed:5,colorSaturation:"60%",horizontalSpeed:200,parallaxAmount:-.2,ribbonCount:3,strokeSize:0,verticalPosition:"random"};new Ribbons;</script>


复制这段内容并添加在前面,handsome主题可以在 开发者设置 自定义输出body 尾部的HTML代码中添加。

Last modification:December 8th, 2019 at 05:41 pm
如果觉得我的文章对你有用,请随意赞赏