js实现彩带飘舞背景动画效果代码

代码语言:html

所属分类:背景

代码描述:js实现彩带飘舞背景动画效果代码

代码标签: 飘舞 背景 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
</head>
<body>

    <script  type="text/javascript" size="90" alpha="0.2" zIndex="0">

        (function (name, factory) {
            if (typeof window === "object") {
                window[name] = factory();
            }

        })("Ribbons", function () {
            var _w = window,
            _b = document.body,
            //返回html dom中的body节点 即<body>
            _d = document.documentElement; //返回html dom中的root 节点 即<html>

            // 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: 200,
                    // 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.style["background-color"] = "#1f1f1f";
                        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) {
                        c.........完整代码请登录后点击上方下载按钮下载查看

网友评论0