当前位置:首页 > JavaScript

js实现闪电

2026-03-14 08:22:11JavaScript

使用Canvas绘制闪电效果

通过HTML5的Canvas API可以动态生成闪电效果。以下是一个简单的实现方法:

js实现闪电

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

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

    const startX = Math.random() * canvas.width;
    const startY = 0;
    ctx.moveTo(startX, startY);

    let currentX = startX;
    let currentY = startY;

    while (currentY < canvas.height) {
        const nextX = currentX + (Math.random() - 0.5) * 30;
        const nextY = currentY + Math.random() * 20 + 10;
        ctx.lineTo(nextX, nextY);
        currentX = nextX;
        currentY = nextY;
    }

    ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)';
    ctx.lineWidth = 2;
    ctx.stroke();

    // 添加闪光效果
    ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
}

setInterval(drawLightning, 300);

使用CSS动画创建闪电效果

纯CSS也可以模拟闪电效果,适合简单的视觉展示:

js实现闪电

<style>
.lightning {
    position: absolute;
    width: 2px;
    height: 100%;
    background: white;
    left: 50%;
    box-shadow: 0 0 10px 2px white;
    opacity: 0;
    animation: lightning 4s infinite;
}

@keyframes lightning {
    0% { opacity: 0; }
    1% { opacity: 0.8; }
    2% { opacity: 0; }
    8% { opacity: 0; }
    9% { opacity: 0.5; }
    10% { opacity: 0; }
    100% { opacity: 0; }
}
</style>

<div class="lightning"></div>

使用Three.js创建3D闪电

对于更复杂的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();

function createLightning() {
    const points = [];
    points.push(new THREE.Vector3(0, 10, 0));

    for (let i = 1; i < 10; i++) {
        points.push(new THREE.Vector3(
            points[i-1].x + (Math.random() - 0.5) * 2,
            points[i-1].y - Math.random() * 3,
            0
        ));
    }

    const geometry = new THREE.BufferGeometry().setFromPoints(points);
    const material = new THREE.LineBasicMaterial({ color: 0x00ffff });
    const lightning = new THREE.Line(geometry, material);
    scene.add(lightning);

    setTimeout(() => scene.remove(lightning), 100);
}

setInterval(createLightning, 1000);

优化闪电效果

为了更真实的闪电效果,可以考虑以下优化:

  • 添加分支闪电
  • 实现渐变消失效果
  • 添加环境光变化
  • 使用粒子系统增强视觉效果

每种方法适用于不同场景,Canvas适合2D游戏,CSS适合简单页面效果,Three.js适合3D场景。根据需求选择合适方案。

标签: 闪电js
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的J…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js实现轮播代码

js实现轮播代码

基础轮播实现 使用HTML、CSS和JavaScript创建一个简单的轮播效果。HTML部分定义轮播容器和图片元素。 <div class="carousel"> <div c…