Melody主题的背景代码雨特效

一个多月前折腾完的melody主题上的背景代码雨特效,今天终于良心发现来填坑了)

顺便吐槽一句melody相关的资料真的好少,完全靠自己看主题源码+和其他主题对比来摸索,这大概就是开荒的痛并快乐着吧T.T。(形成用户生态是有多重要.jpg

参考文章(Hexo 博客优化之博客美化系列(持续更新))里的是以Material主题为例,本文以Melody主题为例。

背景效果如图(https://c10udlnk.top/):

image-20210714151554043

以下步骤均以博客文件夹为当前目录。

js文件创建

.\node_modules\hexo-theme-melody\source\js下新建一个DigitalRain.js(跟参考文章的不一样,原文章的特效定位有问题,所以这里有魔改):

window.onload = function(){ var canvas = document.createElement('canvas'), context = canvas.getContext('2d'), pr = window.devicePixelRatio || 1, width = window.innerWidth, height = window.innerHeight canvas.width = width * pr canvas.height = height * pr context.scale(pr, pr) context.globalAlpha = 0.8 canvas.style.cssText = 'opacity:0.8;position:fixed;top:0;left:0;z-index:-1;width:100%;height:100%;pointer-events:none;' // create canvas document.getElementsByTagName('body')[0].appendChild(canvas); // var canvas = document.getElementById("mycanvas"); // //获取画布的上下文 // var context =canvas.getContext("2d"); // var s = window.screen; // var W = canvas.width = s.width; // var H = canvas.height; //获取浏览器屏幕的宽度和高度 //var W = window.innerWidth; //var H = window.innerHeight; //设置canvas的宽度和高度 // canvas.width = W; // canvas.height = H; //每个文字的字体大小 var fontSize = 12; //计算列 var colunms = Math.floor(canvas.width /fontSize); //记录每列文字的y轴坐标 var drops = []; //给每一个文字初始化一个起始点的位置 for(var i=0;i<colunms;i++){ drops.push(0); } //运动的文字 var str ="r3ver5e.4^fun c10udlnk"; //4:fillText(str,x,y);原理就是去更改y的坐标位置 //绘画的函数 function draw(){ context.fillStyle = "rgba(238,238,238,.09)";//遮盖层 context.fillRect(0,0,canvas.width,canvas.height); //给字体设置样式 context.font = "600 "+fontSize+"px Consolas"; //给字体添加颜色 context.fillStyle = randColor();//["#33B5E5", "#0099CC", "#AA66CC", "#9933CC", "#99CC00", "#669900", "#FFBB33", "#FF8800", "#FF4444", "#CC0000"][parseInt(Math.random() * 10)];可以rgb,hsl, 标准色,十六进制颜色 //写入画布中 for(var i=0;i<colunms;i++){ var index = Math.floor(Math.random() * str.length); var x = i*fontSize; var y = drops[i] *fontSize; context.fillText(str[index],x,y); //如果要改变时间,肯定就是改变每次他的起点 if(y >= canvas.height && Math.random() > 0.99){ drops[i] = 0; } drops[i]++; } }; function randColor(){//随机颜色 var r = Math.floor(Math.random() * 256); var g = Math.floor(Math.random() * 256); var b = Math.floor(Math.random() * 256); return "rgb("+r+","+g+","+b+")"; } draw(); setInterval(draw,35); };

更改pug文件

.\node_modules\hexo-theme-melody\layout\includes\additional-js.pug里最开头添加一句:include ./third-party/DigitalRain.pug

image-20210714163807222

然后在.\node_modules\hexo-theme-melody\layout\includes\third-party下新建一个DigitalRain.pug

script(src=url_for('/js/DigitalRain.js'))

image-20210714163902675

这样背景就有代码雨特效啦~


__EOF__

本文作者c10udlnk
本文链接https://www.cnblogs.com/c10udlnk/p/15011719.html
关于博主:欢迎关注我的个人博客-> https://c10udlnk.top/
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   c10udlnk  阅读(398)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示