ngx-sparklefall
Version:
Angular directive and component for SparkleFall - Beautiful falling sparkle animations
188 lines (187 loc) • 6.8 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Directive, ElementRef, Input } from '@angular/core';
import SparkleFallLib from 'sparklefall';
let SparkleFallDirective = class SparkleFallDirective {
elementRef;
sparkleFallEnabled = true;
sparkleFallInterval = 800;
sparkleFallDuration = 5000;
sparkleFallSparkles = ['✨', '⭐', '💫', '🌟'];
sparkleFallColors = null;
sparkleFallMinSize = 10;
sparkleFallMaxSize = 30;
sparkleFallMinDuration = 2;
sparkleFallMaxDuration = 5;
sparkleFallWind = 0;
sparkleFallSpin = true;
sparkleFallMaxSparkles = 50;
sparkleFallAutoStart = true;
sparkleFallZIndex = 9999;
sparkleInstance;
constructor(elementRef) {
this.elementRef = elementRef;
}
ngOnInit() {
if (this.sparkleFallEnabled) {
this.initializeSparkles();
}
}
ngOnChanges(changes) {
// Handle enabled state change
if (changes['sparkleFallEnabled']) {
if (this.sparkleFallEnabled && !this.sparkleInstance) {
this.initializeSparkles();
}
else if (!this.sparkleFallEnabled && this.sparkleInstance) {
this.destroySparkles();
}
}
// Update configuration for other changes
if (this.sparkleInstance && !changes['sparkleFallEnabled']) {
const config = {};
if (changes['sparkleFallInterval'])
config.interval = this.sparkleFallInterval;
if (changes['sparkleFallWind'])
config.wind = this.sparkleFallWind;
if (changes['sparkleFallMaxSparkles'])
config.maxSparkles = this.sparkleFallMaxSparkles;
if (changes['sparkleFallMinSize'])
config.minSize = this.sparkleFallMinSize;
if (changes['sparkleFallMaxSize'])
config.maxSize = this.sparkleFallMaxSize;
if (changes['sparkleFallMinDuration'])
config.minDuration = this.sparkleFallMinDuration;
if (changes['sparkleFallMaxDuration'])
config.maxDuration = this.sparkleFallMaxDuration;
if (changes['sparkleFallSpin'])
config.spin = this.sparkleFallSpin;
if (Object.keys(config).length > 0) {
this.sparkleInstance.updateConfig(config);
}
}
}
ngOnDestroy() {
this.destroySparkles();
}
initializeSparkles() {
// Ensure element has relative positioning
const element = this.elementRef.nativeElement;
if (getComputedStyle(element).position === 'static') {
element.style.position = 'relative';
}
this.sparkleInstance = new SparkleFallLib({
container: element,
interval: this.sparkleFallInterval,
duration: this.sparkleFallDuration,
sparkles: this.sparkleFallSparkles,
colors: this.sparkleFallColors,
minSize: this.sparkleFallMinSize,
maxSize: this.sparkleFallMaxSize,
minDuration: this.sparkleFallMinDuration,
maxDuration: this.sparkleFallMaxDuration,
wind: this.sparkleFallWind,
spin: this.sparkleFallSpin,
maxSparkles: this.sparkleFallMaxSparkles,
autoStart: this.sparkleFallAutoStart,
zIndex: this.sparkleFallZIndex
});
}
destroySparkles() {
if (this.sparkleInstance) {
this.sparkleInstance.destroy();
this.sparkleInstance = null;
}
}
start() {
if (this.sparkleInstance) {
this.sparkleInstance.start();
}
}
stop() {
if (this.sparkleInstance) {
this.sparkleInstance.stop();
}
}
clear() {
if (this.sparkleInstance) {
this.sparkleInstance.clear();
}
}
burst(count = 10) {
if (this.sparkleInstance) {
this.sparkleInstance.burst(count);
}
}
};
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallEnabled", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallInterval", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallDuration", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], SparkleFallDirective.prototype, "sparkleFallSparkles", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallColors", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallMinSize", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallMaxSize", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallMinDuration", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallMaxDuration", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallWind", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallSpin", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallMaxSparkles", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallAutoStart", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], SparkleFallDirective.prototype, "sparkleFallZIndex", void 0);
SparkleFallDirective = __decorate([
Directive({
selector: '[sparkleFall]',
standalone: true
}),
__metadata("design:paramtypes", [ElementRef])
], SparkleFallDirective);
export { SparkleFallDirective };