forked from joinpursuit/FSW-Personal-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrain-engine-manager.js
More file actions
642 lines (533 loc) · 17.4 KB
/
rain-engine-manager.js
File metadata and controls
642 lines (533 loc) · 17.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/**
* Rain Engine Manager - Dual Engine Architecture
* Provides unified API for WebGL (RaindropFX) and Canvas2D (RainyDay.js) rain effects
*/
// Unified API Interface
class RainEngine {
constructor(canvas, options = {}) {
this.canvas = canvas;
this.options = this.mergeDefaultOptions(options);
this.isRunning = false;
this.animationId = null;
}
async start() {
throw new Error('start() must be implemented by engine');
}
stop() {
throw new Error('stop() must be implemented by engine');
}
resize(width, height) {
throw new Error('resize() must be implemented by engine');
}
async setBackground(source) {
throw new Error('setBackground() must be implemented by engine');
}
setIntensity(level) {
throw new Error('setIntensity() must be implemented by engine');
}
setWind(strength) {
throw new Error('setWind() must be implemented by engine');
}
setCondensation(enabled) {
throw new Error('setCondensation() must be implemented by engine');
}
destroy() {
throw new Error('destroy() must be implemented by engine');
}
mergeDefaultOptions(options) {
return {
// Common options
width: this.canvas.width || 800,
height: this.canvas.height || 600,
background: options.background || null,
intensity: options.intensity || 2, // 0-3 scale
wind: options.wind || 0.5, // 0-1 scale
condensation: options.condensation !== false, // default true
// Engine-specific options will be merged by individual engines
...options
};
}
}
// WebGL RaindropFX Engine Implementation
class RaindropFXEngine extends RainEngine {
constructor(canvas, options = {}) {
super(canvas, options);
this.renderer = null;
this.simulator = null;
this.isWebGLSupported = this.detectWebGLSupport();
if (!this.isWebGLSupported) {
throw new Error('WebGL not supported, use RainyDayEngine instead');
}
}
detectWebGLSupport() {
try {
// Create a test canvas for WebGL detection
const testCanvas = document.createElement('canvas');
const gl = testCanvas.getContext('webgl2') || testCanvas.getContext('webgl');
if (!gl) return false;
// Test basic WebGL functionality
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
if (!vertexShader || !fragmentShader) return false;
// Clean up
gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);
return true;
} catch (e) {
console.warn('WebGL detection failed:', e);
return false;
}
}
async start() {
if (this.isRunning) return;
try {
// Initialize WebGL renderer and simulator
await this.initializeWebGL();
this.isRunning = true;
this.animationLoop();
} catch (error) {
console.error('Failed to start RaindropFX engine:', error);
throw error;
}
}
async initializeWebGL() {
console.log('Initializing RaindropFX WebGL engine...');
// Ensure canvas is properly sized
if (this.canvas.width === 0 || this.canvas.height === 0) {
console.warn('Canvas has zero dimensions, setting default size');
this.canvas.width = 800;
this.canvas.height = 600;
}
// Create a WebGL context with proper error handling
let gl = null;
try {
// Try WebGL2 first with minimal options
gl = this.canvas.getContext('webgl2');
if (!gl) {
// Fallback to WebGL1
gl = this.canvas.getContext('webgl');
}
if (!gl) {
// Last resort: experimental-webgl
gl = this.canvas.getContext('experimental-webgl');
}
} catch (error) {
console.error('WebGL context creation error:', error);
throw new Error('WebGL context creation failed: ' + error.message);
}
if (!gl) {
throw new Error('WebGL context creation failed - no WebGL support available');
}
// Set up basic WebGL state
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.clearColor(0, 0, 0, 0);
gl.viewport(0, 0, this.canvas.width, this.canvas.height);
this.gl = gl;
console.log('WebGL context created successfully');
}
animationLoop() {
if (!this.isRunning) return;
// Simple animated gradient to show WebGL is working
const time = performance.now() * 0.001;
const r = 0.1 + 0.1 * Math.sin(time);
const g = 0.2 + 0.1 * Math.cos(time * 0.7);
const b = 0.4 + 0.1 * Math.sin(time * 1.3);
this.gl.clearColor(r, g, b, 1.0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
this.animationId = requestAnimationFrame(() => this.animationLoop());
}
stop() {
this.isRunning = false;
if (this.animationId) {
cancelAnimationFrame(this.animationId);
this.animationId = null;
}
}
resize(width, height) {
this.canvas.width = width;
this.canvas.height = height;
this.options.width = width;
this.options.height = height;
if (this.gl) {
this.gl.viewport(0, 0, width, height);
}
}
async setBackground(source) {
this.options.background = source;
// Placeholder for background loading
console.log('Setting background:', source);
}
setIntensity(level) {
this.options.intensity = Math.max(0, Math.min(3, level));
// Map intensity to engine-specific parameters
const intensityMap = {
0: { dropletsPerSeconds: 0, spawnLimit: 0 },
1: { dropletsPerSeconds: 200, spawnLimit: 500 },
2: { dropletsPerSeconds: 500, spawnLimit: 1000 },
3: { dropletsPerSeconds: 800, spawnLimit: 2000 }
};
const params = intensityMap[Math.floor(this.options.intensity)];
Object.assign(this.options, params);
}
setWind(strength) {
this.options.wind = Math.max(0, Math.min(1, strength));
// Map wind to xShifting parameter
this.options.xShifting = [0, strength * 0.12];
}
setCondensation(enabled) {
this.options.condensation = enabled;
this.options.mist = enabled;
}
destroy() {
this.stop();
if (this.gl) {
// Clean up WebGL resources
this.gl = null;
}
}
}
// Canvas2D RainyDay Engine Implementation
class RainyDayEngine extends RainEngine {
constructor(canvas, options = {}) {
super(canvas, options);
this.rainyDay = null;
this.rainyDayOptions = this.buildRainyDayOptions();
}
buildRainyDayOptions() {
return {
image: this.options.background,
fps: 24,
blur: 8 + (this.options.intensity * 2), // 8-14 based on intensity
enableCollisions: true,
gravityAngleVariance: 0.01 + (this.options.wind * 0.02), // 0.01-0.03
trail: this.options.condensation ? 'TRAIL_SMUDGE' : 'TRAIL_DROPS',
presets: this.getIntensityPresets()
};
}
getIntensityPresets() {
const intensityPresets = {
0: [],
1: [[3, 5, 0.3, 50]],
2: [[3, 5, 0.6, 50], [6, 8, 0.2, 30]],
3: [[3, 5, 0.8, 50], [6, 8, 0.4, 30], [10, 15, 0.1, 20]]
};
return intensityPresets[Math.floor(this.options.intensity)] || intensityPresets[2];
}
async start() {
if (this.isRunning) return;
try {
// Initialize RainyDay.js
await this.initializeRainyDay();
this.isRunning = true;
} catch (error) {
console.error('Failed to start RainyDay engine:', error);
throw error;
}
}
async initializeRainyDay() {
// Check if RainyDay is available
if (typeof RainyDay === 'undefined') {
throw new Error('RainyDay.js library not loaded. Please ensure the script is included.');
}
console.log('Initializing RainyDay.js engine...');
// Ensure we have a proper IMG element for RainyDay.js
let imageElement = this.options.background;
// Handle different background source types
if (typeof this.options.background === 'string' && this.options.background.startsWith('#')) {
imageElement = document.querySelector(this.options.background);
}
// If we have a canvas or other non-IMG element, create a proper IMG element
if (!imageElement || imageElement.tagName !== 'IMG') {
console.log('Creating IMG element for RainyDay.js...');
imageElement = await this.createImageElementForRainyDay();
}
// Wait for image to load
if (imageElement.tagName === 'IMG') {
console.log('Waiting for background image to load...');
await new Promise((resolve, reject) => {
if (imageElement.complete) {
resolve();
} else {
imageElement.onload = resolve;
imageElement.onerror = reject;
}
});
}
try {
// Create RainyDay instance with proper IMG element
this.rainyDay = new RainyDay({
image: imageElement,
fps: this.rainyDayOptions.fps,
blur: this.rainyDayOptions.blur,
enableCollisions: this.rainyDayOptions.enableCollisions,
gravityAngleVariance: this.rainyDayOptions.gravityAngleVariance
});
console.log('RainyDay instance created successfully');
// Start rain with presets
if (this.rainyDayOptions.presets.length > 0) {
this.rainyDay.rain(this.rainyDayOptions.presets, 50);
console.log('Rain started with presets:', this.rainyDayOptions.presets);
}
} catch (error) {
console.error('Failed to create RainyDay instance:', error);
throw new Error('Failed to create RainyDay instance: ' + error.message);
}
}
async createImageElementForRainyDay() {
// Create a new IMG element for RainyDay.js
const img = document.createElement('img');
img.crossOrigin = 'anonymous';
// Set up the image source
if (typeof this.options.background === 'string') {
if (this.options.background.startsWith('http') || this.options.background.startsWith('data:')) {
img.src = this.options.background;
} else {
// Assume it's a URL
img.src = this.options.background;
}
} else if (this.options.background && this.options.background.tagName === 'IMG') {
img.src = this.options.background.src;
} else {
// Create a default background
img.src = 'data:image/svg+xml;base64,' + btoa(`
<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#4a90e2;stop-opacity:1" />
<stop offset="100%" style="stop-color:#357abd;stop-opacity:1" />
</linearGradient>
</defs>
<rect width="100%" height="100%" fill="url(#grad)"/>
</svg>
`);
}
return img;
}
stop() {
this.isRunning = false;
if (this.rainyDay) {
this.rainyDay.pause();
}
}
resize(width, height) {
this.canvas.width = width;
this.canvas.height = height;
this.options.width = width;
this.options.height = height;
if (this.rainyDay) {
// RainyDay doesn't have a direct resize method, so we need to recreate
this.stop();
this.initializeRainyDay();
}
}
async setBackground(source) {
this.options.background = source;
this.rainyDayOptions.image = source;
if (this.rainyDay) {
// Update RainyDay background
this.rainyDay.clear();
this.rainyDay = new RainyDay(this.rainyDayOptions);
if (this.isRunning) {
this.rainyDay.rain(this.rainyDayOptions.presets, 50);
}
}
}
setIntensity(level) {
this.options.intensity = Math.max(0, Math.min(3, level));
this.rainyDayOptions.presets = this.getIntensityPresets();
this.rainyDayOptions.blur = 8 + (level * 2);
if (this.rainyDay && this.isRunning) {
this.rainyDay.clear();
this.rainyDay.rain(this.rainyDayOptions.presets, 50);
}
}
setWind(strength) {
this.options.wind = Math.max(0, Math.min(1, strength));
this.rainyDayOptions.gravityAngleVariance = 0.01 + (strength * 0.02);
if (this.rainyDay) {
this.rainyDay.gravityAngleVariance = this.rainyDayOptions.gravityAngleVariance;
}
}
setCondensation(enabled) {
this.options.condensation = enabled;
this.rainyDayOptions.trail = enabled ? 'TRAIL_SMUDGE' : 'TRAIL_DROPS';
if (this.rainyDay) {
// Update trail mode
this.rainyDay.trail = this.rainyDayOptions.trail;
}
}
destroy() {
this.stop();
if (this.rainyDay) {
this.rainyDay.clear();
this.rainyDay = null;
}
}
}
// Main Engine Manager
class RainEngineManager {
constructor(canvas, options = {}) {
this.canvas = canvas;
this.options = options;
this.currentEngine = null;
this.engineType = null;
this.performanceMonitor = new PerformanceMonitor();
}
// Detect WebGL support
detectWebGLSupport() {
try {
// Create a test canvas for WebGL detection
const testCanvas = document.createElement('canvas');
const gl = testCanvas.getContext('webgl2') || testCanvas.getContext('webgl');
if (!gl) return false;
// Test basic WebGL functionality
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
if (!vertexShader || !fragmentShader) return false;
// Clean up
gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);
return true;
} catch (e) {
console.warn('WebGL detection failed:', e);
return false;
}
}
// Create appropriate engine
createEngine(forceType = null) {
const webglSupported = this.detectWebGLSupport();
const engineType = forceType || (webglSupported ? 'webgl' : 'canvas2d');
console.log(`Creating ${engineType} engine (WebGL supported: ${webglSupported})`);
switch (engineType) {
case 'webgl':
if (!webglSupported) {
console.warn('WebGL not supported, falling back to Canvas2D');
return this.createEngine('canvas2d');
}
try {
this.currentEngine = new RaindropFXEngine(this.canvas, this.options);
this.engineType = 'webgl';
console.log('WebGL engine created successfully');
} catch (error) {
console.warn('WebGL engine creation failed, falling back to Canvas2D:', error.message);
return this.createEngine('canvas2d');
}
break;
case 'canvas2d':
this.currentEngine = new RainyDayEngine(this.canvas, this.options);
this.engineType = 'canvas2d';
console.log('Canvas2D engine created successfully');
break;
default:
throw new Error(`Unknown engine type: ${engineType}`);
}
return this.currentEngine;
}
// Switch between engines
async switchEngine(engineType) {
if (this.engineType === engineType) return;
const wasRunning = this.currentEngine?.isRunning || false;
// Stop current engine
if (this.currentEngine) {
this.currentEngine.stop();
this.currentEngine.destroy();
}
// Create new engine
this.createEngine(engineType);
// Restart if was running
if (wasRunning) {
await this.currentEngine.start();
}
console.log(`Switched to ${engineType} engine`);
}
// Unified API methods
async start() {
if (!this.currentEngine) {
this.createEngine();
}
return this.currentEngine.start();
}
stop() {
if (this.currentEngine) {
this.currentEngine.stop();
}
}
resize(width, height) {
if (this.currentEngine) {
this.currentEngine.resize(width, height);
}
}
async setBackground(source) {
if (this.currentEngine) {
return this.currentEngine.setBackground(source);
}
}
setIntensity(level) {
if (this.currentEngine) {
this.currentEngine.setIntensity(level);
}
}
setWind(strength) {
if (this.currentEngine) {
this.currentEngine.setWind(strength);
}
}
setCondensation(enabled) {
if (this.currentEngine) {
this.currentEngine.setCondensation(enabled);
}
}
destroy() {
if (this.currentEngine) {
this.currentEngine.destroy();
this.currentEngine = null;
}
}
// Get current engine info
getEngineInfo() {
return {
type: this.engineType,
isRunning: this.currentEngine?.isRunning || false,
webglSupported: this.detectWebGLSupport()
};
}
}
// Performance Monitor
class PerformanceMonitor {
constructor() {
this.frameCount = 0;
this.lastTime = performance.now();
this.fps = 60;
this.fpsHistory = [];
}
update() {
this.frameCount++;
const now = performance.now();
if (now - this.lastTime >= 1000) {
this.fps = this.frameCount;
this.fpsHistory.push(this.fps);
// Keep only last 10 FPS readings
if (this.fpsHistory.length > 10) {
this.fpsHistory.shift();
}
this.frameCount = 0;
this.lastTime = now;
}
}
getAverageFPS() {
if (this.fpsHistory.length === 0) return this.fps;
return this.fpsHistory.reduce((a, b) => a + b, 0) / this.fpsHistory.length;
}
shouldSwitchToFallback() {
return this.getAverageFPS() < 30 && this.fpsHistory.length >= 5;
}
}
// Export for use
if (typeof module !== 'undefined' && module.exports) {
module.exports = { RainEngineManager, RaindropFXEngine, RainyDayEngine };
} else {
window.RainEngineManager = RainEngineManager;
window.RaindropFXEngine = RaindropFXEngine;
window.RainyDayEngine = RainyDayEngine;
}