canvas实现文字变成沙粒交互动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas实现文字变成沙粒交互动画效果代码

代码标签: canvas 文字 变成 沙粒 交互 动画

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

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

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

  <link href="https://fonts.googleapis.com/css?family=Medula+One" rel="stylesheet">
  
  
  
<style>
body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
    overflow: hidden;
}

.container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

h1 {
    font-size: 8rem;
    text-align: center;
    color: #333;
    text-transform: uppercase;
    position: absolute;
    z-index: 0;
    transition: opacity 0.5s ease;
    font-weight: 900;
    padding: 1.5rem 0;
}

canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
}

.hidden {
    opacity: 0;
}
</style>


  
  
</head>

<body>
    <div class="container">
        <h1 id="text">BFW.WIKI</h1>
        <canvas id="canvas"></canvas>
    </div>

      <script  >
document.addEventListener('DOMContentLoaded', () => {
  // Get elements
  const text = document.getElementById('text');
  const canvas = document.getElementById('canvas');
  const ctx = canvas.getContext('2d', { alpha: true });

  // Set canvas dimensions
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;

  // Configuration options
  const config = {
    particleDensity: 3, // Lower = more particles
    particleSizeMultiplier: 1 / 20, // Adjust particle size relative to font
    gravity: 0.15,
    friction: 0.95,
    bounce: 0.6,
    returnSpeed: 0.05,
    colorVariation: 15, // Random color variation
    initialCycleDelay: 2000, // Initial delay before animation starts
    fallingDuration: 5000, // How long particles fall
    returnDuration: 800, // How long particles return
    restDuration: 2000, // Rest time between cycles
    canInteract: true, // Allow interaction with the mouse
    interactiveForce: 5, // Force applied by mouse inte.........完整代码请登录后点击上方下载按钮下载查看

网友评论0