当前位置:首页 > JavaScript

js实现星星闪烁的效果

2026-04-05 01:05:41JavaScript

实现星星闪烁效果的方法

使用CSS动画结合JavaScript

通过CSS定义闪烁动画,JavaScript动态添加星星元素并应用动画。

js实现星星闪烁的效果

.star {
  position: absolute;
  background-color: white;
  border-radius: 50%;
  animation: twinkle 1s infinite alternate;
}

@keyframes twinkle {
  0% { opacity: 0.2; transform: scale(0.8); }
  100% { opacity: 1; transform: scale(1.2); }
}
function createStars() {
  const container = document.getElementById('star-container');
  const starCount = 100;

  for (let i = 0; i < starCount; i++) {
    const star = document.createElement('div');
    star.className = 'star';

    // 随机位置和大小
    const size = Math.random() * 3 + 1;
    star.style.width = `${size}px`;
    star.style.height = `${size}px`;
    star.style.left = `${Math.random() * 100}%`;
    star.style.top = `${Math.random() * 100}%`;

    // 随机动画延迟
    star.style.animationDelay = `${Math.random() * 2}s`;

    container.appendChild(star);
  }
}

window.onload = createStars;

使用Canvas绘制闪烁星星

通过Canvas实现更灵活的星星效果,包括不同形状和大小的星星。

js实现星星闪烁的效果

const canvas = document.getElementById('star-canvas');
const ctx = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const stars = Array(200).fill().map(() => ({
  x: Math.random() * canvas.width,
  y: Math.random() * canvas.height,
  size: Math.random() * 3 + 1,
  brightness: Math.random(),
  speed: Math.random() * 0.05
}));

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  stars.forEach(star => {
    star.brightness += star.speed;
    if (star.brightness > 1 || star.brightness < 0) {
      star.speed = -star.speed;
    }

    ctx.globalAlpha = star.brightness;
    ctx.fillStyle = 'white';
    ctx.beginPath();
    ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2);
    ctx.fill();
  });

  requestAnimationFrame(animate);
}

animate();

使用GSAP动画库

利用GSAP库实现更复杂的闪烁效果和动画控制。

import { gsap } from 'gsap';

function createGsapStars() {
  const container = document.getElementById('star-container');
  const stars = Array(50).fill().map(() => {
    const star = document.createElement('div');
    star.className = 'star';
    Object.assign(star.style, {
      position: 'absolute',
      width: `${Math.random() * 4 + 1}px`,
      height: `${Math.random() * 4 + 1}px`,
      left: `${Math.random() * 100}%`,
      top: `${Math.random() * 100}%`,
      backgroundColor: 'white',
      borderRadius: '50%'
    });
    container.appendChild(star);
    return star;
  });

  stars.forEach(star => {
    gsap.to(star, {
      opacity: 0,
      scale: 0.5,
      duration: Math.random() * 1 + 0.5,
      repeat: -1,
      yoyo: true,
      ease: 'power1.inOut'
    });
  });
}

实现不同形状的星星

创建星形而非圆形的闪烁效果。

function createStarShape(x, y, spikes, outerRadius, innerRadius) {
  let rot = Math.PI/2 * 3;
  let step = Math.PI / spikes;

  ctx.beginPath();
  ctx.moveTo(x, y - outerRadius);

  for(let i = 0; i < spikes; i++) {
    const x1 = x + Math.cos(rot) * outerRadius;
    const y1 = y + Math.sin(rot) * outerRadius;
    ctx.lineTo(x1, y1);
    rot += step;

    const x2 = x + Math.cos(rot) * innerRadius;
    const y2 = y + Math.sin(rot) * innerRadius;
    ctx.lineTo(x2, y2);
    rot += step;
  }

  ctx.lineTo(x, y - outerRadius);
  ctx.closePath();
}

// 在Canvas动画循环中使用
stars.forEach(star => {
  ctx.globalAlpha = star.brightness;
  ctx.fillStyle = 'white';
  createStarShape(star.x, star.y, 5, star.size, star.size * 0.4);
  ctx.fill();
});

每种方法适用于不同场景:CSS动画适合简单实现,Canvas提供更高自由度,GSAP适合复杂动画控制,自定义形状可创造独特视觉效果。

标签: 星星效果
分享给朋友:

相关文章

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

vue实现旋转效果

vue实现旋转效果

使用CSS动画实现旋转 在Vue中可以通过CSS的@keyframes或transform属性实现旋转效果。在组件的<style>部分定义旋转动画: .rotate { animat…

vue实现翻牌效果

vue实现翻牌效果

实现翻牌效果的基本思路 翻牌效果通常指卡片正面和背面的翻转动画,可以通过CSS 3D变换和Vue的过渡系统实现。核心是利用transform-style: preserve-3d和rotateY旋转完…

vue实现聊天效果

vue实现聊天效果

Vue 实现聊天效果 在 Vue 中实现聊天效果,可以通过组件化设计、数据绑定和事件处理来完成。以下是关键步骤和代码示例。 数据结构和状态管理 使用 Vue 的 data 或 ref(Composi…

vue实现导航栏效果

vue实现导航栏效果

Vue 实现导航栏效果的方法 基础导航栏结构 使用 Vue 的模板语法构建导航栏的基本 HTML 结构,通常包含 <nav> 和 <ul> 元素。通过 v-for 动态渲染导航…

vue实现滚动日志效果

vue实现滚动日志效果

实现滚动日志效果的基本思路 滚动日志效果通常指动态添加日志内容并自动滚动到底部,常见于实时日志监控或聊天界面。Vue中可通过以下方法实现: 使用v-for渲染日志列表 在Vue模板中使用v-for循…