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

相关文章

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是