@erikwatson/snowfall 3.1.2 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ /**
2
+ * @module snowfall
3
+ *
4
+ * Lightweight, high-performance snowfall effect for the web.
5
+ * Fully configurable, supports multiple layers, wind, gusts, and scheduled activation.
6
+ *
7
+ * Stay cool ☃️
8
+ */
9
+ import { UserConfig, UserSchedule } from './types';
10
+ /**
11
+ * Starts the Snowfall simulation.
12
+ *
13
+ * @param {UserConfig} [config] - A config, possibly from the [Visual Config Editor](https://erikwatson.github.io/snowfall-editor/).
14
+ */
15
+ export declare function start(config?: UserConfig): void;
16
+ export declare function restart(config?: UserConfig): void;
17
+ export declare function stop(): void;
18
+ /**
19
+ * Starts the Snowfall simulation when today's date falls within the date range in the schedule.
20
+ *
21
+ * @param {UserSchedule} userSchedule - A schedule config, defining when the simulation should run from, and when the simulation should run to.
22
+ * @param {UserConfig} [config] - A config, possibly from the [Visual Config Editor](https://erikwatson.github.io/snowfall-editor/).
23
+ */
24
+ export declare function schedule(userSchedule: UserSchedule, config?: UserConfig): void;
25
+ /**
26
+ * Set the colour of the Snowflakes
27
+ * @param {string} colour - The colour to set
28
+ * @param {number} layer - The layer to set the colour for
29
+ */
30
+ export declare function setColour(colour: string, layer: number): void;
31
+ /**
32
+ * Set the density of the Snowflakes. This is the number of snowflakes on screen
33
+ * at a resolution of 1280 x 1080, but this number is scaled up and down at
34
+ * higher and lower resolutions respectively to give a consistent look when
35
+ * resizing.
36
+ *
37
+ * Setting this restarts the simulation.
38
+ *
39
+ * @param {number} density - A number representing the density of snowflakes.
40
+ * @param {number} layer - The layer to set the density for
41
+ */
42
+ export declare function setDensity(density: number, layer: number): void;
43
+ /**
44
+ * Set the Amplitude of the sway of the Snowflakes
45
+ *
46
+ * @param {number} amplitude - The Amplitude to set
47
+ * @param {number} layer - The layer to set the amplitude for
48
+ */
49
+ export declare function setAmplitude(amplitude: number, layer: number): void;
50
+ /**
51
+ * Set the Frequency of the sway of the Snowflakes.
52
+ *
53
+ * @param {number} frequency - The frequency to set
54
+ * @param {number} layer - The layer to set the frequency for
55
+ */
56
+ export declare function setFrequency(frequency: number, layer: number): void;
57
+ /**
58
+ * Pauses or resumes a specific layer.
59
+ *
60
+ * @param {boolean} pause - Pass true to pause the layer, false to resume
61
+ * @param {number} layer - The layer index
62
+ */
63
+ export declare function setPaused(pause: boolean, layer: number): void;
64
+ /**
65
+ * Pause/unpause the snowfall update loop for a specific layer.
66
+ *
67
+ * @param layer - The layer index to toggle
68
+ */
69
+ export declare function togglePaused(layer: number): void;
70
+ /**
71
+ * Set the angle and strength of the wind in the simulation.
72
+ *
73
+ * @param {number} angle - The angle of the wind, in degrees
74
+ * @param {number} strength - The strength of the wind
75
+ * @param {number} layer - The layer to set the wind for
76
+ */
77
+ export declare function setWind(angle: number, strength: number, layer: number): void;
78
+ /**
79
+ * Set the angle of the wind in the simulation.
80
+ *
81
+ * @param {number} angle - The angle of the wind, in degrees
82
+ * @param {number} layer - The layer to set the wind angle for
83
+ */
84
+ export declare function setWindAngle(angle: number, layer: number): void;
85
+ /**
86
+ * Set the strength of the wind in the simulation.
87
+ *
88
+ * @param {number} strength - The strength of the wind
89
+ * @param {number} layer - The layer to apply the wind strength to
90
+ */
91
+ export declare function setWindStrength(strength: number, layer: number): void;
92
+ /**
93
+ * Set the wind gusts in the simulation.
94
+ * This restarts the simulation.
95
+ *
96
+ * @param {boolean} gusts - Should there be gusts in the wind?
97
+ * @param {number} layer - The layer to set the gusts for
98
+ */
99
+ export declare function setGusts(gusts: boolean, layer: number): void;
100
+ /**
101
+ * Set the angle and strength of gravity in the simulation.
102
+ *
103
+ * @param {number} angle - The angle of gravity, in degrees
104
+ * @param {number} strength - The strength of the gravity
105
+ * @param {number} layer - The layer to set the gravity for
106
+ */
107
+ export declare function setGravity(angle: number, strength: number, layer: number): void;
108
+ /**
109
+ * Set the angle of gravity in the simulation.
110
+ *
111
+ * @param {number} angle - The angle of gravity, in degrees
112
+ * @param {number} layer - The layer to set the gravity angle for
113
+ */
114
+ export declare function setGravityAngle(angle: number, layer: number): void;
115
+ /**
116
+ * Set the strength of gravity in the simulation.
117
+ *
118
+ * @param {number} strength - The strength of the gravity
119
+ * @param {number} layer - The layer to set the gravity strength for
120
+ */
121
+ export declare function setGravityStrength(strength: number, layer: number): void;
122
+ /**
123
+ * Set the minimum additional strength of the wind when it's coming in.
124
+ *
125
+ * @param {number} min - The minimum additional strength of the wind when it's coming in.
126
+ * @param {number} layer - The layer to set the wind in additional strength min for
127
+ * @returns {void}
128
+ */
129
+ export declare function setWindInAdditionalStrengthMin(min: number, layer: number): void;
130
+ /**
131
+ * Set the maximum additional strength of the wind when it's coming in.
132
+ *
133
+ * @param {number} max - The maximum additional strength of the wind when it's coming in.
134
+ * @param {number} layer - The layer to set the wind in additional strength max for
135
+ * @returns {void}
136
+ */
137
+ export declare function setWindInAdditionalStrengthMax(max: number, layer: number): void;
138
+ /**
139
+ * Set the minimum duration of the wind when it's coming in.
140
+ *
141
+ * @param {number} min - The minimum duration of the wind when it's coming in.
142
+ * @param {number} layer - The layer to set the wind in duration min for
143
+ * @returns {void}
144
+ */
145
+ export declare function setWindInDurationMin(min: number, layer: number): void;
146
+ /**
147
+ * Set the maximum duration of the wind when it's coming in.
148
+ *
149
+ * @param {number} max - The maximum duration of the wind when it's coming in.
150
+ * @param {number} layer - The layer to set the wind in duration max for
151
+ * @returns {void}
152
+ */
153
+ export declare function setWindInDurationMax(max: number, layer: number): void;
154
+ /**
155
+ * Set the minimum delay of the wind when it's coming in.
156
+ *
157
+ * @param {number} min - The minimum delay of the wind when it's coming in.
158
+ * @param {number} layer - The layer to set the wind in delay min for
159
+ * @returns {void}
160
+ */
161
+ export declare function setWindInDelayMin(min: number, layer: number): void;
162
+ /**
163
+ * Set the maximum delay of the wind when it's coming in.
164
+ *
165
+ * @param {number} max - The maximum delay of the wind when it's coming in.
166
+ * @param {number} layer - The layer to set the wind in delay max for
167
+ * @returns {void}
168
+ */
169
+ export declare function setWindInDelayMax(max: number, layer: number): void;
170
+ /**
171
+ * Set the minimum duration of the wind when it's going out.
172
+ *
173
+ * @param {number} min - The minimum duration of the wind when it's going out.
174
+ * @param {number} layer - The layer to set the wind out duration min for
175
+ * @returns {void}
176
+ */
177
+ export declare function setWindOutDurationMin(min: number, layer: number): void;
178
+ /**
179
+ * Set the maximum duration of the wind when it's going out.
180
+ *
181
+ * @param {number} max - The maximum duration of the wind when it's going out.
182
+ * @param {number} layer - The layer to set the wind out duration max for
183
+ * @returns {void}
184
+ */
185
+ export declare function setWindOutDurationMax(max: number, layer: number): void;
186
+ /**
187
+ * Set the minimum delay of the wind when it's going out.
188
+ *
189
+ * @param {number} min - The minimum delay of the wind when it's going out.
190
+ * @param {number} layer - The layer to set the wind out delay min for
191
+ * @returns {void}
192
+ */
193
+ export declare function setWindOutDelayMin(min: number, layer: number): void;
194
+ /**
195
+ * Set the maximum delay of the wind when it's going out.
196
+ *
197
+ * @param {number} max - The maximum delay of the wind when it's going out.
198
+ * @param {number} layer - The layer to set the wind out delay max for
199
+ * @returns {void}
200
+ */
201
+ export declare function setWindOutDelayMax(max: number, layer: number): void;
202
+ /**
203
+ * Set the chance of the wind changing direction after a gust.
204
+ *
205
+ * @param {number} chance - The chance of the wind changing direction after a gust.
206
+ * @param {number} layer - The layer to set the wind out change chance for
207
+ * @returns {void}
208
+ */
209
+ export declare function setWindOutChangeChance(chance: number, layer: number): void;
210
+ /**
211
+ * Set the minimum mass of the snowflakes.
212
+ *
213
+ * @param {number} min - The minimum mass of the snowflakes.
214
+ * @param {number} layer - The layer to set the mass min for
215
+ */
216
+ export declare function setMassMin(min: number, layer: number): void;
217
+ /**
218
+ * Set the maximum mass of the snowflakes.
219
+ *
220
+ * @param {number} max - The maximum mass of the snowflakes.
221
+ * @param {number} layer - The layer to set the mass max for
222
+ */
223
+ export declare function setMassMax(max: number, layer: number): void;
224
+ /**
225
+ * Set the minimum size of the snowflakes.
226
+ *
227
+ * @param {number} min - The minimum rendered size of the snowflakes.
228
+ * @param {number} layer - The layer to set the rendered size min for
229
+ */
230
+ export declare function setSizeMin(min: number, layer: number): void;
231
+ /**
232
+ * Set the maximum size of the snowflakes.
233
+ *
234
+ * @param {number} max - The maximum rendered size of the snowflakes.
235
+ * @param {number} layer - The layer to set the rendered size max for
236
+ */
237
+ export declare function setSizeMax(max: number, layer: number): void;
238
+ export { diff } from './config';
239
+ export * from './defaults';
240
+ export { clone } from './utils';
241
+ export { isSimpleLayer } from './utils';
242
+ export { UserSchedule, UserConfig, SimpleLayerConfig, ImageLayerConfig, CompleteUserConfig, BaseConfig, ConfigLayer, Gravity, SizeBounds, Sway, Wind, Gusts, In, Out } from './types';
@@ -0,0 +1,2 @@
1
+ export { addWind, addGravity, addSwayMotion, screenWrap, fadeIn, addRotation } from './move';
2
+ export { drawLayer } from './render';
@@ -0,0 +1,16 @@
1
+ import { Snowflake } from '../types';
2
+ export declare function addWind(snowflake: Snowflake, angle: number, strength: number): void;
3
+ export declare function addRotation(snowflake: Snowflake): void;
4
+ export declare function addGravity(snowflake: Snowflake, angle: number, strength: number): void;
5
+ export declare function addSwayMotion(snowflake: Snowflake, gravity: {
6
+ angle: number;
7
+ strength: number;
8
+ }, sway: {
9
+ frequency: number;
10
+ amplitude: number;
11
+ }): void;
12
+ export declare function fadeIn(snowflake: Snowflake): void;
13
+ export declare function screenWrap(snowflake: Snowflake, width: number, height: number, gravity: {
14
+ angle: number;
15
+ strength: number;
16
+ }): void;
@@ -0,0 +1,3 @@
1
+ import { Graphics } from '@erikwatson/bramble';
2
+ import { Snowflake } from '../types';
3
+ export declare function drawLayer(gfx: Graphics, snowflakes: Snowflake[], colour: string): void;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,280 @@
1
+ import { Graphics, Vec2 } from '@erikwatson/bramble';
2
+ export type SizeBounds = {
3
+ min: number;
4
+ max: number;
5
+ };
6
+ export type In = {
7
+ /**
8
+ * The minimum and maximum strength to add to the wind gusts.
9
+ * @default { min: 1, max: 3 }
10
+ */
11
+ additionalStrength: SizeBounds;
12
+ /**
13
+ * The minimum and maximum duration of the wind gusts.
14
+ * @default { min: 1000, max: 3000 }
15
+ */
16
+ duration: SizeBounds;
17
+ /**
18
+ * The minimum and maximum delay before the wind eases off.
19
+ * @default { min: 5000, max: 10000 }
20
+ */
21
+ delay: SizeBounds;
22
+ };
23
+ export type Out = {
24
+ /**
25
+ * The minimum and maximum duration of the wind gusts.
26
+ * @default { min: 5000, max: 10000 }
27
+ */
28
+ duration: SizeBounds;
29
+ /**
30
+ * The minimum and maximum delay before the wind eases off.
31
+ * @default { min: 1000, max: 10000 }
32
+ */
33
+ delay: SizeBounds;
34
+ };
35
+ export type Gusts = {
36
+ /**
37
+ * Should the wind gust?
38
+ * @default true
39
+ */
40
+ active: boolean;
41
+ /**
42
+ * The likelihood of the wind changing direction after a gust. 0.0 is never, 1.0 is always.
43
+ * @default 0.25
44
+ */
45
+ changeChance: number;
46
+ in: In;
47
+ out: Out;
48
+ };
49
+ export type Gravity = {
50
+ /**
51
+ * The angle of gravity, in degrees.
52
+ * @default 90
53
+ */
54
+ angle: number;
55
+ /**
56
+ * The strength of gravity.
57
+ * @default 0.7
58
+ */
59
+ strength: number;
60
+ };
61
+ export type Sway = {
62
+ /**
63
+ * The frequency of the sway the snowflakes follow.
64
+ * @default 0.02
65
+ */
66
+ frequency: number;
67
+ /**
68
+ * The amplitude of the sway the snowflakes follow.
69
+ * @default 1.0
70
+ */
71
+ amplitude: number;
72
+ };
73
+ export type Wind = {
74
+ /**
75
+ * The angle of the wind, in degrees.
76
+ * @default 0
77
+ */
78
+ angle: number;
79
+ /**
80
+ * The strength of the wind.
81
+ * @default 0
82
+ */
83
+ strength: number;
84
+ gusts: Gusts;
85
+ };
86
+ export interface BaseLayerConfig {
87
+ /**
88
+ * A number representing the required density of snowflakes on screen. Note, this is not the actual number of snowflakes.
89
+ * @default 200
90
+ */
91
+ density: number;
92
+ /**
93
+ * The size of the snowflakes.
94
+ * @default { min: 1, max: 3 }
95
+ */
96
+ mass: SizeBounds;
97
+ size: SizeBounds;
98
+ sway: Sway;
99
+ gravity: Gravity;
100
+ wind: Wind;
101
+ opacity: SizeBounds;
102
+ }
103
+ export interface SimpleLayerConfig extends BaseLayerConfig {
104
+ mode: 'simple';
105
+ /**
106
+ * A hex string representing the colour of the snowflakes in the foreground.
107
+ * @default '#8d90b7'
108
+ */
109
+ colour: string;
110
+ }
111
+ export interface ImageLayerConfig extends BaseLayerConfig {
112
+ mode: 'image';
113
+ /**
114
+ * An image to render
115
+ */
116
+ image: string;
117
+ /**
118
+ * Should the snow rotate, or not?
119
+ * @default false
120
+ */
121
+ rotate: boolean;
122
+ }
123
+ export interface BaseConfig {
124
+ [key: string]: any;
125
+ /**
126
+ * A hex string representing the Background Colour of the canvas.
127
+ * @default '#0d0014'
128
+ */
129
+ /**
130
+ * An array of uniquely configured snowflake layers.
131
+ */
132
+ layers: ConfigLayer[];
133
+ }
134
+ export type ConfigLayer = SimpleLayerConfig | ImageLayerConfig;
135
+ export interface Config extends BaseConfig {
136
+ /**
137
+ * The element to attach the simulation to.
138
+ * @default '#snowfall'
139
+ */
140
+ attachTo: HTMLElement;
141
+ }
142
+ export type DeepPartial<T> = {
143
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
144
+ };
145
+ export interface CompleteUserConfig extends BaseConfig {
146
+ /**
147
+ * The element to attach the simulation to, as a string representing the ID.
148
+ */
149
+ attachTo: string;
150
+ /**
151
+ * A fully configured array of snowflake layers.
152
+ */
153
+ layers: ConfigLayer[];
154
+ }
155
+ export interface UserConfig extends Omit<Partial<CompleteUserConfig>, 'layers'> {
156
+ /**
157
+ * An optional array of uniquely configured snowflake layers,
158
+ * where each layer can be partially configured.
159
+ * @default undefined
160
+ */
161
+ layers?: DeepPartial<ConfigLayer>[];
162
+ /**
163
+ * A string that represents the ID of an element you want to attach snowfall to.
164
+ * @default '#snowfall'
165
+ */
166
+ attachTo?: string;
167
+ }
168
+ export interface UserSchedule {
169
+ /** The date from which to start the simulation */
170
+ from: {
171
+ /** The day of the month, starting at 1 */
172
+ day: number;
173
+ /** The month of the year, starting at 1 */
174
+ month: number;
175
+ };
176
+ /** The date on which to end the simulation */
177
+ to: {
178
+ /** The day of the month, starting at 1 */
179
+ day: number;
180
+ /** The month of the year, starting at 1 */
181
+ month: number;
182
+ };
183
+ }
184
+ export type Snowflake = {
185
+ position: Vec2;
186
+ mass: number;
187
+ size: number;
188
+ renderedSize: number;
189
+ noise: number;
190
+ time: number;
191
+ amplitude: number;
192
+ frequency: number;
193
+ random: number;
194
+ rotation: number;
195
+ opacity: number;
196
+ };
197
+ export interface Simulation {
198
+ start: (config: UserConfig) => void;
199
+ setAmplitude: (num: number, layer: number) => void;
200
+ setDensity: (den: number, layer: number) => void;
201
+ setFrequency: (freq: number, layer: number) => void;
202
+ setGravity: (degrees: number, strength: number, layer: number) => void;
203
+ setGravityAngle: (degrees: number, layer: number) => void;
204
+ setGravityStrength: (strength: number, layer: number) => void;
205
+ setPaused: (pause: boolean, layer: number) => void;
206
+ setWind: (degrees: number, strength: number, layer: number) => void;
207
+ setWindAngle: (degrees: number, layer: number) => void;
208
+ setWindStrength: (strength: number, layer: number) => void;
209
+ setGusts: (gusts: boolean, layer: number) => void;
210
+ togglePaused: (layer: number) => void;
211
+ setWindInAdditionalStrengthMin: (min: number, layer: number) => void;
212
+ setWindInAdditionalStrengthMax: (max: number, layer: number) => void;
213
+ setWindInDurationMin: (min: number, layer: number) => void;
214
+ setWindInDurationMax: (max: number, layer: number) => void;
215
+ setWindInDelayMin: (min: number, layer: number) => void;
216
+ setWindInDelayMax: (max: number, layer: number) => void;
217
+ setWindOutDurationMin: (min: number, layer: number) => void;
218
+ setWindOutDurationMax: (max: number, layer: number) => void;
219
+ setWindOutDelayMin: (min: number, layer: number) => void;
220
+ setWindOutDelayMax: (max: number, layer: number) => void;
221
+ setWindOutChangeChance: (chance: number, layer: number) => void;
222
+ setColour: (colour: string, layer: number) => void;
223
+ setMassMin: (min: number, layer: number) => void;
224
+ setMassMax: (max: number, layer: number) => void;
225
+ setSizeMin: (min: number, layer: number) => void;
226
+ setSizeMax: (max: number, layer: number) => void;
227
+ restart: (config?: UserConfig) => void;
228
+ stop: () => void;
229
+ }
230
+ export interface HasGravity {
231
+ setGravity(degrees: number, strength: number): void;
232
+ setGravityAngle(degrees: number): void;
233
+ setGravityStrength(strength: number): void;
234
+ }
235
+ export interface HasWind {
236
+ setWind(degrees: number, strength: number): void;
237
+ setWindAngle(degrees: number): void;
238
+ setWindStrength(strength: number): void;
239
+ setGusts(gusts: boolean): void;
240
+ setWindInAdditionalStrengthMin(min: number): void;
241
+ setWindInAdditionalStrengthMax(max: number): void;
242
+ setWindInDurationMin(min: number): void;
243
+ setWindInDurationMax(max: number): void;
244
+ setWindInDelayMin(min: number): void;
245
+ setWindInDelayMax(max: number): void;
246
+ setWindOutDurationMin(min: number): void;
247
+ setWindOutDurationMax(max: number): void;
248
+ setWindOutDelayMin(min: number): void;
249
+ setWindOutDelayMax(max: number): void;
250
+ setWindOutChangeChance(chance: number): void;
251
+ }
252
+ export interface HasSway {
253
+ setAmplitude(num: number): void;
254
+ setFrequency(freq: number): void;
255
+ }
256
+ export interface HasDensity {
257
+ setDensity(density: number): void;
258
+ }
259
+ export interface HasRender {
260
+ render(gfx: Graphics): void;
261
+ }
262
+ export interface IBaseLayer extends HasGravity, HasWind, HasSway {
263
+ start(): void;
264
+ pause(): void;
265
+ resume(): void;
266
+ restart(): void;
267
+ update(dt: number): void;
268
+ setPaused(pause: boolean): void;
269
+ togglePaused(): void;
270
+ }
271
+ export interface ISimpleLayer extends IBaseLayer, HasRender {
272
+ mode: 'simple';
273
+ config: SimpleLayerConfig;
274
+ setColour(colour: string): void;
275
+ }
276
+ export interface IImageLayer extends IBaseLayer, HasRender {
277
+ mode: 'image';
278
+ config: ImageLayerConfig;
279
+ setImage(image: string): void;
280
+ }
@@ -0,0 +1,19 @@
1
+ import { BaseLayerConfig, ConfigLayer, ImageLayerConfig, SimpleLayerConfig, UserSchedule } from './types';
2
+ export declare function getElementOrThrow(id: string): HTMLElement;
3
+ export declare function withinSchedule(schedule: UserSchedule): boolean;
4
+ export declare function requiredSnowflakes(width: number, height: number, density: number): number;
5
+ export declare function clone(obj: any): any;
6
+ export declare function makeSnowflakes(num: number, config: BaseLayerConfig | SimpleLayerConfig | ImageLayerConfig, width: number, height: number): {
7
+ position: import("@erikwatson/bramble").Vec2;
8
+ mass: number;
9
+ size: number;
10
+ renderedSize: number;
11
+ noise: number;
12
+ amplitude: number;
13
+ frequency: number;
14
+ random: number;
15
+ rotation: number;
16
+ time: number;
17
+ opacity: number;
18
+ }[];
19
+ export declare function isSimpleLayer(layer: ConfigLayer): layer is SimpleLayerConfig;