使用js和canvas实现下雪效果

2023-12-22 09:52

本文主要是介绍使用js和canvas实现下雪效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求:

实现较为逼真的下雪效果

实现:

<!-- snow 样式 -->

<style>

body {

display: flex;

justify-content: center;

align-items: center;

gap: 1em;

min-height: 100vh;

background-image: radial-gradient(circle at 50% 100%, #1b1b35, #121225);

font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,

"Helvetica Neue", "Open Sans", system-ui, sans-serif,

"Apple Color Emoji", "Segoe UI Emoji";

}

.canvas {

position: absolute;

inset: 0;

pointer-events: none;

}

.rotate-container {

display: inline-block;

position: absolute;

z-index: 17;

width: 10rem;

height: 10rem;

left: 50%;

top: 5.8rem;

animation: rotate-container 17s linear infinite;

transform: translateX(-50%);

}

.ball-img {

width: 100%;

height: 100%;

}

.title-img{

display: inline-block;

position: absolute;

z-index: 17;

width: 6rem;

/* height: 2rem; */

left: 50%;

top: 10rem;

transform: translateX(-50%);

}

/* @keyframes rotate-container {

0% {

transform: translateX(-50%) rotate(0deg) scale(1);

}

25% {

transform: translateX(-50%) rotate(90deg) scale(0.8);

}

50% {

transform: translateX(-50%) rotate(180deg) scale(1);

}

75% {

transform: translateX(-50%) rotate(270deg) scale(0.8);

}

100% {

transform: translateX(-50%) rotate(360deg) scale(1);

}

} */



 

@keyframes rotate-container {

0% {

transform: translateX(-50%) rotate(0deg) scale(1);

}

25% {

transform: translateX(-50%) rotate(90deg) scale(1);

}

50% {

transform: translateX(-50%) rotate(180deg) scale(1);

}

75% {

transform: translateX(-50%) rotate(270deg) scale(1);

}

100% {

transform: translateX(-50%) rotate(360deg) scale(1);

}

}

.T9TB07{

z-index: 17;

}

.zlfwHK{

z-index: 17;

}

<body style="background: #fff;">

<div id="app" id="app" style="background-color: #333;

overflow: hidden;">

<!-- canvas 雪花 -->

<canvas class="canvas" style="display: flex;

z-index: 18;

position: fixed;

"></canvas>

</div>

<!-- 雪花 代码 -->

<script>

const canvas = document.querySelector(".canvas");

const ctx = canvas.getContext("2d");

const pixelRatio = window.devicePixelRatio || 1;

const snowflakes = [];

class Snowflake {

constructor() {

this.x = Math.random() * canvas.width;

this.y = Math.random() * canvas.height;

const maxSize = 3;

this.size = Math.random() * (maxSize - 1) + 1;

this.velocity = this.size * 0.35;

const opacity = this.size / maxSize;

this.fill = `rgb(255 255 255 / ${opacity})`;

this.windSpeed = (Math.random() - 0.5) * 0.1;

this.windAngle = Math.random() * Math.PI * 2;

}

isOutsideCanvas() {

return this.y > canvas.height + this.size;

}

reset() {

this.x = Math.random() * canvas.width;

this.y = -this.size;

}

update() {

this.windAngle += this.windSpeed;

this.wind = Math.cos(this.windAngle) * 0.5;

this.x += this.wind;

this.y += this.velocity;

if (this.isOutsideCanvas()) {

this.reset();

}

}

draw() {

ctx.beginPath();

ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);

ctx.fillStyle = this.fill;

ctx.fill();

ctx.closePath();

}

}

const createSnowflakes = () => {

// snowflakeCount = Math.floor(

// (window.innerWidth * window.innerHeight) / 1400

// );

snowflakeCount = Math.floor(

(1920*2000) / 1400

);

console.log(snowflakeCount,'snowflakeCount')

for (let i = 0; i < snowflakeCount; i++) {

snowflakes.push(new Snowflake());

}

};

console.log(window.innerWidth,'window.innerWidth-')

console.log(window.innerHeight,'window.innerHeight-')

// 调整canvas大小

const resizeCanvas = () => {

const width = window.innerWidth;

console.log(width,'window.innerWidth')

const height = window.innerHeight;

console.log(pixelRatio,'pixelRatio')

canvas.width = width * pixelRatio;

console.log(canvas.width,'canvas.width')

canvas.height = height * pixelRatio;


 

canvas.style.width = `${width}px`;

canvas.style.height = `${height}px`;

// 修改

// canvas.style.width = `100%`;

// canvas.style.height = `100%`;

ctx.scale(pixelRatio, pixelRatio);

snowflakes.length = 0;

createSnowflakes();

};

window.addEventListener("resize", resizeCanvas);

resizeCanvas();

const render = () => {

requestAnimationFrame(render);

ctx.clearRect(0, 0, canvas.width, canvas.height);

snowflakes.forEach((snowflake) => {

snowflake.update();

snowflake.draw();

});

};

render();

</script>

</body>

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /><!-- snow 样式 --><style>body {display: flex;justify-content: center;align-items: center;gap: 1em;min-height: 100vh;background-image: radial-gradient(circle at 50% 100%, #1b1b35, #121225);font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,"Helvetica Neue", "Open Sans", system-ui, sans-serif,"Apple Color Emoji", "Segoe UI Emoji";}.canvas {position: absolute;inset: 0;pointer-events: none;}.rotate-container {display: inline-block;position: absolute;z-index: 17;width: 10rem;height: 10rem;left: 50%;top: 5.8rem;animation: rotate-container 17s linear infinite;transform: translateX(-50%);}.ball-img {width: 100%;height: 100%;}.title-img{display: inline-block;position: absolute;z-index: 17;width: 6rem;/* height: 2rem; */left: 50%;top: 10rem;transform: translateX(-50%);}@keyframes rotate-container {0% {transform: translateX(-50%) rotate(0deg) scale(1);}25% {transform: translateX(-50%) rotate(90deg) scale(1);}50% {transform: translateX(-50%) rotate(180deg) scale(1);}75% {transform: translateX(-50%) rotate(270deg) scale(1);}100% {transform: translateX(-50%) rotate(360deg) scale(1);}}.T9TB07{z-index: 17;}.zlfwHK{z-index: 17;}</style></head><body style="background: #fff;"><div id="app" id="app" style="background-color: #333;overflow: hidden;"><!-- canvas 雪花 --><canvas class="canvas" style="display: flex;z-index: 18;position: fixed;"></canvas></div><!-- 雪花 代码 --><script>const canvas = document.querySelector(".canvas");const ctx = canvas.getContext("2d");const pixelRatio = window.devicePixelRatio || 1;const snowflakes = [];class Snowflake {constructor() {this.x = Math.random() * canvas.width;this.y = Math.random() * canvas.height;const maxSize = 3;this.size = Math.random() * (maxSize - 1) + 1;this.velocity = this.size * 0.35;const opacity = this.size / maxSize;this.fill = `rgb(255 255 255 / ${opacity})`;this.windSpeed = (Math.random() - 0.5) * 0.1;this.windAngle = Math.random() * Math.PI * 2;}isOutsideCanvas() {return this.y > canvas.height + this.size;}reset() {this.x = Math.random() * canvas.width;this.y = -this.size;}update() {this.windAngle += this.windSpeed;this.wind = Math.cos(this.windAngle) * 0.5;this.x += this.wind;this.y += this.velocity;if (this.isOutsideCanvas()) {this.reset();}}draw() {ctx.beginPath();ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);ctx.fillStyle = this.fill;ctx.fill();ctx.closePath();}}const createSnowflakes = () => {// snowflakeCount = Math.floor(//   (window.innerWidth * window.innerHeight) / 1400// );snowflakeCount = Math.floor((1920*2000) / 1400);console.log(snowflakeCount,'snowflakeCount')for (let i = 0; i < snowflakeCount; i++) {snowflakes.push(new Snowflake());}};console.log(window.innerWidth,'window.innerWidth-')console.log(window.innerHeight,'window.innerHeight-')// 调整canvas大小const resizeCanvas = () => {const width = window.innerWidth;console.log(width,'window.innerWidth')const height = window.innerHeight;console.log(pixelRatio,'pixelRatio')canvas.width = width * pixelRatio;console.log(canvas.width,'canvas.width')canvas.height = height * pixelRatio;canvas.style.width = `${width}px`;canvas.style.height = `${height}px`;// 修改// canvas.style.width = `100%`;// canvas.style.height = `100%`;ctx.scale(pixelRatio, pixelRatio);snowflakes.length = 0;createSnowflakes();};window.addEventListener("resize", resizeCanvas);resizeCanvas();const render = () => {requestAnimationFrame(render);ctx.clearRect(0, 0, canvas.width, canvas.height);snowflakes.forEach((snowflake) => {snowflake.update();snowflake.draw();});};render();</script></body></html>

实现效果:

录屏2023-12-21 11.26.28

这篇关于使用js和canvas实现下雪效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/523524

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入