svelte-confetti-explosion 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- package/ConfettiExplosion.svelte +17 -39
- package/README.md +1 -1
- package/package.json +1 -1
package/ConfettiExplosion.svelte
CHANGED
@@ -4,22 +4,11 @@ const ROTATION_SPEED_MAX = 800; // maximum possible duration of single particle
|
|
4
4
|
const CRAZY_PARTICLES_FREQUENCY = 0.1; // 0-1 frequency of crazy curvy unpredictable particles
|
5
5
|
const CRAZY_PARTICLE_CRAZINESS = 0.3; // 0-1 how crazy these crazy particles are
|
6
6
|
const BEZIER_MEDIAN = 0.5; // utility for mid-point bezier curves, to ensure smooth motion paths
|
7
|
-
const FORCE = 0.5; // 0-1 roughly the vertical force at which particles initially explode
|
8
|
-
const SIZE = 12; // max height for particle rectangles, diameter for particle circles
|
9
|
-
const FLOOR_HEIGHT = 800; // pixels the particles will fall from initial explosion point
|
10
|
-
const FLOOR_WIDTH = 1600; // horizontal spread of particles in pixels
|
11
|
-
const PARTICLE_COUNT = 150;
|
12
|
-
const DURATION = 3500;
|
13
|
-
const COLORS = ['#FFC700', '#F00', '#2E3191', '#41BBC7'];
|
14
7
|
const abs = Math.abs, random = Math.random, mathRound = Math.round, max = Math.max;
|
15
|
-
const createParticles = (count, colors) => {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
degree: i * increment,
|
20
|
-
}));
|
21
|
-
};
|
22
|
-
const waitFor = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
8
|
+
const createParticles = (count, colors) => Array.from({ length: count }, (_, i) => ({
|
9
|
+
color: colors[i % colors.length],
|
10
|
+
degree: (i * 360) / count,
|
11
|
+
}));
|
23
12
|
// From here: https://stackoverflow.com/a/11832950
|
24
13
|
const round = (num, precision = 2) => mathRound((num + Number.EPSILON) * 10 ** precision) / 10 ** precision;
|
25
14
|
const mapRange = (value, x1, y1, x2, y2) => ((value - x1) * (y2 - x2)) / (y1 - x1) + x2;
|
@@ -50,19 +39,14 @@ const validate = (particleCount, duration, colors, particleSize, force, stageHei
|
|
50
39
|
!assertPositiveInteger(particleSize, 'particleSize') ||
|
51
40
|
!assertPositiveInteger(force, 'force') ||
|
52
41
|
!assertPositiveInteger(stageHeight, 'floorHeight') ||
|
53
|
-
!assertPositiveInteger(stageWidth, 'floorWidth'))
|
42
|
+
!assertPositiveInteger(stageWidth, 'floorWidth'))
|
54
43
|
return false;
|
55
|
-
|
56
|
-
if (!isUndefined(particlesShape) &&
|
57
|
-
!['mix', 'circles', 'rectangles'].includes(particlesShape)) {
|
44
|
+
if (!isUndefined(particlesShape) && !['mix', 'circles', 'rectangles'].includes(particlesShape))
|
58
45
|
return error('particlesShape should be either "mix" or "circles" or "rectangle"');
|
59
|
-
|
60
|
-
if (!isUndefined(colors) && !Array.isArray(colors)) {
|
46
|
+
if (!isUndefined(colors) && !Array.isArray(colors))
|
61
47
|
return error('colors must be an array of strings');
|
62
|
-
|
63
|
-
if (force > 1) {
|
48
|
+
if (force > 1)
|
64
49
|
return error('force must be within 0 and 1');
|
65
|
-
}
|
66
50
|
return true;
|
67
51
|
};
|
68
52
|
</script>
|
@@ -79,7 +63,7 @@ const validate = (particleCount, duration, colors, particleSize, force, stageHei
|
|
79
63
|
* <ConfettiExplosion particleCount={200} />
|
80
64
|
* ```
|
81
65
|
*/
|
82
|
-
export let particleCount =
|
66
|
+
export let particleCount = 150;
|
83
67
|
/**
|
84
68
|
* Size of the confetti particles in pixels
|
85
69
|
*
|
@@ -91,7 +75,7 @@ export let particleCount = PARTICLE_COUNT;
|
|
91
75
|
* <ConfettiExplosion particleSize={20} />
|
92
76
|
* ```
|
93
77
|
*/
|
94
|
-
export let particleSize =
|
78
|
+
export let particleSize = 12;
|
95
79
|
/**
|
96
80
|
* Duration of the animation in milliseconds
|
97
81
|
*
|
@@ -103,7 +87,7 @@ export let particleSize = SIZE;
|
|
103
87
|
* <ConfettiExplosion duration={5000} />
|
104
88
|
* ```
|
105
89
|
*/
|
106
|
-
export let duration =
|
90
|
+
export let duration = 3500;
|
107
91
|
/**
|
108
92
|
* Shape of particles to use. Can be `mix`, `circles` or `rectangles`
|
109
93
|
*
|
@@ -138,7 +122,7 @@ export let particlesShape = 'mix';
|
|
138
122
|
* <ConfettiExplosion colors={['var(--yellow)', 'var(--red)', '#2E3191', '#41BBC7']} />
|
139
123
|
* ```
|
140
124
|
*/
|
141
|
-
export let colors =
|
125
|
+
export let colors = ['#FFC700', '#FF0000', '#2E3191', '#41BBC7'];
|
142
126
|
/**
|
143
127
|
* Force of the confetti particles. Between 0 and 1. 0 is no force, 1 is maximum force.
|
144
128
|
*
|
@@ -150,7 +134,7 @@ export let colors = COLORS;
|
|
150
134
|
* <ConfettiExplosion force={0.8} />
|
151
135
|
* ```
|
152
136
|
*/
|
153
|
-
export let force =
|
137
|
+
export let force = 0.5;
|
154
138
|
/**
|
155
139
|
* Height of the stage in pixels. Confetti will only fall within this height.
|
156
140
|
*
|
@@ -162,7 +146,7 @@ export let force = FORCE;
|
|
162
146
|
* <ConfettiExplosion floorHeight={500} />
|
163
147
|
* ```
|
164
148
|
*/
|
165
|
-
export let stageHeight =
|
149
|
+
export let stageHeight = 800;
|
166
150
|
/**
|
167
151
|
* Width of the stage in pixels. Confetti will only fall within this width.
|
168
152
|
*
|
@@ -174,7 +158,7 @@ export let stageHeight = FLOOR_HEIGHT;
|
|
174
158
|
* <ConfettiExplosion floorWidth={1000} />
|
175
159
|
* ```
|
176
160
|
*/
|
177
|
-
export let stageWidth =
|
161
|
+
export let stageWidth = 1600;
|
178
162
|
/**
|
179
163
|
* Whether or not destroy all confetti nodes after the `duration` period has passed. By default it destroys all nodes, to preserve memory.
|
180
164
|
*
|
@@ -190,12 +174,7 @@ export let shouldDestroyAfterDone = true;
|
|
190
174
|
let isVisible = true;
|
191
175
|
$: particles = createParticles(particleCount, colors);
|
192
176
|
$: isValid = validate(particleCount, duration, colors, particleSize, force, stageHeight, stageWidth, particlesShape);
|
193
|
-
onMount(
|
194
|
-
await waitFor(duration);
|
195
|
-
if (shouldDestroyAfterDone) {
|
196
|
-
isVisible = false;
|
197
|
-
}
|
198
|
-
});
|
177
|
+
onMount(() => setTimeout(() => shouldDestroyAfterDone && (isVisible = false), duration));
|
199
178
|
function confettiStyles(node, degree) {
|
200
179
|
// Crazy calculations for generating styles
|
201
180
|
const rotationTransform = mathRound(random() * (POSSIBLE_ROTATION_TRANSFORMS - 1));
|
@@ -268,8 +247,7 @@ function confettiStyles(node, degree) {
|
|
268
247
|
}
|
269
248
|
.particle div {
|
270
249
|
position: absolute;
|
271
|
-
|
272
|
-
left: 0;
|
250
|
+
inset: 0;
|
273
251
|
animation: y-axis var(--duration-chaos) forwards cubic-bezier(var(--y1), var(--y2), var(--y3), var(--y4));
|
274
252
|
width: var(--width);
|
275
253
|
height: var(--height);
|
package/README.md
CHANGED
@@ -6,7 +6,7 @@ Let's party 🎊🎊 with Svelte! svelte-confetti-explosion allows you to show a
|
|
6
6
|
|
7
7
|
## Features
|
8
8
|
|
9
|
-
- 🤏 Tiny -
|
9
|
+
- 🤏 Tiny - 1.95KB min+br.
|
10
10
|
- 🐇 Simple - Quite simple to use, and effectively no-config required!
|
11
11
|
- 🧙♀️ Elegant - Use a Svelte component rather than setting things up in `onMount` hook.
|
12
12
|
- 🗃️ Highly customizable - Offers tons of options that you can modify to get different behaviors.
|