forked from teuchezh/dynamic-weather-card
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththunderstorm.ts
More file actions
62 lines (52 loc) · 1.91 KB
/
thunderstorm.ts
File metadata and controls
62 lines (52 loc) · 1.91 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
import { BaseAnimation } from './base';
import { RainyAnimation } from './rainy';
import { TimeOfDay } from '../types';
/**
* Thunderstorm weather animation
*/
export class ThunderstormAnimation extends BaseAnimation {
private rainyAnimation: RainyAnimation;
constructor(ctx: CanvasRenderingContext2D) {
super(ctx);
this.rainyAnimation = new RainyAnimation(ctx);
}
/**
* Draw thunderstorm weather
* @param time - Animation time (unused, for interface compatibility)
* @param width - Canvas width
* @param height - Canvas height
* @param timeOfDay - Time of day info
* @param withRain - Include rain flag
*/
draw(time: number, width: number, height: number, timeOfDay: TimeOfDay, withRain: boolean = true): void {
const currentTime = Date.now() * 0.001;
// Dark clouds
this.drawClouds(currentTime, width, height, 1.0);
// Rain if specified
if (withRain) {
this.rainyAnimation.draw(time, width, height, timeOfDay, false);
}
// Lightning flash effect
this.drawLightning(width, height, currentTime);
}
/**
* Draw lightning flash effect
* @param width - Canvas width
* @param height - Canvas height
* @param time - Animation time
*/
private drawLightning(width: number, height: number, time: number): void {
// Create unpredictable flash pattern
const flashPattern = Math.sin(time * 2.5) * Math.sin(time * 5.3) * Math.sin(time * 7.1);
const flashIntensity = Math.max(0, flashPattern);
// Flashes occur less frequently and more sharply
if (flashIntensity > 0.4) {
const normalizedIntensity = (flashIntensity - 0.4) / 0.6;
const alpha = normalizedIntensity * 0.6;
// Smooth fade for realistic effect
const fadeAlpha = Math.min(alpha, Math.sin(normalizedIntensity * Math.PI) * 0.6);
this.ctx.fillStyle = `rgba(255, 255, 255, ${fadeAlpha})`;
this.ctx.fillRect(0, 0, width, height);
}
}
}