beautiful-backgrounds 0.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,17 @@
1
+ export default abstract class BeautifulBackground extends HTMLElement {
2
+ protected shadowR: ShadowRoot;
3
+ protected width: number;
4
+ protected height: number;
5
+ protected canvas: HTMLCanvasElement;
6
+ protected ctx: CanvasRenderingContext2D;
7
+ private resizeDebounceTimer;
8
+ constructor();
9
+ protected abstract connectedCallback(): void;
10
+ protected abstract disconnectedCallback(): void;
11
+ protected abstract initialize(): void;
12
+ protected debouncedResizeCanvas(): void;
13
+ protected resizeCanvas(): void;
14
+ protected setSize(): void;
15
+ protected parseAttributeToFloat(attr: string, prefix?: string): number;
16
+ protected parseAttributeToInt(attr: string, prefix?: string): number;
17
+ }
@@ -0,0 +1,40 @@
1
+ export default class BeautifulBackground extends HTMLElement {
2
+ constructor() {
3
+ super();
4
+ this.shadowR = this.attachShadow({ mode: 'open' });
5
+ this.shadowR.innerHTML = `
6
+ <style>
7
+ :host {
8
+ display: flex;
9
+ }
10
+ </style>
11
+ `;
12
+ this.resizeDebounceTimer = null;
13
+ this.canvas = document.createElement('canvas');
14
+ this.ctx = this.canvas.getContext('2d');
15
+ }
16
+ debouncedResizeCanvas() {
17
+ clearTimeout(this.resizeDebounceTimer);
18
+ this.resizeDebounceTimer = setTimeout(() => {
19
+ this.resizeCanvas();
20
+ }, 1000); // Adjust the debounce time as needed
21
+ }
22
+ resizeCanvas() {
23
+ this.setSize();
24
+ this.initialize();
25
+ }
26
+ setSize() {
27
+ this.width =
28
+ Number.parseInt(this.getAttribute('data-width')) || this.parentElement.clientWidth;
29
+ this.height =
30
+ Number.parseInt(this.getAttribute('data-height')) || this.parentElement.clientHeight;
31
+ this.canvas.width = this.width;
32
+ this.canvas.height = this.height;
33
+ }
34
+ parseAttributeToFloat(attr, prefix = 'data') {
35
+ return Number.parseFloat(this.getAttribute(`${prefix}-${attr}`));
36
+ }
37
+ parseAttributeToInt(attr, prefix = 'data') {
38
+ return Number.parseInt(this.getAttribute(`${prefix}-${attr}`));
39
+ }
40
+ }
@@ -0,0 +1,14 @@
1
+ import BB from './BeautifulBackground';
2
+ import Star from './particles/Star';
3
+ export declare class BbStarField extends BB {
4
+ private numStars;
5
+ private trailOpacity;
6
+ private stars;
7
+ private animationFrameId;
8
+ constructor();
9
+ protected connectedCallback(): void;
10
+ protected disconnectedCallback(): void;
11
+ protected initialize(): void;
12
+ protected startAnimation(): void;
13
+ protected createStar(): Star;
14
+ }
@@ -0,0 +1,67 @@
1
+ import BB from './BeautifulBackground';
2
+ import Star from './particles/Star';
3
+ import { getRandomFloat } from '../utils/number';
4
+ import { randomColor } from '../utils/color';
5
+ export class BbStarField extends BB {
6
+ constructor() {
7
+ super();
8
+ }
9
+ connectedCallback() {
10
+ this.shadowR.appendChild(this.canvas);
11
+ this.resizeCanvas();
12
+ this.initialize();
13
+ this.startAnimation();
14
+ window.addEventListener('resize', this.debouncedResizeCanvas.bind(this));
15
+ }
16
+ disconnectedCallback() {
17
+ window.removeEventListener('resize', this.debouncedResizeCanvas.bind(this));
18
+ if (this.animationFrameId !== null) {
19
+ window.cancelAnimationFrame(this.animationFrameId);
20
+ }
21
+ }
22
+ initialize() {
23
+ this.stars = [];
24
+ this.numStars = this.parseAttributeToFloat('num-stars') || 100;
25
+ this.trailOpacity = 0.1;
26
+ this.stars = [];
27
+ this.animationFrameId = null;
28
+ for (let i = 0; i < this.numStars; i++) {
29
+ this.stars.push(this.createStar());
30
+ }
31
+ }
32
+ startAnimation() {
33
+ const animate = () => {
34
+ this.ctx.fillStyle = `rgba(0, 0, 0, ${this.trailOpacity})`;
35
+ this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
36
+ this.stars = this.stars.filter((star) => !star.isDead());
37
+ while (this.stars.length < this.numStars) {
38
+ this.stars.push(this.createStar());
39
+ }
40
+ this.stars.forEach((star) => {
41
+ star.update(this.canvas.width / 2, this.canvas.height / 2);
42
+ star.draw(this.ctx);
43
+ });
44
+ this.animationFrameId = requestAnimationFrame(animate);
45
+ };
46
+ this.animationFrameId = requestAnimationFrame(animate);
47
+ }
48
+ createStar() {
49
+ const minOrbitRadius = this.parseAttributeToFloat('orbit-radius-min') || 10;
50
+ const maxOrbitRadius = this.parseAttributeToFloat('orbit-radius-max') ||
51
+ Math.max(this.width, this.height) / 2;
52
+ const orbitRange = maxOrbitRadius - minOrbitRadius;
53
+ const size = getRandomFloat(this.parseAttributeToInt('star-size-min') || 0.1, this.parseAttributeToInt('star-size-max') || 1.5);
54
+ const speed = getRandomFloat(this.parseAttributeToInt('star-speed-min') || 0.001, this.parseAttributeToInt('star-speed-max') || 0.003);
55
+ const lifespan = getRandomFloat(this.parseAttributeToInt('star-lifespan-min') || 1000, this.parseAttributeToInt('star-lifespan-max') || 10000);
56
+ const color = randomColor([
57
+ this.parseAttributeToFloat('star-color-hue-start') || 30,
58
+ this.parseAttributeToFloat('star-color-hue-end') || 75
59
+ ]);
60
+ const angle = getRandomFloat(0, Math.PI * 2);
61
+ const orbitRadius = Math.sqrt(Math.random()) * orbitRange + minOrbitRadius;
62
+ return new Star(size, speed, lifespan, color, orbitRadius, angle);
63
+ }
64
+ }
65
+ if (!window.customElements.get('bb-star-field')) {
66
+ window.customElements.define('bb-star-field', BbStarField);
67
+ }
@@ -0,0 +1,19 @@
1
+ export default class Star {
2
+ orbitRadius: number;
3
+ radius: number;
4
+ speed: number;
5
+ angle: number;
6
+ color: string;
7
+ born: number;
8
+ lifespan: number;
9
+ size: number;
10
+ x: number;
11
+ y: number;
12
+ fadeInDuration: number;
13
+ fadeOutDuration: number;
14
+ constructor(size?: number, speed?: number, lifespan?: number, color?: string, orbitRadius?: number, angle?: number);
15
+ update(centerX: number, centerY: number): void;
16
+ draw(ctx: CanvasRenderingContext2D): void;
17
+ isDead(): boolean;
18
+ private calculateOpacity;
19
+ }
@@ -0,0 +1,45 @@
1
+ export default class Star {
2
+ constructor(size = 1, speed = 0.001, lifespan = 1000, color = '#fff', orbitRadius = 100, angle = 0) {
3
+ this.size = size;
4
+ this.speed = speed;
5
+ this.lifespan = lifespan;
6
+ this.color = color;
7
+ this.orbitRadius = orbitRadius;
8
+ this.angle = angle;
9
+ this.fadeInDuration = this.lifespan * 0.25;
10
+ this.fadeOutDuration = this.lifespan * 0.5;
11
+ this.born = Date.now();
12
+ this.x = 0;
13
+ this.y = 0;
14
+ }
15
+ update(centerX, centerY) {
16
+ this.angle += this.speed;
17
+ this.x = centerX + Math.cos(this.angle) * this.orbitRadius;
18
+ this.y = centerY + Math.sin(this.angle) * this.orbitRadius;
19
+ }
20
+ draw(ctx) {
21
+ const opacity = this.calculateOpacity();
22
+ ctx.beginPath();
23
+ ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
24
+ ctx.fillStyle = this.color;
25
+ ctx.globalAlpha = opacity;
26
+ ctx.shadowBlur = 10;
27
+ ctx.shadowColor = this.color;
28
+ ctx.fill();
29
+ ctx.globalAlpha = 1;
30
+ ctx.shadowBlur = 0;
31
+ }
32
+ isDead() {
33
+ return Date.now() - this.born > this.lifespan;
34
+ }
35
+ calculateOpacity() {
36
+ const age = Date.now() - this.born;
37
+ if (age < this.fadeInDuration) {
38
+ return age / this.fadeInDuration; // Fade in
39
+ }
40
+ else if (age > this.lifespan - this.fadeOutDuration) {
41
+ return (this.lifespan - age) / this.fadeOutDuration; // Fade out
42
+ }
43
+ return 1; // Fully visible
44
+ }
45
+ }
@@ -0,0 +1 @@
1
+ export { BbStarField } from './components/StarField';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { BbStarField } from './components/StarField';
@@ -0,0 +1 @@
1
+ export declare function randomColor(hRange?: number[], sRange?: number[], lRange?: number[]): string;
@@ -0,0 +1,7 @@
1
+ import { getRandomInt } from './number';
2
+ export function randomColor(hRange = [0, 360], sRange = [0, 100], lRange = [40, 60]) {
3
+ const h = getRandomInt(hRange[0], hRange[1]);
4
+ const s = getRandomInt(sRange[0], sRange[1]);
5
+ const l = getRandomInt(lRange[0], lRange[1]);
6
+ return `hsl(${h}, ${s}%, ${l}%)`;
7
+ }
@@ -0,0 +1,3 @@
1
+ export declare function getRandomFloat(min: number, max: number): number;
2
+ export declare function getRandomInt(min: number, max: number): number;
3
+ export declare function interpolate(value: number, domainStart: number, domainEnd: number, rangeStart: number, rangeEnd: number): number;
@@ -0,0 +1,19 @@
1
+ export function getRandomFloat(min, max) {
2
+ return Math.random() * (max - min) + min;
3
+ }
4
+ export function getRandomInt(min, max) {
5
+ min = Math.ceil(min);
6
+ max = Math.floor(max);
7
+ return Math.floor(Math.random() * (max - min + 1) + min);
8
+ }
9
+ export function interpolate(value, domainStart, domainEnd, rangeStart, rangeEnd) {
10
+ // Ensure the value is within the domain
11
+ if (value < domainStart)
12
+ value = domainStart;
13
+ if (value > domainEnd)
14
+ value = domainEnd;
15
+ // Normalize the value within the domain
16
+ const normalized = (value - domainStart) / (domainEnd - domainStart);
17
+ // Scale the normalized value to the range and return it
18
+ return normalized * (rangeEnd - rangeStart) + rangeStart;
19
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "beautiful-backgrounds",
3
+ "version": "0.0.1",
4
+ "author": "Niekes",
5
+ "description": "A helper library to help me build amazing web apps",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "scripts": {
10
+ "clean": "rm -rf dist/components dist/utils/ && rm -f dist/index.js dist/index.d.ts",
11
+ "build": "npm run clean && npx tsc",
12
+ "watch": "npm run clean && npx tsc --watch",
13
+ "release": "npm run clean && npm run build && git push --follow-tags && npm publish"
14
+ },
15
+ "files": [
16
+ "dist/"
17
+ ],
18
+ "devDependencies": {
19
+ "typescript": "5.3.2"
20
+ }
21
+ }