-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsnowy.ts
More file actions
150 lines (128 loc) · 4.54 KB
/
Copy pathsnowy.ts
File metadata and controls
150 lines (128 loc) · 4.54 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
import { BaseAnimation } from './base';
import { TimeOfDay } from '../types';
interface Snowflake {
x: number;
y: number;
speedY: number;
speedX: number;
size: number;
alpha: number;
rotation: number;
rotationSpeed: number;
swayPhase: number;
swaySpeed: number;
}
/**
* Snowy weather animation
*/
export class SnowyAnimation extends BaseAnimation {
private snowflakes: Snowflake[] = [];
private lastTime: number = 0;
/**
* Draw snowy weather
* @param time - Animation time (unused, for interface compatibility)
* @param width - Canvas width
* @param height - Canvas height
* @param timeOfDay - Time of day info
*/
draw(time: number, width: number, height: number, _timeOfDay: TimeOfDay): void {
const currentTime = Date.now() * 0.001;
this.drawClouds(currentTime, width, height, 0.7);
this.drawSnowflakes(width, height);
}
/**
* Draw snowflakes
* @param width - Canvas width
* @param height - Canvas height
*/
private drawSnowflakes(width: number, height: number): void {
// Calculate snowflake count based on area
const snowflakeCount = Math.floor((width * height) / 5000);
const targetCount = Math.max(30, Math.min(snowflakeCount, 80));
// Initialize or adjust snowflakes
if (this.snowflakes.length !== targetCount) {
this.snowflakes = [];
for (let i = 0; i < targetCount; i++) {
this.snowflakes.push({
x: Math.random() * width,
y: Math.random() * height - Math.random() * 100,
speedY: 15 + Math.random() * 10,
speedX: (Math.random() - 0.5) * 8,
size: 1.5 + Math.random() * 1.5,
alpha: 0.6 + Math.random() * 0.3,
rotation: Math.random() * Math.PI * 2,
rotationSpeed: (Math.random() - 0.5) * 0.3,
swayPhase: Math.random() * Math.PI * 2,
swaySpeed: 0.5 + Math.random() * 0.5
});
}
}
// Calculate real delta time for smooth animation
const currentTime = Date.now() * 0.001;
const deltaTime = this.lastTime > 0 ? Math.min(currentTime - this.lastTime, 0.1) : 1 / 60;
this.lastTime = currentTime;
const currentAnimTime = currentTime;
this.ctx.lineCap = 'round';
for (let i = 0; i < this.snowflakes.length; i++) {
const flake = this.snowflakes[i];
// Update position with gentle swaying
const sway = Math.sin(currentAnimTime * flake.swaySpeed + flake.swayPhase) * 2;
flake.y += flake.speedY * deltaTime;
flake.x += (flake.speedX + sway) * deltaTime;
flake.rotation += flake.rotationSpeed * deltaTime;
// Reset when off screen (smooth loop)
if (flake.y > height + 20) {
flake.y = -20 - Math.random() * 50;
flake.x = Math.random() * width;
}
// Wrap horizontally
if (flake.x < -10) {
flake.x = width + 10;
} else if (flake.x > width + 10) {
flake.x = -10;
}
this.drawSnowflake(flake.x, flake.y, flake.size, flake.alpha, flake.rotation);
}
}
/**
* Draw a single snowflake
* @param x - X position
* @param y - Y position
* @param size - Snowflake size
* @param alpha - Opacity
* @param rotation - Rotation angle
*/
private drawSnowflake(x: number, y: number, size: number, alpha: number, rotation: number): void {
this.ctx.save();
this.ctx.translate(x, y);
this.ctx.rotate(rotation);
this.ctx.strokeStyle = `rgba(255, 255, 255, ${alpha})`;
this.ctx.lineWidth = 1;
// Optimize: draw all branches in a single path
this.ctx.beginPath();
// Draw 6-pointed snowflake
for (let j = 0; j < 6; j++) {
const angle = (Math.PI / 3) * j;
const cos = Math.cos(angle);
const sin = Math.sin(angle);
// Main branch
this.ctx.moveTo(0, 0);
this.ctx.lineTo(sin * size * 2.5, cos * size * 2.5);
// Side branches (rotated coordinates)
const branch1X = sin * size * 1.5 + cos * size * 0.5;
const branch1Y = cos * size * 1.5 - sin * size * 0.5;
const branch1EndX = sin * size * 1.8 + cos * size * 1.2;
const branch1EndY = cos * size * 1.8 - sin * size * 1.2;
this.ctx.moveTo(branch1X, branch1Y);
this.ctx.lineTo(branch1EndX, branch1EndY);
const branch2X = sin * size * 1.5 - cos * size * 0.5;
const branch2Y = cos * size * 1.5 + sin * size * 0.5;
const branch2EndX = sin * size * 1.8 - cos * size * 1.2;
const branch2EndY = cos * size * 1.8 + sin * size * 1.2;
this.ctx.moveTo(branch2X, branch2Y);
this.ctx.lineTo(branch2EndX, branch2EndY);
}
this.ctx.stroke();
this.ctx.restore();
}
}