UNPKG

ng-cw-v12

Version:

Angular UI component library

161 lines (156 loc) 8.43 kB
import * as i0 from '@angular/core'; import { Component, Input, NgModule } from '@angular/core'; import * as i1 from '@angular/common'; import { CommonModule } from '@angular/common'; class SparklesTextComponent { constructor() { /** 展示的文本内容 */ this._text = ''; /** 闪烁点的数量 */ this.ncCount = 10; /** 闪烁点的颜色数组 */ this.ncColors = ['#9E7AFF', '#FE8BBB', '#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4']; /** 闪烁循环时间(秒) */ this.ncDuration = 0.8; /** 闪烁点的最大尺寸 */ this.ncSize = 21; this.sparkles = []; } set ncText(value) { this._text = value; this.cleanup(); setTimeout(() => { this.initializeSparkles(); this.startAnimation(); }); } get ncText() { return this._text; } ngOnInit() { } ngOnDestroy() { this.cleanup(); } initializeSparkles() { this.sparkles = Array.from({ length: this.ncCount }, () => this.generateSparkle()); } generateSparkle() { const x = `${Math.random() * 100}%`; const y = `${Math.random() * 100}%`; const color = this.ncColors[Math.floor(Math.random() * this.ncColors.length)]; const delay = Math.random() * 2; const scale = Math.random() * 1 + 0.3; const lifespan = Math.random() * 10 + 5; const id = `${x}-${y}-${Date.now()}-${Math.random()}`; const opacity = 0; const rotation = 75; const scaleValue = 0; return { id, x, y, color, delay, scale, lifespan, opacity, rotation, scaleValue }; } startAnimation() { // 更新闪烁点生命周期 this.intervalId = window.setInterval(() => { this.sparkles = this.sparkles.map(sparkle => { if (sparkle.lifespan <= 0) { return this.generateSparkle(); } else { return Object.assign(Object.assign({}, sparkle), { lifespan: sparkle.lifespan - 0.1 }); } }); }, 100); // 动画循环 - 模拟Framer Motion的关键帧动画 const animate = () => { this.sparkles = this.sparkles.map(sparkle => { const time = (Date.now() * 0.001 + sparkle.delay) % this.ncDuration; // 使用可配置的循环时间 const progress = time / this.ncDuration; // 模拟关键帧动画 [0, 1, 0] let opacity; let scaleValue; let rotation; if (progress <= 0.5) { // 前半段:0 -> 1 const t = progress * 2; opacity = t; scaleValue = t * sparkle.scale; rotation = 75 + (120 - 75) * t; } else { // 后半段:1 -> 0 const t = (progress - 0.5) * 2; opacity = 1 - t; scaleValue = (1 - t) * sparkle.scale; rotation = 120 + (150 - 120) * t; } return Object.assign(Object.assign({}, sparkle), { opacity, rotation, scaleValue }); }); this.animationFrameId = requestAnimationFrame(animate); }; animate(); } cleanup() { if (this.intervalId) { clearInterval(this.intervalId); } if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); } } getSparkleStyle(sparkle) { return { left: sparkle.x, top: sparkle.y, opacity: sparkle.opacity, transform: `scale(${sparkle.scaleValue}) rotate(${sparkle.rotation}deg)` }; } trackBySparkleId(index, sparkle) { return sparkle.id; } } SparklesTextComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: SparklesTextComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); SparklesTextComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.1.5", type: SparklesTextComponent, selector: "nc-sparkles-text", inputs: { ncText: "ncText", ncCount: "ncCount", ncColors: "ncColors", ncDuration: "ncDuration", ncSize: "ncSize" }, ngImport: i0, template: "<div class=\"sparkles-text\">\n <span class=\"sparkles-container\">\n <!-- \u95EA\u70C1\u70B9 -->\n <svg *ngFor=\"let sparkle of sparkles; trackBy: trackBySparkleId\" [ngStyle]=\"getSparkleStyle(sparkle)\"\n class=\"sparkle-svg\" [attr.width]=\"ncSize\" [attr.height]=\"ncSize\" viewBox=\"0 0 21 21\">\n <path\n d=\"M9.82531 0.843845C10.0553 0.215178 10.9446 0.215178 11.1746 0.843845L11.8618 2.72026C12.4006 4.19229 12.3916 6.39157 13.5 7.5C14.6084 8.60843 16.8077 8.59935 18.2797 9.13822L20.1561 9.82534C20.7858 10.0553 20.7858 10.9447 20.1561 11.1747L18.2797 11.8618C16.8077 12.4007 14.6084 12.3916 13.5 13.5C12.3916 14.6084 12.4006 16.8077 11.8618 18.2798L11.1746 20.1562C10.9446 20.7858 10.0553 20.7858 9.82531 20.1562L9.13819 18.2798C8.59932 16.8077 8.60843 14.6084 7.5 13.5C6.39157 12.3916 4.19225 12.4007 2.72023 11.8618L0.843814 11.1747C0.215148 10.9447 0.215148 10.0553 0.843814 9.82534L2.72023 9.13822C4.19225 8.59935 6.39157 8.60843 7.5 7.5C8.60843 6.39157 8.59932 4.19229 9.13819 2.72026L9.82531 0.843845Z\"\n [attr.fill]=\"sparkle.color\" />\n </svg>\n <!-- \u6587\u672C\u5185\u5BB9 -->\n <span class=\"sparkles-content\">{{ncText}}</span>\n </span>\n</div>", styles: [".sparkles-text{position:relative;display:inline-block}.sparkles-text .sparkles-container{position:relative;display:inline-block}.sparkles-text .sparkles-container .sparkle-svg{position:absolute;pointer-events:none;z-index:20}.sparkles-text .sparkles-container .sparkles-content{position:relative;z-index:10}\n"], directives: [{ type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: SparklesTextComponent, decorators: [{ type: Component, args: [{ selector: 'nc-sparkles-text', templateUrl: './sparkles-text.component.html', styleUrls: ['./sparkles-text.component.less'] }] }], ctorParameters: function () { return []; }, propDecorators: { ncText: [{ type: Input }], ncCount: [{ type: Input }], ncColors: [{ type: Input }], ncDuration: [{ type: Input }], ncSize: [{ type: Input }] } }); class NcSparklesTextModule { } NcSparklesTextModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcSparklesTextModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); NcSparklesTextModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcSparklesTextModule, declarations: [SparklesTextComponent], imports: [CommonModule], exports: [SparklesTextComponent] }); NcSparklesTextModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcSparklesTextModule, imports: [[ CommonModule ]] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcSparklesTextModule, decorators: [{ type: NgModule, args: [{ declarations: [ SparklesTextComponent ], imports: [ CommonModule ], exports: [ SparklesTextComponent ] }] }] }); /** * Generated bundle index. Do not edit. */ export { NcSparklesTextModule, SparklesTextComponent }; //# sourceMappingURL=ng-cw-v12-sparkles-text.js.map