canvas下雪动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas下雪动画效果代码

代码标签: canvas 下雪 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

 
  
<style>
body {
	background: linear-gradient(0deg, rgba(7,37,48,1) 0%, rgba(0,0,0,1) 100%);
	height: 100svh;
}

.snowverlay {
/* 	Position depending how you want it to behave on scroll */
	position: absolute;  
/* 	position: fixed; */
	
	top: 0;
	left: 0;
	width: 100%;
	pointer-events: none;
}
</style>

  
</head>

<body translate="no">
  <canvas class="snowverlay"></canvas>

  
      <script  >
const CANVAS_HEIGHT = 0.3;
const SNOWFLAKE_AMOUNT = 50;
const SNOWFLAKE_SIZE = {
  min: 1,
  max: 4 };

const SNOWFLAKE_SPEED = {
  min: 0.2,
  max: 1.2 };

const CANVAS_SELECTOR = ".snowverlay";

let animationFrame;

// Shared utilities
const setupCanvas = () => {
  const canvas = document.querySelector(CANVAS_SELECTOR);
  const ctx = canvas.getContext("2d");
  if (!ctx) {
    return null;
  }

  const setCanvasSize = () => {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight * CANVAS_HEIGHT;
  };

  setCanvasSize();
  window.addEventListener("resize", setCanvasSize);

  return { canvas, ctx };
};

const createSnowflake = (canvas, isAnimated = true, index = 0) => ({
  x: Math.random() * canvas.width,
  y: isAnimated ?
  -20 - index * canvas.height / SNOWFLAKE_AMOUNT :
  Math.random() * canvas.height,
  size:
  Math.random() * (SNOWFLAKE_SIZE.max - SNOWFLAKE_SIZE.min) +
  SNOWFLAKE_SIZE.min,
  speed:
  Math.random() * (SNOWFLAKE_SPEED.max - SNOWFLAKE_SPEED.min) +
  SNOWFLAKE_SPEED.min,
  opacity: isAnimated ? null : Math.random() * 0.5 + 0.2 });


const drawSnowflake = (ctx, flake, canv.........完整代码请登录后点击上方下载按钮下载查看

网友评论0