Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/effects/background.js
1036 views
1
/*globals define*/
2
define([
3
'object2d',
4
'utils'
5
], function( Object2D, Utils ) {
6
'use strict';
7
8
var defaults = {
9
count: 100,
10
parallax: 0.25,
11
12
hueSpread: 50,
13
saturationSpread: 15,
14
lightnessSpread: 25
15
};
16
17
function Background( width, height, options ) {
18
Object2D.call( this, 0, 0 );
19
20
Utils.defaults( this, options, defaults );
21
22
this.canvas = document.createElement( 'canvas' );
23
this.ctx = this.canvas.getContext( '2d' );
24
25
this.width = width || 0;
26
this.height = height || 0;
27
28
this.game = null;
29
this.camera = null;
30
}
31
32
Background.prototype = new Object2D();
33
Background.prototype.constructor = Background;
34
35
Background.prototype.prerender = function() {
36
var hsl = this.fill.hslObject();
37
38
var hue = hsl.hue,
39
saturation = hsl.saturation,
40
lightness = hsl.lightness;
41
42
var width = this.width,
43
height = this.height;
44
45
var rects = [];
46
var rectCount = this.count;
47
48
var rectHue, rectSaturation, rectLightness;
49
while ( rectCount-- ) {
50
rectHue = this.hueSpread ? Utils.intSpread( hue, this.hueSpread ) : hue;
51
rectSaturation = this.saturationSpread ? Utils.intSpread( saturation, this.saturationSpread ) : saturation;
52
rectLightness = this.lightnessSpread ? Utils.intSpread( lightness, this.lightnessSpread ): lightness;
53
54
rects.push({
55
x: Math.random() * width,
56
y: Math.random() * height,
57
width: Utils.randomFloat( 0.05, 0.3 ) * width,
58
height: Utils.randomFloat( 0.05, 0.3 ) * height,
59
hue: rectHue,
60
saturation: rectSaturation + '%',
61
lightness: rectLightness + '%'
62
});
63
}
64
65
var ctx = this.ctx;
66
ctx.fillStyle = this.fill.rgba();
67
ctx.fillRect( 0, 0, width, height );
68
69
rects.forEach(function( rect ) {
70
ctx.save();
71
ctx.translate( rect.x, rect.y );
72
73
ctx.beginPath();
74
ctx.rect( -0.5 * rect.width, -0.5 * rect.height, rect.width, rect.height );
75
ctx.fillStyle = 'hsla(' +
76
rect.hue + ', ' +
77
rect.saturation + ', ' +
78
rect.lightness + ', ' +
79
Math.random() +
80
')';
81
82
ctx.fill();
83
84
ctx.restore();
85
});
86
};
87
88
Background.prototype.draw = function( ctx ) {
89
if ( !this.game || !this.camera ) {
90
return;
91
}
92
93
ctx.save();
94
ctx.translate( this.camera.x * this.parallax, this.camera.y * this.parallax );
95
ctx.scale( this.parallax, this.parallax );
96
97
ctx.drawImage( this.canvas, -0.5 * this.width, -0.5 * this.height );
98
99
ctx.restore();
100
};
101
102
Object.defineProperty( Background.prototype, 'width', {
103
get: function() {
104
return this.canvas.width;
105
},
106
107
set: function( width ) {
108
this.canvas.width = width || 0;
109
}
110
});
111
112
Object.defineProperty( Background.prototype, 'height', {
113
get: function() {
114
return this.canvas.height;
115
},
116
117
set: function( height ) {
118
this.canvas.height = height || 0;
119
}
120
});
121
122
return Background;
123
});
124
125