本文实例为大家分享了Vue实现代码瀑布流背景的具体代码,供大家参考,具体内容如下

先看一下效果:

代码奉上:

<template>
    <canvas id="canvas" />
</template>

<script>
    export default {
        name: "BackCanvas",
        props: {
            height: {
                type: Number,
                default: 500
            }
        },
        data() {
            return {
                settings: {
                    COL_WIDTH: 15,
                    COL_HEIGHT: 15,
                    // 速度参数 最小值:4 - 最大值:8
                    VELOCITY_PARAMS: {
                        min: 3,
                        max: 8
                    },
                    // 代码长度参数  最小值 20  - 最大值 40
                    CODE_LENGTH_PARAMS: {
                        min: 20,
                        max: 40
                    }
                },
                animation: null,
                c: null,
                ctx: null,
                lineC: null,
                ctx2: null,
                WIDTH: window.innerWidth,
                HEIGHT: window.innerHeight,
                COLUMNS: null,
                canvii: [],
                // font from here https://www.dafont.com/matrix-code-nfi.font
                font: '24px matrix-code',
                letters: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'this', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '$', ' ', '-', '*', '/', '=', '%', '"', '\'', '#', '&', '_', '(', ')', ',', '.', ';', ':', '?', '!', '\\', '|', '{', '}', '<', '>', '[', ']', '^', '~'],
                codes: [],
                createCodeLoop: null,
                codesCounter: 0
            }
        },
        mounted() {
            this.init();
        },
        methods: {
            init () {
                this.c = document.getElementById( 'canvas' );
                this.ctx = this.c.getContext( '2d' );
                this.c.width = this.WIDTH;
                this.c.height = this.HEIGHT;

                this.ctx.shadowBlur = 0;
                this.ctx.fillStyle = '#000';
                this.ctx.fillRect(0, 0, this.WIDTH, this.HEIGHT);
                this.ctx.font = this.font;

                this.COLUMNS = Math.ceil(this.WIDTH / this.settings.COL_WIDTH);

                for (let i = 0; i < this.COLUMNS; i  ) {
                    this.codes[i] = [];
                    this.codes[i][0] = {
                        'open': true,
                        'position': {'x': 0, 'y': 0},
                        'strength': 0
                    };
                }

                this.loop();

                this.createLines();

                this.createCode();

                window.onresize = function () {
                    window.cancelAnimationFrame(this.animation);
                    this.animation = null;
                    this.ctx.clearRect(0, 0, this.WIDTH, this.HEIGHT);
                    this.codesCounter = 0;

                    this.ctx2.clearRect(0, 0, this.WIDTH, this.HEIGHT);

                    this.WIDTH = window.innerWidth;
                    this.HEIGHT = window.innerHeight;
                    this.init();
                };
            },
            loop () {
                this.animation = requestAnimationFrame( () => { this.loop(); } );
                this.draw();
            },
            draw () {
                let velocity, height, x, y, c, ctx;
                // slow fade BG colour
                this.ctx.shadowColor = 'rgba(0, 0, 0, 0.5)';
                this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
                this.ctx.fillRect(0, 0, this.WIDTH, this.HEIGHT);
                this.ctx.globalCompositeOperation = 'source-over';
                for (let i = 0; i < this.COLUMNS; i  ) {
                    if (this.codes[i][0].canvas) {
                        velocity = this.codes[i][0].velocity;
                        height = this.codes[i][0].canvas.height;
                        x = this.codes[i][0].position.x;
                        y = this.codes[i][0].position.y - height;
                        c = this.codes[i][0].canvas;
                        ctx = c.getContext('2d');

                        this.ctx.drawImage(c, x, y, this.settings.COL_WIDTH, height);

                        if ((this.codes[i][0].position.y - height) < this.HEIGHT){
                            this.codes[i][0].position.y  = velocity;
                        } else {
                            this.codes[i][0].position.y = 0;
                        }
                    }
                }
            },
            createCode () {
                if (this.codesCounter > this.COLUMNS) {
                    clearTimeout(this.createCodeLoop);
                    return;
                }
                let randomInterval = this.randomFromInterval(0, 100);
                let column = this.assignColumn();
                if (column) {
                    let codeLength = this.randomFromInterval(this.settings.CODE_LENGTH_PARAMS.min, this.settings.CODE_LENGTH_PARAMS.max);
                    let codeVelocity = (Math.random() * (this.settings.VELOCITY_PARAMS.max - this.settings.VELOCITY_PARAMS.min))   this.settings.VELOCITY_PARAMS.min;
                    let lettersLength = this.letters.length;

                    this.codes[column][0].position = {'x': (column * this.settings.COL_WIDTH), 'y': 0};
                    this.codes[column][0].velocity = codeVelocity;
                    this.codes[column][0].strength = this.codes[column][0].velocity / this.settings.VELOCITY_PARAMS.max;

                    for (let i = 1; i <= codeLength; i  ) {
                        let newLetter = this.randomFromInterval(0, (lettersLength - 1));
                        this.codes[column][i] = this.letters[newLetter];
                    }

                    this.createCanvii(column);

                    this.codesCounter  ;
                }
                this.createCodeLoop = setTimeout(this.createCode, randomInterval);

            },
            createCanvii (i) {
                let codeLen = this.codes[i].length - 1;
                let canvHeight = codeLen * this.settings.COL_HEIGHT;
                let velocity = this.codes[i][0].velocity;
                let strength = this.codes[i][0].strength;
                let text, fadeStrength;

                let newCanv = document.createElement('canvas');
                let newCtx = newCanv.getContext('2d');

                newCanv.width = this.settings.COL_WIDTH;
                newCanv.height = canvHeight;

                for (let j = 1; j < codeLen; j  ) {
                    text = this.codes[i][j];
                    newCtx.globalCompositeOperation = 'source-over';
                    newCtx.font = '24px matrix-code';

                    if (j < 5) {
                        newCtx.shadowColor = 'hsl(104, 79%, 74%)';
                        newCtx.shadowOffsetX = 0;
                        newCtx.shadowOffsetY = 0;
                        // 设置模糊程度
                        newCtx.shadowBlur = 6;
                        newCtx.fillStyle = 'hsla(104, 79%, '   (100 - (j * 5))   '%, '   strength   ')';
                    } else if (j > (codeLen - 4)) {
                        fadeStrength = j / codeLen;
                        fadeStrength = 1 - fadeStrength;

                        newCtx.shadowOffsetX = 0;
                        newCtx.shadowOffsetY = 0;
                        newCtx.shadowBlur = 0;
                        newCtx.fillStyle = 'hsla(104, 79%, 74%, '   (fadeStrength   0.3)   ')';
                    } else {
                        newCtx.shadowOffsetX = 0;
                        newCtx.shadowOffsetY = 0;
                        newCtx.shadowBlur = 0;
                        newCtx.fillStyle = 'hsla(104, 79%, 74%, '   strength   ')';
                    }

                    newCtx.fillText(text, 0, (canvHeight - (j * this.settings.COL_HEIGHT)));
                }

                this.codes[i][0].canvas = newCanv;

            },
            createLines () {
                this.linesC = document.createElement('canvas');
                this.linesC.width = this.WIDTH;
                this.linesC.height = this.HEIGHT;
                this.linesC.style.position = 'fixed';
                this.linesC.style.top = 0;
                this.linesC.style.left = 0;
                this.linesC.style.zIndex = 10;
                document.body.appendChild(this.linesC);

                let linesYBlack = 0;
                let linesYWhite = 0;
                this.ctx2 = this.linesC.getContext('2d');

                this.ctx2.beginPath();

                this.ctx2.lineWidth = 1;
                this.ctx2.strokeStyle = 'rgba(0, 0, 0, 0.7)';

                while (linesYBlack < this.HEIGHT) {

                    this.ctx2.moveTo(0, linesYBlack);
                    this.ctx2.lineTo(this.WIDTH, linesYBlack);

                    linesYBlack  = 5;
                }

                this.ctx2.lineWidth = 0.15;
                this.ctx2.strokeStyle = 'rgba(255, 255, 255, 0)';

                while (linesYWhite < this.HEIGHT) {

                    this.ctx2.moveTo(0, linesYWhite 1);
                    this.ctx2.lineTo(this.WIDTH, linesYWhite 1);

                    linesYWhite  = 5;
                }

                this.ctx2.stroke();
            },
            assignColumn () {
                let randomColumn = this.randomFromInterval(0, (this.COLUMNS - 1));

                if (this.codes[randomColumn][0].open) {
                    this.codes[randomColumn][0].open = false;
                } else {
                    return false;
                }

                return randomColumn;
            },
            randomFromInterval (from, to) {
                return Math.floor(Math.random() * (to - from  1 )   from);
            }

        }
    }
</script>

<style scoped>
/** 让这个背景固定在页面不随着滚动而滚动 */
 #canvas {
     position: fixed;
     top: 0;
     left: 0;
 }
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

Vue实现炫酷的代码瀑布流背景的更多相关文章

  1. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  2. HTML5 canvas 瀑布流文字效果的示例代码

    这篇文章主要介绍了HTML5 canvas 瀑布流文字效果的示例代码的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. 小程序瀑布流解决左右两边高度差距过大的问题

    这篇文章主要介绍了小程序瀑布流解决左右两边高度差距过大的问题的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. 3种方式实现瀑布流布局小结

    这篇文章主要介绍了3种方式实现瀑布流布局小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. Swift:用UICollectionView整一个瀑布流

    除了以上说到的内容之外,collectionview还有一个专门处理布局的UICollectionViewLayout。苹果给了一个基础的布局UICollectionViewFlowLayout,可以实现一个基本的流式布局。直接干掉,拖一个UICollectionViewController进去并设置为默认的Controller。之后在interfacebuilder中把collectionview的类设置为HomeCollectionViewController。接下来再次回到collectionvie

  6. Swift UITableView瀑布流/NSURLConnection异步网络请求

    去年写过一个OC版本的瀑布流Demo《UITableView实现的瀑布流效果》。接触Swift一段时间了,今天就是用Swift再写了一个瀑布流的Demo。原理是一样的这里不再赘述。在写这个Demo的过程中是用到了NSURLConnection的异步网络请求和GCD做了一个图片的异步加载,没有做图片的缓存,所以是用起来有些卡。cell是带左滑删除视图的,删除功能是没有实现的。O(∩_∩)O哈哈~

  7. vue自定义加载指令v-loading占位图指令v-showimg

    这篇文章主要为大家介绍了vue自定义加载指令和v-loading占位图指令v-showimg的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  8. vue使用动画实现滚动表格效果

    这篇文章主要为大家详细介绍了vue使用动画实现滚动表格效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. 关于Vue 监控数组的问题

    这篇文章主要介绍了Vue 监控数组的示例,主要包括Vue 是如何追踪数据发生变化,Vue 如何更新数组以及为什么有些数组的数据变更不能被 Vue 监测到,对vue监控数组知识是面试比较常见的问题,感兴趣的朋友一起看看吧

  10. Vue子组件props从父组件接收数据并存入data

    这篇文章主要介绍了Vue子组件props从父组件接收数据并存入data的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部