女朋友想让我的网站下点雪,我立马打开电脑…(附源码)

news/2024/5/21 0:14:51/文章来源:https://blog.csdn.net/weixin_35681869/article/details/112551183

点击上方[全栈开发者社区]右上角[...][设为星标⭐]

前言

女朋友常逛的设计网站这两天页面上多了下雪的效果,于是问我我的网站能下雪吗,作为一个程序员我一般会说实现不了,但是作为男朋友,不能说不行。

雪我们可以使用span标签和css的径向渐变简单意思一下:

.snow {display: block;width: 100px;height: 100px;background-image: radial-gradient(#fff 0%, rgba(255, 255, 255, 0) 60%);border-radius: 50%;
}
复制代码

效果如下:

很多雪

一片雪是不够的,成千上万才浪漫,世界上没有两片相同的雪花,所以每片雪都有自己的大小位置速度等属性,为此先创建一个雪花类:

class Snow {constructor (opt = {}) {// 元素this.el = null// 直径this.width = 0// 最大直径this.maxWidth = opt.maxWidth || 80// 最小直径this.minWidth = opt.minWidth || 2// 透明度this.opacity = 0// 水平位置this.x = 0// 重置位置this.y = 0// 速度this.speed = 0// 最大速度this.maxSpeed = opt.maxSpeed || 4// 最小速度this.minSpeed = opt.minSpeed || 1// 浏览器窗口尺寸this.windowWidth = window.innerWidththis.windowHeight = window.innerHeightthis.init()}// 初始化各种属性init () {this.width = Math.floor(Math.random() * this.maxWidth + this.minWidth)this.opacity = Math.random()this.x = Math.floor(Math.random() * (this.windowWidth - this.width))this.y = Math.floor(Math.random() * (this.windowHeight - this.width))this.speed = Math.random() * this.maxSpeed + this.minSpeed}// 设置样式setStyle () {this.el.style.cssText = `position: fixed;left: 0;top: 0;display: block;width: ${this.width}px;height: ${this.width}px;opacity: ${this.opacity};background-image: radial-gradient(#fff 0%, rgba(255, 255, 255, 0) 60%);border-radius: 50%;z-index: 9999999999999;pointer-events: none;transform: translate(${this.x}px, ${this.y}px);`}// 渲染render () {this.el = document.createElement('div')this.setStyle()document.body.appendChild(this.el)}
}
复制代码

init方法用来生成随机的初始大小、位置、速度等属性,在浏览器窗口内new100片试试:

let snowList = []
for (let i = 0; i < 100; i++) {let snow = new Snow()snow.render()snowList.push(snow)
}
复制代码

效果如下:

动起来

雪动起来才能叫下雪,动起来很简单,不断改变xy坐标就可以了,给snow类加个运动的方法:

class snow {move () {this.x += this.speedthis.y += this.speedthis.el.style.left = this.x + 'px'this.el.style.top = this.y + 'px'}
}
复制代码

接下来使用requestAnimationFrame不断刷新:

moveSnow () {window.requestAnimationFrame(() => {snowList.forEach((item) => {item.move()})moveSnow()})
}
复制代码

效果如下,因为速度是正数,所以整体是往右斜的:

可以看到动起来了,但是出屏幕就不见了,所以雪是会消失的对吗?要让雪不停很简单,检测雪的位置,如果超出屏幕了就让它回到顶部,修改一下move方法:

move () {this.x += this.speedthis.y += this.speed// 完全离开窗口就调一下初始化方法,另外还需要修改一下init方法,因为重新出现我们是希望它的y坐标为0或者小于0,这样就不会又凭空出现的感觉,而是从天上下来的if (this.x < -this.width || this.x > this.windowWidth || this.y > this.windowHeight) {this.init(true)this.setStyle()}this.el.style.left = this.x + 'px'this.el.style.top = this.y + 'px'}
复制代码
init (reset) {// ...this.width = Math.floor(Math.random() * this.maxWidth + this.minWidth)this.y = reset ? -this.width : Math.floor(Math.random() * this.windowHeight)// ...}
复制代码

这样就能源源不断的下雪了:

优化

1.水平速度

水平和垂直方向的速度是一样的,但是看起来有点太斜了,所以调整一下,把水平速度和垂直速度区分开来:

class Snow {constructor (opt = {}) {// ...// 水平速度this.sx = 0// 垂直速度this.sy = 0// ...}init (reset) {// ...this.sy = Math.random() * this.maxSpeed + this.minSpeedthis.sx = this.sy * Math.random()}move () {this.x += this.sxthis.y += this.sy// ...}
}
复制代码

2.左下角没有雪

因为整体向右倾斜,所以左下角大概率没有雪,这可以通过让雪随机出现在左侧来解决:

init (reset) {// ...this.x = Math.floor(Math.random() * (this.windowWidth - this.width))this.y = Math.floor(Math.random() * (this.windowHeight - this.width))if (reset && Math.random() > 0.8) {// 让一小部分的雪初始化在左侧this.x = -this.width} else if (reset) {this.y = -this.width}// ...
}
复制代码

3.眼前的雪

随机性的选择一点雪给它较大的体积、透明度和速度,然后再使用css33D透视效果,把它的z轴数值调大一点,这样的感觉就好像是在眼前划过的一样:

<body style="perspective: 500;-webkit-perspective: 500"></body>
复制代码
class Snow {constructor (opt = {}) {// ...// z轴数值this.z = 0// 快速划过的最大速度this.quickMaxSpeed = opt.quickMaxSpeed || 10// 快速划过的最小速度this.quickMinSpeed = opt.quickMinSpeed || 8// 快速划过的宽度this.quickWidth = opt.quickWidth || 80// 快速划过的透明度this.quickOpacity = opt.quickOpacity || 0.2// ...}init (reset) {let isQuick = Math.random() > 0.8this.width = isQuick ? this.quickWidth : Math.floor(Math.random() * this.maxWidth + this.minWidth)this.z = isQuick ? Math.random() * 300 + 200 : 0this.opacity = isQuick ? this.quickOpacity : Math.random()// ...this.sy = isQuick ? Math.random() * this.quickMaxSpeed + this.quickMinSpeed : Math.random() * this.maxSpeed + this.minSpeed// ...}move () {// ...this.el.style.transform = `translate3d(${this.x}px, ${this.y}px, ${this.z}px)`}
}
复制代码

4.鹅毛大雪

雪花嘛,轻如鹅毛,鹅毛是怎么飘的?是不是左右摆动的飘?那我们也可以选择一部分的雪花让它跟鹅毛一样飘,左右摇摆很简单,速度一会加一会减就可以了:

class Snow {constructor (opt = {}) {// ...// 是否左右摇摆this.isSwing = false// 左右摇摆的步长this.stepSx = 0.03// ...}// 随机初始化属性init (reset) {// ...this.isSwing = Math.random() > 0.8// ...}move () {if (this.isSwing) {if (this.sx >= 1 || this.sx <= -1) {this.stepSx = -this.stepSx}this.sx += this.stepSx}// ...}
}
复制代码

除了上述这种方法,左右摇摆还有一种方式,就是使用正弦或余弦函数,因为它们的曲线翻转90度就是左右摇摆:

img

我们使用正弦函数,公式为:y=sin(x)x的值是弧度表示,只要一直增加就可以了,y的值用来修改雪花的水平方向的速度变化步长:

class Snow {constructor (opt = {}) {// ...// 是否左右摇摆this.isSwing = false// 左右摇摆的正弦函数x变量this.swingRadian = 0// 左右摇摆的正弦x步长this.swingStep = 0.01// ...}init (reset) {// ...this.swingStep = 0.01 * Math.random()}move () {if (this.isSwing) {this.swingRadian += this.swingStepthis.x += this.sx * Math.sin(this.swingRadian * Math.PI) * 0.2} else {this.x += this.sx}// ...}
}
复制代码

因为正弦函数y的值是从1变化到-1,摆动幅度太了,所以乘了个小数0.2缩小一点,想要幅度小一点,还有一个方法是不要使用整个正弦曲线,可以从中截取一个适合的区间大小,比如就让x的值在0.9π1.1π之前变化:

class Snow {constructor (opt = {}) {// ...// 是否左右摇摆this.isSwing = false// 左右摇摆的正弦函数x变量this.swingRadian = 1// 需要改成一个中间值// 左右摇摆的正弦x步长this.swingStep = 0.01// ...}init (reset) {// ...this.swingStep = 0.01 * Math.random()this.swingRadian = Math.random() * (1.1 - 0.9) + 0.9// 也让它随机一下}move () {if (this.isSwing) {if (this.swingRadian > 1.1 || this.swingRadian < 0.9) {this.swingStep = -this.swingStep}this.swingRadian += this.swingStepthis.x += this.sx * Math.sin(this.swingRadian * Math.PI)} else {this.x += this.sx}// ...}
}
复制代码

5.下的慢一点

既然给水平加了曲线,垂直方向上是不是也可以改成非匀速呢?当然可以,区别是速度得一直是正的,不然就要出现反自然现象了,改变速度曲线同样可以使用正余弦,上面我们使用了0.9π1.1π之间的正弦曲线,根据上图可以发现对应的余弦曲线都是负的,趋势是先慢后快,所以可以利用这一段来改变垂直方向的速度:

move () {if (this.isSwing) {if (this.swingRadian > 1.1 || this.swingRadian < 0.9) {this.swingStep = -this.swingStep}this.swingRadian += this.swingStepthis.x += this.sx * Math.sin(this.swingRadian * Math.PI)this.y -= this.sy * Math.cos(this.swingRadian * Math.PI)// 因为速度都是负的,所以改成-} else {this.x += this.sxthis.y += this.sy}// ...
}
复制代码

6.在最上面

为了防止为页面上原本层级更高的元素遮挡,给雪花的样式加一个很大的层级:

render () {this.el = document.createElement('div')this.el.style.cssText = `// ...z-index: 9999999999999;`document.body.appendChild(this.el)
}
复制代码

7.看不见我

修改了层级,所以雪花会在页面的最上层,那么可能会挡住其他元素的鼠标事件,需要禁止它响应鼠标事件:

render () {this.el = document.createElement('div')this.el.style.cssText = `// ...pointer-events: none;`document.body.appendChild(this.el)}
复制代码

8.更好一点

使用性能更好的transform属性来做动画:

render () {this.el = document.createElement('div')this.el.style.cssText = `left: 0;top: 0;transform: translate(${this.x}px, ${this.y}px);`document.body.appendChild(this.el)
}
复制代码
move () {// ...// this.el.style.left = this.x + 'px'// this.el.style.top = this.y + 'px'this.el.style.transform = `translate(${this.x}px, ${this.y}px)`
}
复制代码

当然,最好的方式是用canvas来画。

最终效果:

下雨&雨夹雪

下完雪,接下来顺便下个雨,雨和雪差不多,都是从天上掉下来,但是雨的速度更快,通常也不会左右摇摆什么的,方向也基本是一致的,先来修改一下样式:

setStyle () {this.el.style.cssText = `// ...width: 1px;// ...`
}
复制代码

很简单,只要把宽度写死为1就行了:

接下来把摇摆去掉:

move () {this.x += this.sxthis.y += this.sy// ...
}
复制代码

效果如下:

可以发现雨是竖着在水平移动,显然是不行的,需要让它倾斜一定的角度,和运动方向保持一致,这个也很简单,算一下斜率,水平速度除以垂直速度:

move () {// ...this.el.style.transform = `translate(${this.x}px, ${this.y}px) ${this.getRotate(this.sy, this.sx)}`
}
getRotate(sy, sx) {return `rotate(${sx === 0 ? 0 : (90 + Math.atan(sy / sx) * (180 / Math.PI))}deg)`
}
复制代码

因为tan(θ)=sy/sxθ=Math.atan(sy / sx),因为雨的线段默认是从上到下垂直的,θ是代表和水平方向上的夹角,所以需要先旋转90度,再旋转夹角的度数,最后弧度转角度的公式为:角度=弧度*(180/π)。

雨和雪都实现了,让它们一起出来,就是雨夹雪了:

根据天气下雪

把上面的代码放到网站上就有下雪的效果了,另外也可以使用天气厂商的api,根据实时天气来下雪或者下雨,再实现一下太阳、乌云等效果,一个沉浸式天气就完成了,有兴趣的可自行实践。

转自:街角小林

juejin.cn/post/6910857740327845901

完整代码

https://github.com/wanglin2/snow

- EOF -

觉得本文对你有帮助?请分享给更多人关注「全栈开发者社区」加星标,提升全栈技能
本公众号会不定期给大家发福利,包括送书、学习资源等,敬请期待吧!
如果感觉推送内容不错,不妨右下角点个在看转发朋友圈或收藏,感谢支持。
好文章,留言、点赞、在看和分享一条龙吧❤️

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_727211.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

amazon s3_在Amazon S3上托管网站

amazon s3Everyone knows that Amazon S3 is great for storing files. Its fast, inexpensive, and easy to setup. What you may not realize is that you can also host static websites on this robust platform. 众所周知&#xff0c; Amazon S3非常适合存储文件。 它快速…

linux文件服务器用途,文件服务器CPU篇 - 构建文件服务器:OS与CPU应用选择篇_服务器应用_Linux公社-Linux系统门户网站...

文件服务器CPU篇文件服务器的主要用途是存储&#xff0c;而不是处理能力&#xff0c;特别是家庭文件服务器而言&#xff0c;包括处理器在内部件都应该让位于硬盘、机箱和电源。文件服务器不需要最新的处理器&#xff0c;对于可能从来没必要将数据同时分发到多个客户机的文件服务…

必看,程序员应该访问的最佳网站

【公众号回复 “1024”&#xff0c;免费领取程序员赚钱实操经验】大家好&#xff0c;我是章鱼猫。今天给大家推荐的这个项目是「Best-websites-a-programmer-should-visit-zh」—— 程序员应该访问的最佳网站中文版&#xff0c;一些对程序员有用的网站。在学习 CS 的时候有一些…

查找微信公众号服务器,墨涩网 - 免插件实现微信公众号搜索连接wordpress网站文章——墨涩网...

微信公众号当前是每个用户都在使用的功能&#xff0c;而使用wordpress程序的站长会希望把自己的网站连接到微信公众号。连接后通过公众号访问网站的内容。目前网上有很多可以实现微信公众号和wordpress网站链接的插件很多&#xff0c;个人觉得醉的最好的应该是功能强大的水煮鱼…

php 网站语言切换,php 多语言切换

目录结构&#xff1a; // 文件内容&#xff1a; /include/language.php Java代码 ?php $languagesarray(); $languages[ zh-cn ][ name ] china ; $languages[ zh-cn ][ image ] flag1.jpg ; $languages[ en ][ name ] english目录结构&#xff1a;//文件内容&#xff1a;/inc…

突发:全球最大的同性交友网站挂了,修复长达四个小时的背后是一个悲伤的故事...

loonggg读完需要3分钟速读仅需 1 分钟今天下午&#xff0c;作为全球最大的同性交友网站&#xff1a;GitHub&#xff0c;突然挂了&#xff0c;一直在这两张图中间来回切换。再刷新一下&#xff0c;从 500 变成了小马&#xff0c;你还别说&#xff0c;我仔细看这个小马&#xff0…

2017年最受欢迎的10个编程挑战网站

来源&#xff1a;CSDN大数据 作者&#xff1a;Daniel Borowski 本文长度为1704字&#xff0c;建议阅读4分钟 如果你想不断地提高自己的编程技能&#xff0c;那么不断尝试去解决那些编程中的难题&#xff0c;这是一个非常不错的途径。本文为你列举了10个编程挑战网站&#xff0c…

透过日播放量超过6亿的《延禧攻略》,看2018视频网站格局

作者介绍徐麟目前就职于上海唯品会产品技术中心&#xff0c;哥大统计数据狗&#xff0c;从事数据挖掘&分析工作&#xff0c;喜欢用R&Python玩一些不一样的数据文章来源数据森麟如需转载&#xff0c;请联系原作者授权前言随着《延禧攻略》的播出&#xff0c;魏璎珞、富察…

看看黑客是如何攻破一个网站的?网友:原来...

点击上方[全栈开发者社区]→右上角[...]→[设为星标⭐]点击领取全栈资料&#xff1a;全栈资料通过本文你将了解黑客常用的入手思路和技术手法&#xff0c;适合热爱网络信息安全的新手朋友了解学习。本文将从最开始的信息收集开始讲述黑客是如何一步步的攻破你的网站和服务器的。…

有哪些网站,一旦知道,你就离不开了?

loonggg读完需要5分钟速读仅需 2 分钟大家好&#xff0c;我是你们的校长。感觉好久没有给大家分享工具类的好东西了&#xff0c;今天给大家分享几个好东西。作为程序员&#xff0c;一个技术人员&#xff0c;我从工作的角度和职场的角度给大家推荐几个非常不错的网站吧&#xff…

添加javascript_使用JavaScript将搜索添加到您的网站

添加javascriptStatic website generators like Gatsby and Jekyll are popular because they allow the creation of complex, templated pages that can be hosted anywhere. But the awesome simplicity of website generators is also limiting. Search is particularly ha…

不吸吮的网站建设者

我从不追求真棒 (I Never Pursue My Awesome Ideas) Im constantly struck with ideas for new web projects. Unfortunately, I rarely act on them because I have too little time and theyre too much of a hassle to see through. 我一直对新的Web项目的想法感到惊讶。 不…

GitHub Star 10K,让你的网站更炫酷的开源库

【公众号回复 “1024”&#xff0c;免费领取程序员赚钱实操经验】大家好&#xff0c;我是你们的章鱼猫。现在不少网站都支持了骨架屏&#xff0c;能够在网页数据加载前&#xff0c;展示固定的布局&#xff0c;能够减少用户在进入网页时感受到白屏的不适感。今天要给大家推荐一个…

bootstrap网站框架_启动框架:网站构建器,Bootstrap主题等

bootstrap网站框架One conclusion that Ive come to when evaluating my skills is that Im not a designer. Each time I redesign this blog, I identify a few sites I like and then meld them together. Of course that means I end up with UI components that dont lo…

牛逼啊,这两个程序员生财案例网站

loonggg读完需要4分钟速读仅需 2 分钟今天是日更生财技能的第二天。我决定每天写一篇生财感悟类的文章&#xff0c;带领大家每天「钱近」一小步。今天我读「生财日历」上 2 号的内容&#xff0c;上面讲的是一个变现超过 100 万美元的国外网课销售漏斗模型的案例。大致内容讲的就…

相亲网站比自己优秀的男人太多?单身程序员惊现神操作!

戳蓝字“CSDN云计算”关注我们哦&#xff01;话说&#xff0c;今年还有不到一个月就要过去了&#xff0c;作为一个单身狗的小编&#xff0c;看着大街上一个个成双成对的情侣&#xff0c;不由感到悲从心来&#xff0c;只能妄图从新闻中寻找一丝安慰。结果你别说&#xff0c;我最…

SEO核心技术纯白帽快速排名方法

我相信很多朋友都知道SEO快速排名&#xff0c;现在流行的快速排名都是众人皆知的黑帽SEO技术&#xff0c;但是却唯独不知道纯白帽也可以快速排名。但是我估计大家都看到过很多新站在短短数月内就上了首页&#xff0c;权重从0升到3&#xff0c;这难道是黑帽吗&#xff0c;不是的…

聊聊云计算:为什么构建网站时常会用到负载均衡

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者&#xff1a;疯子程序员来源&#xff1a;https://blog.csdn.net/qq_40196321/article/details/85075746 负载均衡可以将客户端请求分摊到多个操作单元上进行处理硬件负载均衡负载均衡有很多种不同的实现方式&#xff0c;总的来说…

java比递归查询更快方法_使网站更小更快的5种方法

java比递归查询更快方法Confession: Id say once a week I genuinely wish I was a kid who spent his work day cutting grass and doing landscaping. Why? Because at the end of the day, they are able to say "the grass is cut, the job is complete." A…

干货 | NLP、知识图谱教程、书籍、网站、工具...(附资源链接)

来源&#xff1a;人工智能头条本文多资源&#xff0c;建议阅读收藏。本文整理了关于 NLP 与知识图谱的众多参考资源&#xff0c;涵盖内容与形式非常丰富。[ 导读 ]本文作者一年前整理了这份关于 NLP 与知识图谱的参考资源&#xff0c;涵盖内容与形式也是非常丰富&#xff0c;接…