当前位置:首页 > JavaScript

js实现灯光秀

2026-02-02 19:14:59JavaScript

使用JavaScript实现灯光秀效果

通过Canvas绘制动态灯光效果,结合随机颜色生成和动画循环实现视觉冲击力。以下代码示例展示基础实现方式:

const canvas = document.getElementById('lightShow');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const lights = [];
for (let i = 0; i < 100; i++) {
  lights.push({
    x: Math.random() * canvas.width,
    y: Math.random() * canvas.height,
    radius: Math.random() * 10 + 5,
    color: `hsl(${Math.random() * 360}, 100%, 50%)`,
    speedX: Math.random() * 4 - 2,
    speedY: Math.random() * 4 - 2
  });
}

function animate() {
  ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  lights.forEach(light => {
    ctx.beginPath();
    ctx.arc(light.x, light.y, light.radius, 0, Math.PI * 2);
    ctx.fillStyle = light.color;
    ctx.fill();

    light.x += light.speedX;
    light.y += light.speedY;

    if (light.x < 0 || light.x > canvas.width) light.speedX *= -1;
    if (light.y < 0 || light.y > canvas.height) light.speedY *= -1;
  });

  requestAnimationFrame(animate);
}
animate();

添加交互式灯光控制

通过事件监听实现用户交互效果,灯光会跟随鼠标移动产生波纹:

let mouseX = 0;
let mouseY = 0;

window.addEventListener('mousemove', (e) => {
  mouseX = e.clientX;
  mouseY = e.clientY;
  lights.push({
    x: mouseX,
    y: mouseY,
    radius: 15,
    color: `hsl(${Math.random() * 360}, 100%, 50%)`,
    speedX: Math.random() * 6 - 3,
    speedY: Math.random() * 6 - 3,
    life: 100
  });
});

function animate() {
  // ...原有动画代码...

  lights.forEach((light, index) => {
    if (light.life) {
      light.life--;
      light.radius *= 0.95;
      if (light.life <= 0) {
        lights.splice(index, 1);
      }
    }
  });
}

使用WebGL实现高级效果

对于更复杂的3D灯光效果,建议使用Three.js库:

import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const particles = 5000;
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(particles * 3);
const colors = new Float32Array(particles * 3);

for (let i = 0; i < positions.length; i += 3) {
  positions[i] = (Math.random() - 0.5) * 200;
  positions[i+1] = (Math.random() - 0.5) * 200;
  positions[i+2] = (Math.random() - 0.5) * 200;

  colors[i] = Math.random();
  colors[i+1] = Math.random();
  colors[i+2] = Math.random();
}

geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));

const material = new THREE.PointsMaterial({
  size: 1,
  vertexColors: true,
  transparent: true,
  opacity: 0.8
});

const particleSystem = new THREE.Points(geometry, material);
scene.add(particleSystem);
camera.position.z = 100;

function animate() {
  requestAnimationFrame(animate);
  particleSystem.rotation.x += 0.001;
  particleSystem.rotation.y += 0.002;
  renderer.render(scene, camera);
}
animate();

性能优化建议

限制帧率避免过度消耗资源:

let fps = 30;
let now;
let then = Date.now();
const interval = 1000/fps;

function animate() {
  requestAnimationFrame(animate);
  now = Date.now();
  const delta = now - then;

  if (delta > interval) {
    then = now - (delta % interval);
    // 绘制代码放在这里
  }
}

使用离屏Canvas预渲染静态元素:

js实现灯光秀

const bufferCanvas = document.createElement('canvas');
bufferCanvas.width = canvas.width;
bufferCanvas.height = canvas.height;
const bufferCtx = bufferCanvas.getContext('2d');

// 预渲染背景等不变元素
bufferCtx.fillStyle = 'black';
bufferCtx.fillRect(0, 0, bufferCanvas.width, bufferCanvas.height);

// 在主Canvas中复用
ctx.drawImage(bufferCanvas, 0, 0);

标签: 灯光js
分享给朋友:

相关文章

vue实现点亮灯光

vue实现点亮灯光

Vue 实现点亮灯光效果 在 Vue 中实现点亮灯光效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画和 Vue 数据绑定 通过 Vue 的数据绑定控制 CSS 类名,实现灯光开关…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js实现密码

js实现密码

密码强度验证 使用正则表达式验证密码强度是一种常见方法。以下代码检查密码是否包含大小写字母、数字和特殊字符,且长度至少为8位: function checkPasswordStrength(pass…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…