UNPKG

13.4 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.confetti = void 0;
13const ConfettiOptions_1 = require("./ConfettiOptions");
14const tsparticles_updater_angle_1 = require("tsparticles-updater-angle");
15const tsparticles_move_base_1 = require("tsparticles-move-base");
16const tsparticles_shape_cards_1 = require("tsparticles-shape-cards");
17const tsparticles_shape_circle_1 = require("tsparticles-shape-circle");
18const tsparticles_updater_color_1 = require("tsparticles-updater-color");
19const tsparticles_plugin_emitters_1 = require("tsparticles-plugin-emitters");
20const tsparticles_shape_heart_1 = require("tsparticles-shape-heart");
21const tsparticles_shape_image_1 = require("tsparticles-shape-image");
22const tsparticles_updater_life_1 = require("tsparticles-updater-life");
23const tsparticles_plugin_motion_1 = require("tsparticles-plugin-motion");
24const tsparticles_updater_opacity_1 = require("tsparticles-updater-opacity");
25const tsparticles_updater_out_modes_1 = require("tsparticles-updater-out-modes");
26const tsparticles_shape_polygon_1 = require("tsparticles-shape-polygon");
27const tsparticles_updater_roll_1 = require("tsparticles-updater-roll");
28const tsparticles_updater_size_1 = require("tsparticles-updater-size");
29const tsparticles_shape_square_1 = require("tsparticles-shape-square");
30const tsparticles_shape_star_1 = require("tsparticles-shape-star");
31const tsparticles_shape_text_1 = require("tsparticles-shape-text");
32const tsparticles_updater_tilt_1 = require("tsparticles-updater-tilt");
33const tsparticles_updater_wobble_1 = require("tsparticles-updater-wobble");
34const tsparticles_engine_1 = require("tsparticles-engine");
35let initialized = false;
36let initializing = false;
37const ids = new Map();
38function initPlugins() {
39 return __awaiter(this, void 0, void 0, function* () {
40 if (initialized) {
41 return;
42 }
43 if (initializing) {
44 return new Promise((resolve) => {
45 const interval = setInterval(() => {
46 if (initialized) {
47 clearInterval(interval);
48 resolve();
49 }
50 }, 100);
51 });
52 }
53 initializing = true;
54 yield (0, tsparticles_move_base_1.loadBaseMover)(tsparticles_engine_1.tsParticles);
55 yield (0, tsparticles_plugin_emitters_1.loadEmittersPlugin)(tsparticles_engine_1.tsParticles);
56 yield (0, tsparticles_plugin_motion_1.loadMotionPlugin)(tsparticles_engine_1.tsParticles);
57 yield (0, tsparticles_shape_cards_1.loadCardsShape)(tsparticles_engine_1.tsParticles);
58 yield (0, tsparticles_shape_circle_1.loadCircleShape)(tsparticles_engine_1.tsParticles);
59 yield (0, tsparticles_shape_heart_1.loadHeartShape)(tsparticles_engine_1.tsParticles);
60 yield (0, tsparticles_shape_image_1.loadImageShape)(tsparticles_engine_1.tsParticles);
61 yield (0, tsparticles_shape_polygon_1.loadPolygonShape)(tsparticles_engine_1.tsParticles);
62 yield (0, tsparticles_shape_square_1.loadSquareShape)(tsparticles_engine_1.tsParticles);
63 yield (0, tsparticles_shape_star_1.loadStarShape)(tsparticles_engine_1.tsParticles);
64 yield (0, tsparticles_shape_text_1.loadTextShape)(tsparticles_engine_1.tsParticles);
65 yield (0, tsparticles_updater_angle_1.loadAngleUpdater)(tsparticles_engine_1.tsParticles);
66 yield (0, tsparticles_updater_color_1.loadColorUpdater)(tsparticles_engine_1.tsParticles);
67 yield (0, tsparticles_updater_life_1.loadLifeUpdater)(tsparticles_engine_1.tsParticles);
68 yield (0, tsparticles_updater_opacity_1.loadOpacityUpdater)(tsparticles_engine_1.tsParticles);
69 yield (0, tsparticles_updater_out_modes_1.loadOutModesUpdater)(tsparticles_engine_1.tsParticles);
70 yield (0, tsparticles_updater_roll_1.loadRollUpdater)(tsparticles_engine_1.tsParticles);
71 yield (0, tsparticles_updater_size_1.loadSizeUpdater)(tsparticles_engine_1.tsParticles);
72 yield (0, tsparticles_updater_tilt_1.loadTiltUpdater)(tsparticles_engine_1.tsParticles);
73 yield (0, tsparticles_updater_wobble_1.loadWobbleUpdater)(tsparticles_engine_1.tsParticles);
74 initializing = false;
75 initialized = true;
76 });
77}
78function setConfetti(params) {
79 return __awaiter(this, void 0, void 0, function* () {
80 const actualOptions = new ConfettiOptions_1.ConfettiOptions();
81 actualOptions.load(params.options);
82 let container;
83 if (ids.has(params.id)) {
84 container = ids.get(params.id);
85 if (container && !container.destroyed) {
86 const alias = container;
87 if (alias.addEmitter) {
88 alias.addEmitter({
89 startCount: actualOptions.count,
90 position: actualOptions.position,
91 size: {
92 width: 0,
93 height: 0,
94 },
95 rate: {
96 delay: 0,
97 quantity: 0,
98 },
99 life: {
100 duration: 0.1,
101 count: 1,
102 },
103 particles: {
104 color: {
105 value: actualOptions.colors,
106 },
107 shape: {
108 type: actualOptions.shapes,
109 options: actualOptions.shapeOptions,
110 },
111 size: {
112 value: 5 * actualOptions.scalar,
113 },
114 life: {
115 duration: {
116 value: actualOptions.ticks / 60,
117 },
118 },
119 move: {
120 angle: {
121 value: actualOptions.spread,
122 offset: 0,
123 },
124 drift: {
125 min: -actualOptions.drift,
126 max: actualOptions.drift,
127 },
128 gravity: {
129 acceleration: actualOptions.gravity * 9.81,
130 },
131 speed: actualOptions.startVelocity * 3,
132 decay: 1 - actualOptions.decay,
133 direction: -actualOptions.angle,
134 },
135 },
136 });
137 return;
138 }
139 }
140 }
141 const particlesOptions = {
142 fullScreen: {
143 enable: !params.canvas,
144 zIndex: actualOptions.zIndex,
145 },
146 fpsLimit: 120,
147 particles: {
148 number: {
149 value: 0,
150 },
151 color: {
152 value: actualOptions.colors,
153 },
154 shape: {
155 type: actualOptions.shapes,
156 options: actualOptions.shapeOptions,
157 },
158 opacity: {
159 value: { min: 0, max: 1 },
160 animation: {
161 enable: true,
162 speed: 0.5,
163 startValue: "max",
164 destroy: "min",
165 },
166 },
167 size: {
168 value: 5 * actualOptions.scalar,
169 },
170 links: {
171 enable: false,
172 },
173 life: {
174 duration: {
175 sync: true,
176 value: actualOptions.ticks / 60,
177 },
178 count: 1,
179 },
180 move: {
181 angle: {
182 value: actualOptions.spread,
183 offset: 0,
184 },
185 drift: {
186 min: -actualOptions.drift,
187 max: actualOptions.drift,
188 },
189 enable: true,
190 gravity: {
191 enable: true,
192 acceleration: actualOptions.gravity * 9.81,
193 },
194 speed: actualOptions.startVelocity * 3,
195 decay: 1 - actualOptions.decay,
196 direction: -actualOptions.angle,
197 random: true,
198 straight: false,
199 outModes: {
200 default: "none",
201 bottom: "destroy",
202 },
203 },
204 rotate: {
205 value: {
206 min: 0,
207 max: 360,
208 },
209 direction: "random",
210 animation: {
211 enable: true,
212 speed: 60,
213 },
214 },
215 tilt: {
216 direction: "random",
217 enable: true,
218 value: {
219 min: 0,
220 max: 360,
221 },
222 animation: {
223 enable: true,
224 speed: 60,
225 },
226 },
227 roll: {
228 darken: {
229 enable: true,
230 value: 25,
231 },
232 enable: true,
233 speed: {
234 min: 15,
235 max: 25,
236 },
237 },
238 wobble: {
239 distance: 30,
240 enable: true,
241 speed: {
242 min: -15,
243 max: 15,
244 },
245 },
246 },
247 detectRetina: true,
248 motion: {
249 disable: actualOptions.disableForReducedMotion,
250 },
251 emitters: {
252 name: "confetti",
253 startCount: actualOptions.count,
254 position: actualOptions.position,
255 size: {
256 width: 0,
257 height: 0,
258 },
259 rate: {
260 delay: 0,
261 quantity: 0,
262 },
263 life: {
264 duration: 0.1,
265 count: 1,
266 },
267 },
268 };
269 if (params.id) {
270 container = yield tsparticles_engine_1.tsParticles.load(params.id, particlesOptions);
271 }
272 else if (params.canvas) {
273 container = yield tsparticles_engine_1.tsParticles.set(params.id, params.canvas, particlesOptions);
274 }
275 ids.set(params.id, container);
276 return container;
277 });
278}
279function confetti(idOrOptions, confettiOptions) {
280 return __awaiter(this, void 0, void 0, function* () {
281 yield initPlugins();
282 let options;
283 let id;
284 if (typeof idOrOptions === "string") {
285 id = idOrOptions;
286 options = confettiOptions !== null && confettiOptions !== void 0 ? confettiOptions : {};
287 }
288 else {
289 id = "confetti";
290 options = idOrOptions;
291 }
292 return setConfetti({
293 id,
294 options,
295 });
296 });
297}
298exports.confetti = confetti;
299confetti.create = (canvas, options) => __awaiter(void 0, void 0, void 0, function* () {
300 if (!canvas) {
301 return confetti;
302 }
303 yield initPlugins();
304 const id = canvas.getAttribute("id") || "confetti";
305 canvas.setAttribute("id", id);
306 return (idOrOptions, confettiOptions) => __awaiter(void 0, void 0, void 0, function* () {
307 let subOptions;
308 let subId;
309 if (typeof idOrOptions === "string") {
310 subId = idOrOptions;
311 subOptions = confettiOptions !== null && confettiOptions !== void 0 ? confettiOptions : options;
312 }
313 else {
314 subId = id;
315 subOptions = idOrOptions;
316 }
317 return setConfetti({
318 id: subId,
319 canvas,
320 options: subOptions,
321 });
322 });
323});