-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththunder.html
More file actions
161 lines (132 loc) · 4.36 KB
/
Copy paththunder.html
File metadata and controls
161 lines (132 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Realistic Plasma Discharge</title>
<style>
body { margin: 0; background-color: #020205; overflow: hidden; cursor: crosshair; }
canvas { display: block; filter: contrast(1.2) brightness(1.2); }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let width, height;
let bolts = [];
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
class Lightning {
constructor(x, y, isBranch = false) {
this.segments = [];
this.x = x;
this.y = 0; // Starts from sky
this.tx = x + (Math.random() - 0.5) * 100; // Target X
this.ty = y; // Target Y (click point)
this.life = 20 + Math.random() * 20;
this.opacity = 1;
this.isBranch = isBranch;
// The "flicker" pattern (1 = on, 0 = off)
// This mimics the 'return strokes' of real lightning
this.flicker = [1, 0, 1, 1, 0, 1, 0, 0, 1];
this.flickerStep = 0;
this.generatePath();
}
generatePath() {
let curX = this.x;
let curY = this.y;
// How many segments to draw
const segmentsCount = 30;
const dy = this.ty / segmentsCount;
for (let i = 0; i < segmentsCount; i++) {
let nextY = curY + dy;
// Add horizontal jitter
let nextX = curX + (Math.random() - 0.5) * 60;
// Pull the bolt toward the target click
let pull = (this.tx - nextX) * 0.1;
nextX += pull;
this.segments.push({x1: curX, y1: curY, x2: nextX, y2: nextY});
// Random branching
if (!this.isBranch && Math.random() > 0.92 && i < segmentsCount - 5) {
let branch = new Lightning(nextX, nextY, true);
branch.ty = nextY + (Math.random() * 200);
branch.tx = nextX + (Math.random() - 0.5) * 400;
bolts.push(branch);
}
curX = nextX;
curY = nextY;
}
}
draw() {
if (this.flickerStep < this.flicker.length) {
if (this.flicker[this.flickerStep] === 0) {
this.flickerStep++;
return;
}
}
this.flickerStep++;
ctx.save();
// Lighter composite makes overlapping glows "add up" to white
ctx.globalCompositeOperation = 'lighter';
// 1. The Deep Blue Aura
ctx.beginPath();
ctx.strokeStyle = `rgba(60, 100, 255, ${this.opacity * 0.3})`;
ctx.lineWidth = this.isBranch ? 2 : 8;
ctx.shadowBlur = 30;
ctx.shadowColor = "#05f";
this.renderSegments();
ctx.stroke();
// 2. The Cyan Ionization
ctx.beginPath();
ctx.strokeStyle = `rgba(150, 255, 255, ${this.opacity * 0.6})`;
ctx.lineWidth = this.isBranch ? 1 : 3;
ctx.shadowBlur = 10;
ctx.shadowColor = "#0ff";
this.renderSegments();
ctx.stroke();
// 3. The White-Hot Core
ctx.beginPath();
ctx.strokeStyle = `rgba(255, 255, 255, ${this.opacity})`;
ctx.lineWidth = this.isBranch ? 0.5 : 1.5;
this.renderSegments();
ctx.stroke();
ctx.restore();
this.opacity -= 0.04;
}
renderSegments() {
this.segments.forEach(s => {
ctx.moveTo(s.x1, s.y1);
ctx.lineTo(s.x2, s.y2);
});
}
}
let flash = 0;
window.addEventListener('mousedown', (e) => {
bolts.push(new Lightning(e.clientX, e.clientY));
flash = 0.5; // Massive initial flash
});
function animate() {
// Keep the background slightly translucent for motion trails
ctx.fillStyle = 'rgba(5, 5, 15, 0.2)';
ctx.fillRect(0, 0, width, height);
// Screen Flash logic (The whole sky lights up)
if (flash > 0) {
ctx.fillStyle = `rgba(180, 200, 255, ${flash})`;
ctx.fillRect(0, 0, width, height);
flash *= 0.85; // Fast decay
}
for (let i = bolts.length - 1; i >= 0; i--) {
bolts[i].draw();
if (bolts[i].opacity <= 0) bolts.splice(i, 1);
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>