敲出来的真理-vue引入传统js,传统JavaScript转es格式,ribbon.js转vue

首先建议在操作前阅读下es6的规范,详细了解下模块 Module 的语法

ES6 入门教程https://es6.ruanyifeng.com/#README代码参考

 转 es 前

(function (name, factory) {
    if (typeof window === "object") { window[name] = factory(); }
})("Ribbons", function () {
    var _w = window, _b = document.body, _d = document.documentElement; var random = function () {
        if (arguments.length === 1) {
            if (Array.isArray(arguments[0])) { var index = Math.round(random(0, arguments[0].length - 1)); return arguments[0][index]; }
            return random(0, arguments[0]);
        } else
            if (arguments.length === 2) { return Math.random() * (arguments[1] - arguments[0]) + arguments[0]; }
        return 0;
    }; 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 }; }; 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 }; }; 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; }
    }; 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 = { colorSaturation: "80%", colorBrightness: "60%", colorAlpha: 0.65, colorCycleSpeed: 6, verticalPosition: "center", horizontalSpeed: 200, ribbonCount: 3, strokeSize: 0, parallaxAmount: -0.5, 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(); }; Factory.prototype = {
        constructor: Factory, setOptions: function (options) {
            if (typeof options === "object") {
                for (var key in options) {
                    if (options.hasOwnProperty(key)) { this._options[key] = options[key]; }
                }
            }
        }, 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"] = "rgba(223, 223, 223, 0.3)"; 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();
        }, addRibbon: function () {
            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)); 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; }
            var ribbon = [], point1 = new Point(startx, starty), point2 = new Point(startx, starty), point3 = null, color = Math.round(random(0, 360)), delay = 0; 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; }
                ribbon.push({ 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);
        }, _drawRibbonSection: function (section) {
            if (section) {
                if (section.phase >= 1 && section.alpha <= 0) { return true; }
                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;
        }, _onDraw: function () {
            for (var i = 0, t = this._ribbons.length; i < t; ++i) {
                if (!this._ribbons[i]) { this._ribbons.splice(i, 1); }
            }
            this._context.clearRect(0, 0, this._width, this._height); for (var a = 0; a < this._ribbons.length; ++a) {
                var ribbon = this._ribbons[a], numSections = ribbon.length, numDone = 0; for (var b = 0; b < numSections; ++b) {
                    if (this._drawRibbonSection(ribbon[b])) { numDone++; }
                }
                if (numDone >= numSections) { this._ribbons[a] = null; }
            }
            if (this._ribbons.length < this._options.ribbonCount) { this.addRibbon(); }
            requestAnimationFrame(this._onDraw);
        }, _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; }
            }
        }, _onScroll: function (e) { var screen = screenInfo(e); this._scroll = screen.scrolly; }
    }; return Factory;
}); new Ribbons();

转 es 后 


function RibbonsBase() {
    var _w = window,
        _b = document.body,
        _d = document.documentElement;
    var random = function () {
        if (arguments.length === 1) {
            if (Array.isArray(arguments[0])) {
                var index = Math.round(random(0, arguments[0].length - 1));
                return arguments[0][index];
            }
            return random(0, arguments[0]);
        } else if (arguments.length === 2) {
            return Math.random() * (arguments[1] - arguments[0]) + arguments[0];
        }
        return 0;
    };
    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
        };
    };
    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
        };
    };
    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;
        }
    };
    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 = {
            //色带颜色HSL饱和度
            colorSaturation: "80%",
            //色带颜色HSL亮度量
            colorBrightness: "60%",
            //色带颜色不透明度量
            colorAlpha: 0.65,
            //在HSL颜色空间中循环颜色的速度有多快
            colorCycleSpeed: 6,
            // 每侧Y轴上的起始位置 (top|min, middle|center, bottom|max, random)
            verticalPosition: "random",
            //到达屏幕另一边的速度有多快
            horizontalSpeed: 200,
            //在任何给定时间屏幕上保留多少条色带
            ribbonCount: 5,
            //添加笔划和功能区填充颜色
            strokeSize: 0,
            //按页面滚动上的系数垂直移动功能区
            parallaxAmount: -0.5,
            //随时间向每个功能区区域添加动画效果
            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();
    };
    Factory.prototype = {
        constructor: Factory,
        setOptions: function (options) {
            if (typeof options === "object") {
                for (var key in options) {
                    if (Object.prototype.hasOwnProperty.call(options, 'key')) {
                        this._options[key] = options[key];
                    }
                }
            }
        },
        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"] = "rgba(223, 223, 223, 0.3)";
                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();
        },
        addRibbon: function () {
            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));
            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;
            }
            var ribbon = [],
                point1 = new Point(startx, starty),
                point2 = new Point(startx, starty),
                point3 = null,
                color = Math.round(random(0, 360)),
                delay = 0;
            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;
                }
                ribbon.push({
                    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);
        },
        _drawRibbonSection: function (section) {
            if (section) {
                if (section.phase >= 1 && section.alpha <= 0) {
                    return true;
                }
                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;
        },
        _onDraw: function () {
            for (var i = 0,
                t = this._ribbons.length; i < t; ++i) {
                if (!this._ribbons[i]) {
                    this._ribbons.splice(i, 1);
                }
            }
            this._context.clearRect(0, 0, this._width, this._height);
            for (var a = 0; a < this._ribbons.length; ++a) {
                var ribbon = this._ribbons[a],
                    numSections = ribbon.length,
                    numDone = 0;
                for (var b = 0; b < numSections; ++b) {
                    if (this._drawRibbonSection(ribbon[b])) {
                        numDone++;
                    }
                }
                if (numDone >= numSections) {
                    this._ribbons[a] = null;
                }
            }
            if (this._ribbons.length < this._options.ribbonCount) {
                this.addRibbon();
            }
            requestAnimationFrame(this._onDraw);
        },
        _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;
                }
            }
        },
        _onScroll: function (e) {
            var screen = screenInfo(e);
            this._scroll = screen.scrolly;
        }
    };
    return new Factory();

}

export default {
    dom: function () {
        return document.getElementById("bgCanvas");
    },
    stop: function () {
        var box = document.getElementById("bgCanvas");
        box.parentNode.removeChild(box);
    },
    start: function () {
        window["Ribbons"] = RibbonsBase();
    }
}

vue 页面 

<template>
  <div id="app">
    <div class="content">
      <div class="content_input">
        <div class="title">
          <p>Logo登录</p>
        </div>
        <el-form>
          <el-form-item>
            <el-input
              v-model="companyuid"
              placeholder="公司代码"
              prefix-icon="el-icon-school"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="account"
              placeholder="账号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="password"
              placeholder="密码"
              show-password
              prefix-icon="el-icon-unlock"
            ></el-input>
          </el-form-item>
          <el-form-item type="flex" justify="space-between">
            <el-col :span="11">
              <img src="https://www.oschina.net/action/user/captcha" />
            </el-col>
            <el-col class="line" :span="2">&nbsp;</el-col>
            <el-col :span="11">
              <el-input
                v-model="vcode"
                placeholder="验证码"
                prefix-icon="el-icon-warning-outline"
              ></el-input>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import Ribbons from "../assets/js/ribbon_vue";
export default {
  Ribbons,
  data() {
    return {
      companyuid: "",
      account: "",
      password: "",
      vcode: "",
    };
  },
  mounted: function () {
    Ribbons.start();
  },
  methods: {
    onSubmit() {
      console.log("submit!");
      Ribbons.stop();
    },
  },
};
</script>

  <style>
@charset "utf-8";
* {
  padding: 0;
  margin: 0;
}

.content {
  width: 500px;
  height: 600px;
  box-sizing: border-box;
  padding: 0 50px;
  border-radius: 5px;
  box-shadow: 0px 2px 12px 0px rgba(105, 105, 105, 0.07);
  background: rgba(255, 255, 255, 0.5);
  position: absolute;
  top: 50%;
  left: 80%;
  transform: translate(-50%, -50%);
  animation: mymove 1s ease-in-out alternate;
  overflow: hidden;
  transition: 1.5s;
}

.content_input {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.content_button {
  margin-top: 10px;
}

.el-input {
  margin-bottom: 25px;
}

.title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 30px;
  font-weight: bold;
  color: #606266;
}

.el-button--primary {
  width: 100%;
}
</style>

 附一张效果图^_^

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值