使用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

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker