1. 定義標(biāo)簽:
<h1>北極光之夜。</h1>
<div class="bg"></div>
<canvas id="canvas"></canvas>
h1是標(biāo)題,.bg是背景圖,canvas是畫布。
2. 開(kāi)始js部分,先獲取畫布:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
3.定義常量與數(shù)組,看注釋:
/* 定義x為窗口寬度,y為窗口高度 */
var x = 0 ,y=0;
/* 定義數(shù)組,是為了存儲(chǔ)每一片雪的信息 */
var arr=[];
/* 假設(shè)有600片雪 */
var num=600;
4.初始化數(shù)組,給每片雪花定義隨機(jī)位置坐標(biāo)(x,y)與半徑(r)與一個(gè)顏色(color):
for(let i=0;i<num;i++){
arr.push({
x: x*Math.random(),
y: y*Math.random(),
r: Math.random()*5,
color:`rgba(255,255,255,${Math.random()})`
})
}
Math.random()會(huì)返回0到1間的一個(gè)隨機(jī)數(shù)。
5.繪制每片雪與雪花:
/* 創(chuàng)建image元素 */
var img = new Image();
/* 設(shè)置雪花的地址,只有雪花是用圖片表示 */
img.src = "img/snow.png";
function draw(){
/* 遍歷數(shù)組 */
for(let i=0;i<num;i++){
var yuan = arr[i];
/* 創(chuàng)建路徑 */
ctx.beginPath();
/* 給雪設(shè)置顏色 */
ctx.fillStyle = yuan.color;
/* 繪制雪 */
ctx.arc(yuan.x,yuan.y,yuan.r,0,2*3.14,false);
/* 如果i%30==0才繪制雪花,雪花不用太多 */
if(i%30==0){
/* 繪制雪花圖片 */
ctx.drawImage(img,yuan.x,yuan.y,yuan.r*10,yuan.r*10);
}
/* 填充路徑 */
ctx.fill();
}
}
6.更新雪的位置:
function updated() {
for(let i=0;i<num;i++){
var yuan = arr[i];
/* x軸位置+0.1,變化小點(diǎn) */
yuan.x = yuan.x + 0.1;
/* y軸位置+自身半徑一半,這樣越大的學(xué)走越快 */
yuan.y = yuan.y+yuan.r/2;
/* 如果學(xué)已經(jīng)走出窗口 */
if(yuan.y>y){
/* 重新給雪數(shù)組賦值 */
arr[i]={ x: x*Math.random(),
y: -10,
r: Math.random()*5,
color:`rgba(255,255,255,${Math.random()})`}
}
}
}
7.設(shè)置動(dòng)畫:
setInterval(function(){
/* 清屏 */
ctx.clearRect(0,0,x,y);
/* 繪制 */
draw();
/* 更新 */
updated();
},10)
8.完善,讓x與y自適應(yīng)窗口寬度,調(diào)用下面函數(shù)便可:
/* 綁定窗口大小發(fā)生改變事件,讓canvas隨時(shí)鋪滿瀏覽器可視區(qū) */
window.onresize=resizeCanvas;
function resizeCanvas(){
x=canvas.width=window.innerWidth;
y=canvas.height=window.innerHeight;
}
resizeCanvas();
下是一個(gè)簡(jiǎn)單的PHP下雪效果代碼示例:
```php
<!DOCTYPE html>
<html>
<head>
<title>下雪效果</title>
<style type="text/css">
body {
background-color: #333;
overflow: hidden;
}
.snowflake {
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
animation: fall linear infinite;
}
@keyframes fall {
from {top: -10px;}
to {top: 100%;}
}
</style>
</head>
<body>
<script type="text/javascript">
for (var i = 0; i < 100; i++) {
(function(i) {
setTimeout(function() {
var snowflake = document.createElement('div');
snowflake.className = 'snowflake';
snowflake.style.left = Math.random() * window.innerWidth + 'px';
snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';
document.body.appendChild(snowflake);
}, Math.random() * 2000 + 500);
})(i);
}
</script>
</body>
</html>
```
該代碼使用JavaScript創(chuàng)建了100個(gè)雪花元素,并使用CSS動(dòng)畫實(shí)現(xiàn)了雪花下落的效果。您可以將代碼保存為一個(gè)HTML文件并在瀏覽器中打開(kāi),即可看到下雪的動(dòng)畫效果。
言
女朋友常逛的設(shè)計(jì)網(wǎng)站這兩天頁(yè)面上多了下雪的效果,于是問(wèn)我我的網(wǎng)站能下雪嗎,作為一個(gè)程序員我一般會(huì)說(shuō)實(shí)現(xiàn)不了,但是作為男朋友,不能說(shuō)不行。
雪我們可以使用span標(biāo)簽和css的徑向漸變簡(jiǎn)單意思一下:
.snow {
display: block;
width: 100px;
height: 100px;
background-image: radial-gradient(#fff 0%, rgba(255, 255, 255, 0) 60%);
border-radius: 50%;
}
復(fù)制代碼
效果如下:
一片雪是不夠的,成千上萬(wàn)才浪漫,世界上沒(méi)有兩片相同的雪花,所以每片雪都有自己的大小位置速度等屬性,為此先創(chuàng)建一個(gè)雪花類:
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.innerWidth
this.windowHeight = window.innerHeight
this.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
}
// 設(shè)置樣式
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)
}
}
復(fù)制代碼
init方法用來(lái)生成隨機(jī)的初始大小、位置、速度等屬性,在瀏覽器窗口內(nèi)new100片試試:
let snowList = []
for (let i = 0; i < 100; i++) {
let snow = new Snow()
snow.render()
snowList.push(snow)
}
復(fù)制代碼
效果如下:
雪動(dòng)起來(lái)才能叫下雪,動(dòng)起來(lái)很簡(jiǎn)單,不斷改變x和y坐標(biāo)就可以了,給snow類加個(gè)運(yùn)動(dòng)的方法:
class snow {
move () {
this.x += this.speed
this.y += this.speed
this.el.style.left = this.x + 'px'
this.el.style.top = this.y + 'px'
}
}
復(fù)制代碼
接下來(lái)使用requestAnimationFrame不斷刷新:
moveSnow () {
window.requestAnimationFrame(() => {
snowList.forEach((item) => {
item.move()
})
moveSnow()
})
}
復(fù)制代碼
效果如下,因?yàn)樗俣仁钦龜?shù),所以整體是往右斜的:
可以看到動(dòng)起來(lái)了,但是出屏幕就不見(jiàn)了,所以雪是會(huì)消失得對(duì)嗎?要讓雪不停很簡(jiǎn)單,檢測(cè)雪的位置,如果超出屏幕了就讓它回到頂部,修改一下move方法:
move () {
this.x += this.speed
this.y += this.speed
// 完全離開(kāi)窗口就調(diào)一下初始化方法,另外還需要修改一下init方法,因?yàn)橹匦鲁霈F(xiàn)我們是希望它的y坐標(biāo)為0或者小于0,這樣就不會(huì)又憑空出現(xiàn)的感覺(jué),而是從天上下來(lái)的
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'
}
復(fù)制代碼
init (reset) {
// ...
this.width = Math.floor(Math.random() * this.maxWidth + this.minWidth)
this.y = reset ? -this.width : Math.floor(Math.random() * this.windowHeight)
// ...
}
復(fù)制代碼
這樣就能源源不斷地下雪了:
水平和垂直方向的速度是一樣的,但是看起來(lái)有點(diǎn)太斜了,所以調(diào)整一下,把水平速度和垂直速度區(qū)分開(kāi)來(lái):
class Snow {
constructor (opt = {}) {
// ...
// 水平速度
this.sx = 0
// 垂直速度
this.sy = 0
// ...
}
init (reset) {
// ...
this.sy = Math.random() * this.maxSpeed + this.minSpeed
this.sx = this.sy * Math.random()
}
move () {
this.x += this.sx
this.y += this.sy
// ...
}
}
復(fù)制代碼
因?yàn)檎w向右傾斜,所以左下角大概率沒(méi)有雪,這可以通過(guò)讓雪隨機(jī)出現(xiàn)在左側(cè)來(lái)解決:
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) {// 讓一小部分的雪初始化在左側(cè)
this.x = -this.width
} else if (reset) {
this.y = -this.width
}
// ...
}
復(fù)制代碼
隨機(jī)性的選擇一點(diǎn)雪給它較大的體積、透明度和速度,然后再使用css3的3D透視效果,把它的z軸數(shù)值調(diào)大一點(diǎn),這樣的感覺(jué)就好像是在眼前劃過(guò)的一樣:
<body style="perspective: 500;-webkit-perspective: 500"></body>
復(fù)制代碼
class Snow {
constructor (opt = {}) {
// ...
// z軸數(shù)值
this.z = 0
// 快速劃過(guò)的最大速度
this.quickMaxSpeed = opt.quickMaxSpeed || 10
// 快速劃過(guò)的最小速度
this.quickMinSpeed = opt.quickMinSpeed || 8
// 快速劃過(guò)的寬度
this.quickWidth = opt.quickWidth || 80
// 快速劃過(guò)的透明度
this.quickOpacity = opt.quickOpacity || 0.2
// ...
}
init (reset) {
let isQuick = Math.random() > 0.8
this.width = isQuick ? this.quickWidth : Math.floor(Math.random() * this.maxWidth + this.minWidth)
this.z = isQuick ? Math.random() * 300 + 200 : 0
this.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)`
}
}
復(fù)制代碼
雪花嘛,輕如鵝毛,鵝毛是怎么飄的?是不是左右擺動(dòng)的飄?那我們也可以選擇一部分的雪花讓它跟鵝毛一樣飄,左右搖擺很簡(jiǎn)單,速度一會(huì)加一會(huì)減就可以了:
class Snow {
constructor (opt = {}) {
// ...
// 是否左右搖擺
this.isSwing = false
// 左右搖擺的步長(zhǎng)
this.stepSx = 0.03
// ...
}
// 隨機(jī)初始化屬性
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
}
// ...
}
}
復(fù)制代碼
除了上述這種方法,左右搖擺還有一種方式,就是使用正弦或余弦函數(shù),因?yàn)樗鼈兊那€翻轉(zhuǎn)90度就是左右搖擺:
img
我們使用正弦函數(shù),公式為:y=sin(x),x的值是弧度表示,只要一直增加就可以了,y的值用來(lái)修改雪花的水平方向的速度變化步長(zhǎng):
class Snow {
constructor (opt = {}) {
// ...
// 是否左右搖擺
this.isSwing = false
// 左右搖擺的正弦函數(shù)x變量
this.swingRadian = 0
// 左右搖擺的正弦x步長(zhǎng)
this.swingStep = 0.01
// ...
}
init (reset) {
// ...
this.swingStep = 0.01 * Math.random()
}
move () {
if (this.isSwing) {
this.swingRadian += this.swingStep
this.x += this.sx * Math.sin(this.swingRadian * Math.PI) * 0.2
} else {
this.x += this.sx
}
// ...
}
}
復(fù)制代碼
因?yàn)檎液瘮?shù)y的值是從1變化到-1,擺動(dòng)幅度太了,所以乘了個(gè)小數(shù)0.2縮小一點(diǎn),想要幅度小一點(diǎn),還有一個(gè)方法是不要使用整個(gè)正弦曲線,可以從中截取一個(gè)適合的區(qū)間大小,比如就讓x的值在0.9π到1.1π之前變化:
class Snow {
constructor (opt = {}) {
// ...
// 是否左右搖擺
this.isSwing = false
// 左右搖擺的正弦函數(shù)x變量
this.swingRadian = 1// 需要改成一個(gè)中間值
// 左右搖擺的正弦x步長(zhǎng)
this.swingStep = 0.01
// ...
}
init (reset) {
// ...
this.swingStep = 0.01 * Math.random()
this.swingRadian = Math.random() * (1.1 - 0.9) + 0.9// 也讓它隨機(jī)一下
}
move () {
if (this.isSwing) {
if (this.swingRadian > 1.1 || this.swingRadian < 0.9) {
this.swingStep = -this.swingStep
}
this.swingRadian += this.swingStep
this.x += this.sx * Math.sin(this.swingRadian * Math.PI)
} else {
this.x += this.sx
}
// ...
}
}
復(fù)制代碼
既然給水平加了曲線,垂直方向上是不是也可以改成非勻速呢?當(dāng)然可以,區(qū)別是速度得一直是正的,不然就要出現(xiàn)反自然現(xiàn)象了,改變速度曲線同樣可以使用正余弦,上面我們使用了0.9π到1.1π之間的正弦曲線,根據(jù)上圖可以發(fā)現(xiàn)對(duì)應(yīng)的余弦曲線都是負(fù)的,趨勢(shì)是先慢后快,所以可以利用這一段來(lái)改變垂直方向的速度:
move () {
if (this.isSwing) {
if (this.swingRadian > 1.1 || this.swingRadian < 0.9) {
this.swingStep = -this.swingStep
}
this.swingRadian += this.swingStep
this.x += this.sx * Math.sin(this.swingRadian * Math.PI)
this.y -= this.sy * Math.cos(this.swingRadian * Math.PI)// 因?yàn)樗俣榷际秦?fù)的,所以改成-
} else {
this.x += this.sx
this.y += this.sy
}
// ...
}
復(fù)制代碼
為了防止為頁(yè)面上原本層級(jí)更高的元素遮擋,給雪花的樣式加一個(gè)很大的層級(jí):
render () {
this.el = document.createElement('div')
this.el.style.cssText = `
// ...
z-index: 9999999999999;
`
document.body.appendChild(this.el)
}
復(fù)制代碼
修改了層級(jí),所以雪花會(huì)在頁(yè)面的最上層,那么可能會(huì)擋住其他元素的鼠標(biāo)事件,需要禁止它響應(yīng)鼠標(biāo)事件:
render () {
this.el = document.createElement('div')
this.el.style.cssText = `
// ...
pointer-events: none;
`
document.body.appendChild(this.el)
}
復(fù)制代碼
使用性能更好的transform屬性來(lái)做動(dòng)畫:
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)
}
復(fù)制代碼
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)`
}
復(fù)制代碼
當(dāng)然,最好的方式是用canvas來(lái)畫。
最終效果:
下完雪,接下來(lái)順便下個(gè)雨,雨和雪差不多,都是從天上掉下來(lái),但是雨的速度更快,通常也不會(huì)左右搖擺什么的,方向也基本是一致的,先來(lái)修改一下樣式:
setStyle () {
this.el.style.cssText = `
// ...
width: 1px;
// ...
`
}
復(fù)制代碼
很簡(jiǎn)單,只要把寬度寫死為1就行了:
接下來(lái)把搖擺去掉:
move () {
this.x += this.sx
this.y += this.sy
// ...
}
復(fù)制代碼
效果如下:
可以發(fā)現(xiàn)雨是豎著在水平移動(dòng),顯然是不行的,需要讓它傾斜一定的角度,和運(yùn)動(dòng)方向保持一致,這個(gè)也很簡(jiǎn)單,算一下斜率,水平速度除以垂直速度:
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)`
}
復(fù)制代碼
因?yàn)?span style="color: #E96900; --tt-darkmode-color: #E96900;">tan(θ)=sy/sx,θ=Math.atan(sy / sx),因?yàn)橛甑木€段默認(rèn)是從上到下垂直的,θ是代表和水平方向上的夾角,所以需要先旋轉(zhuǎn)90度,再旋轉(zhuǎn)夾角的度數(shù),最后弧度轉(zhuǎn)角度的公式為:角度=弧度*(180/π)。
雨和雪都實(shí)現(xiàn)了,讓它們一起出來(lái),就是雨夾雪了:
把上面的代碼放到網(wǎng)站上就有下雪的效果了,另外也可以使用天氣廠商的api,根據(jù)實(shí)時(shí)天氣來(lái)下雪或者下雨,再實(shí)現(xiàn)一下太陽(yáng)、烏云等效果,一個(gè)沉浸式天氣就完成了,有興趣的可自行實(shí)踐。
完整代碼
https://github.com/wanglin2/snow
源自:juejin.cn/post/6910857740327845901
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。