<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45