@sublimee/backgrounds 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Juan Figueroa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,40 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CSSProperties } from 'react';
3
+
4
+ interface StarfieldBackgroundProps {
5
+ /** Canvas background color */
6
+ bgColor?: string;
7
+ /** Base star RGB color */
8
+ starColor?: string;
9
+ /** Secondary star RGB color (mixed in) */
10
+ accentColor?: string;
11
+ /** 0-1, fraction of accent stars */
12
+ accentRatio?: number;
13
+ /** Total number of stars */
14
+ starCount?: number;
15
+ /** Maximum drift speed (px/frame) */
16
+ maxSpeed?: number;
17
+ /** Smallest star radius */
18
+ minRadius?: number;
19
+ /** Largest star radius */
20
+ maxRadius?: number;
21
+ /** Enable opacity oscillation */
22
+ twinkle?: boolean;
23
+ /** Oscillation speed multiplier */
24
+ twinkleSpeed?: number;
25
+ /** Enable parallax size layers */
26
+ depth?: boolean;
27
+ /** Enable random shooting stars */
28
+ shootingStars?: boolean;
29
+ /** 0-1, spawn chance per frame */
30
+ shootingStarFrequency?: number;
31
+ /** Extra Tailwind / CSS classes */
32
+ className?: string;
33
+ /** Extra inline styles */
34
+ style?: CSSProperties;
35
+ /** Target frame rate (0 = uncapped) */
36
+ fps?: number;
37
+ }
38
+ declare function StarfieldBackground({ bgColor, starColor, accentColor, accentRatio, starCount, maxSpeed, minRadius, maxRadius, twinkle, twinkleSpeed, depth, shootingStars, shootingStarFrequency, className, style, fps, }: StarfieldBackgroundProps): react_jsx_runtime.JSX.Element;
39
+
40
+ export { StarfieldBackground, type StarfieldBackgroundProps };
@@ -0,0 +1,40 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CSSProperties } from 'react';
3
+
4
+ interface StarfieldBackgroundProps {
5
+ /** Canvas background color */
6
+ bgColor?: string;
7
+ /** Base star RGB color */
8
+ starColor?: string;
9
+ /** Secondary star RGB color (mixed in) */
10
+ accentColor?: string;
11
+ /** 0-1, fraction of accent stars */
12
+ accentRatio?: number;
13
+ /** Total number of stars */
14
+ starCount?: number;
15
+ /** Maximum drift speed (px/frame) */
16
+ maxSpeed?: number;
17
+ /** Smallest star radius */
18
+ minRadius?: number;
19
+ /** Largest star radius */
20
+ maxRadius?: number;
21
+ /** Enable opacity oscillation */
22
+ twinkle?: boolean;
23
+ /** Oscillation speed multiplier */
24
+ twinkleSpeed?: number;
25
+ /** Enable parallax size layers */
26
+ depth?: boolean;
27
+ /** Enable random shooting stars */
28
+ shootingStars?: boolean;
29
+ /** 0-1, spawn chance per frame */
30
+ shootingStarFrequency?: number;
31
+ /** Extra Tailwind / CSS classes */
32
+ className?: string;
33
+ /** Extra inline styles */
34
+ style?: CSSProperties;
35
+ /** Target frame rate (0 = uncapped) */
36
+ fps?: number;
37
+ }
38
+ declare function StarfieldBackground({ bgColor, starColor, accentColor, accentRatio, starCount, maxSpeed, minRadius, maxRadius, twinkle, twinkleSpeed, depth, shootingStars, shootingStarFrequency, className, style, fps, }: StarfieldBackgroundProps): react_jsx_runtime.JSX.Element;
39
+
40
+ export { StarfieldBackground, type StarfieldBackgroundProps };
package/dist/index.js ADDED
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ StarfieldBackground: () => StarfieldBackground
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/starfield-background.tsx
28
+ var import_react = require("react");
29
+ var import_jsx_runtime = require("react/jsx-runtime");
30
+ var HEX_REGEX = /^#?([a-fA-F0-9]{6})$/;
31
+ function hexToRgb(hex) {
32
+ const match = hex.trim().match(HEX_REGEX);
33
+ if (!match) {
34
+ console.warn(`[StarfieldBackground] Invalid hex color "${hex}", defaulting to white`);
35
+ return { r: 255, g: 255, b: 255 };
36
+ }
37
+ const h = match[1];
38
+ return {
39
+ r: parseInt(h.substring(0, 2), 16),
40
+ g: parseInt(h.substring(2, 4), 16),
41
+ b: parseInt(h.substring(4, 6), 16)
42
+ };
43
+ }
44
+ function lerp(a, b, t) {
45
+ return a + (b - a) * t;
46
+ }
47
+ function rand(min, max) {
48
+ return Math.random() * (max - min) + min;
49
+ }
50
+ function StarfieldBackground({
51
+ bgColor = "#000000",
52
+ starColor = "#ffffff",
53
+ accentColor = "#facc15",
54
+ accentRatio = 0.15,
55
+ starCount = 600,
56
+ maxSpeed = 0.35,
57
+ minRadius = 0.4,
58
+ maxRadius = 2.2,
59
+ twinkle = true,
60
+ twinkleSpeed = 1,
61
+ depth = true,
62
+ shootingStars = true,
63
+ shootingStarFrequency = 3e-3,
64
+ className = "",
65
+ style = {},
66
+ fps = 60
67
+ }) {
68
+ const canvasRef = (0, import_react.useRef)(null);
69
+ const animRef = (0, import_react.useRef)(0);
70
+ const starsRef = (0, import_react.useRef)([]);
71
+ const shootingRef = (0, import_react.useRef)([]);
72
+ const sizeRef = (0, import_react.useRef)({ w: 0, h: 0 });
73
+ const makeStar = (0, import_react.useCallback)(
74
+ (w, h) => {
75
+ const isAccent = Math.random() < accentRatio;
76
+ const layer = depth ? rand(0.3, 1) : 1;
77
+ return {
78
+ x: rand(0, w),
79
+ y: rand(0, h),
80
+ r: lerp(minRadius, maxRadius, layer),
81
+ vx: rand(-maxSpeed, maxSpeed) * layer,
82
+ vy: rand(-maxSpeed, maxSpeed) * layer,
83
+ opacity: rand(0.4, 1),
84
+ phase: rand(0, Math.PI * 2),
85
+ twinkleRate: rand(5e-3, 0.025) * twinkleSpeed,
86
+ color: isAccent ? accentColor : starColor,
87
+ layer
88
+ };
89
+ },
90
+ [
91
+ accentRatio,
92
+ starColor,
93
+ accentColor,
94
+ minRadius,
95
+ maxRadius,
96
+ maxSpeed,
97
+ depth,
98
+ twinkleSpeed
99
+ ]
100
+ );
101
+ const makeShootingStar = (0, import_react.useCallback)(
102
+ (w, h) => {
103
+ const angle = rand(Math.PI * 0.15, Math.PI * 0.35);
104
+ const speed = rand(6, 14);
105
+ return {
106
+ x: rand(-w * 0.1, w * 0.7),
107
+ y: rand(-h * 0.1, h * 0.3),
108
+ vx: Math.cos(angle) * speed,
109
+ vy: Math.sin(angle) * speed,
110
+ life: 1,
111
+ decay: rand(8e-3, 0.02),
112
+ length: rand(40, 120),
113
+ width: rand(1, 2.5)
114
+ };
115
+ },
116
+ []
117
+ );
118
+ (0, import_react.useEffect)(() => {
119
+ const canvas = canvasRef.current;
120
+ if (!canvas) return;
121
+ const ctx = canvas.getContext("2d");
122
+ if (!ctx) return;
123
+ const dpr = window.devicePixelRatio || 1;
124
+ const resize = () => {
125
+ const rect = canvas.parentElement?.getBoundingClientRect() ?? {
126
+ width: window.innerWidth,
127
+ height: window.innerHeight
128
+ };
129
+ const w2 = rect.width;
130
+ const h2 = rect.height;
131
+ canvas.width = w2 * dpr;
132
+ canvas.height = h2 * dpr;
133
+ canvas.style.width = `${w2}px`;
134
+ canvas.style.height = `${h2}px`;
135
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
136
+ sizeRef.current = { w: w2, h: h2 };
137
+ };
138
+ resize();
139
+ const { w, h } = sizeRef.current;
140
+ starsRef.current = Array.from({ length: starCount }, () => makeStar(w, h));
141
+ const interval = fps > 0 ? 1e3 / fps : 0;
142
+ let last = 0;
143
+ const draw = (now) => {
144
+ animRef.current = requestAnimationFrame(draw);
145
+ if (interval > 0 && now - last < interval) return;
146
+ last = now;
147
+ const { w: cw, h: ch } = sizeRef.current;
148
+ if (cw === 0 || ch === 0) return;
149
+ ctx.fillStyle = bgColor;
150
+ ctx.fillRect(0, 0, cw, ch);
151
+ const stars = starsRef.current;
152
+ for (let i = 0; i < stars.length; i++) {
153
+ const s = stars[i];
154
+ s.x += s.vx;
155
+ s.y += s.vy;
156
+ if (s.x < -4) s.x = cw + 4;
157
+ if (s.x > cw + 4) s.x = -4;
158
+ if (s.y < -4) s.y = ch + 4;
159
+ if (s.y > ch + 4) s.y = -4;
160
+ let alpha = s.opacity;
161
+ if (twinkle) {
162
+ s.phase += s.twinkleRate;
163
+ alpha = s.opacity * (0.5 + 0.5 * Math.sin(s.phase));
164
+ }
165
+ const { r, g, b } = hexToRgb(s.color);
166
+ ctx.fillStyle = `rgba(${r},${g},${b},${alpha.toFixed(3)})`;
167
+ if (s.r > 1.2) {
168
+ ctx.shadowBlur = s.r * 3;
169
+ ctx.shadowColor = `rgba(${r},${g},${b},${(alpha * 0.5).toFixed(3)})`;
170
+ } else {
171
+ ctx.shadowBlur = 0;
172
+ }
173
+ ctx.beginPath();
174
+ ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
175
+ ctx.fill();
176
+ }
177
+ ctx.shadowBlur = 0;
178
+ if (shootingStars) {
179
+ if (Math.random() < shootingStarFrequency) {
180
+ shootingRef.current.push(makeShootingStar(cw, ch));
181
+ }
182
+ const shots = shootingRef.current;
183
+ for (let i = shots.length - 1; i >= 0; i--) {
184
+ const ss = shots[i];
185
+ ss.x += ss.vx;
186
+ ss.y += ss.vy;
187
+ ss.life -= ss.decay;
188
+ if (ss.life <= 0) {
189
+ shots.splice(i, 1);
190
+ continue;
191
+ }
192
+ const tailX = ss.x - ss.vx / Math.hypot(ss.vx, ss.vy) * ss.length;
193
+ const tailY = ss.y - ss.vy / Math.hypot(ss.vx, ss.vy) * ss.length;
194
+ const grad = ctx.createLinearGradient(tailX, tailY, ss.x, ss.y);
195
+ grad.addColorStop(0, `rgba(255,255,255,0)`);
196
+ grad.addColorStop(1, `rgba(255,255,255,${ss.life.toFixed(3)})`);
197
+ ctx.strokeStyle = grad;
198
+ ctx.lineWidth = ss.width;
199
+ ctx.lineCap = "round";
200
+ ctx.beginPath();
201
+ ctx.moveTo(tailX, tailY);
202
+ ctx.lineTo(ss.x, ss.y);
203
+ ctx.stroke();
204
+ }
205
+ }
206
+ };
207
+ animRef.current = requestAnimationFrame(draw);
208
+ const ro = new ResizeObserver(() => {
209
+ resize();
210
+ const { w: nw, h: nh } = sizeRef.current;
211
+ starsRef.current.forEach((s) => {
212
+ if (s.x > nw) s.x = rand(0, nw);
213
+ if (s.y > nh) s.y = rand(0, nh);
214
+ });
215
+ });
216
+ if (canvas.parentElement) ro.observe(canvas.parentElement);
217
+ return () => {
218
+ cancelAnimationFrame(animRef.current);
219
+ ro.disconnect();
220
+ };
221
+ }, [
222
+ bgColor,
223
+ starCount,
224
+ twinkle,
225
+ shootingStars,
226
+ shootingStarFrequency,
227
+ fps,
228
+ makeStar,
229
+ makeShootingStar
230
+ ]);
231
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
232
+ "canvas",
233
+ {
234
+ ref: canvasRef,
235
+ className: `absolute inset-0 ${className}`,
236
+ style: { pointerEvents: "none", ...style },
237
+ "aria-hidden": "true"
238
+ }
239
+ );
240
+ }
241
+ // Annotate the CommonJS export names for ESM import in node:
242
+ 0 && (module.exports = {
243
+ StarfieldBackground
244
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,217 @@
1
+ // src/starfield-background.tsx
2
+ import { useRef, useEffect, useCallback } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var HEX_REGEX = /^#?([a-fA-F0-9]{6})$/;
5
+ function hexToRgb(hex) {
6
+ const match = hex.trim().match(HEX_REGEX);
7
+ if (!match) {
8
+ console.warn(`[StarfieldBackground] Invalid hex color "${hex}", defaulting to white`);
9
+ return { r: 255, g: 255, b: 255 };
10
+ }
11
+ const h = match[1];
12
+ return {
13
+ r: parseInt(h.substring(0, 2), 16),
14
+ g: parseInt(h.substring(2, 4), 16),
15
+ b: parseInt(h.substring(4, 6), 16)
16
+ };
17
+ }
18
+ function lerp(a, b, t) {
19
+ return a + (b - a) * t;
20
+ }
21
+ function rand(min, max) {
22
+ return Math.random() * (max - min) + min;
23
+ }
24
+ function StarfieldBackground({
25
+ bgColor = "#000000",
26
+ starColor = "#ffffff",
27
+ accentColor = "#facc15",
28
+ accentRatio = 0.15,
29
+ starCount = 600,
30
+ maxSpeed = 0.35,
31
+ minRadius = 0.4,
32
+ maxRadius = 2.2,
33
+ twinkle = true,
34
+ twinkleSpeed = 1,
35
+ depth = true,
36
+ shootingStars = true,
37
+ shootingStarFrequency = 3e-3,
38
+ className = "",
39
+ style = {},
40
+ fps = 60
41
+ }) {
42
+ const canvasRef = useRef(null);
43
+ const animRef = useRef(0);
44
+ const starsRef = useRef([]);
45
+ const shootingRef = useRef([]);
46
+ const sizeRef = useRef({ w: 0, h: 0 });
47
+ const makeStar = useCallback(
48
+ (w, h) => {
49
+ const isAccent = Math.random() < accentRatio;
50
+ const layer = depth ? rand(0.3, 1) : 1;
51
+ return {
52
+ x: rand(0, w),
53
+ y: rand(0, h),
54
+ r: lerp(minRadius, maxRadius, layer),
55
+ vx: rand(-maxSpeed, maxSpeed) * layer,
56
+ vy: rand(-maxSpeed, maxSpeed) * layer,
57
+ opacity: rand(0.4, 1),
58
+ phase: rand(0, Math.PI * 2),
59
+ twinkleRate: rand(5e-3, 0.025) * twinkleSpeed,
60
+ color: isAccent ? accentColor : starColor,
61
+ layer
62
+ };
63
+ },
64
+ [
65
+ accentRatio,
66
+ starColor,
67
+ accentColor,
68
+ minRadius,
69
+ maxRadius,
70
+ maxSpeed,
71
+ depth,
72
+ twinkleSpeed
73
+ ]
74
+ );
75
+ const makeShootingStar = useCallback(
76
+ (w, h) => {
77
+ const angle = rand(Math.PI * 0.15, Math.PI * 0.35);
78
+ const speed = rand(6, 14);
79
+ return {
80
+ x: rand(-w * 0.1, w * 0.7),
81
+ y: rand(-h * 0.1, h * 0.3),
82
+ vx: Math.cos(angle) * speed,
83
+ vy: Math.sin(angle) * speed,
84
+ life: 1,
85
+ decay: rand(8e-3, 0.02),
86
+ length: rand(40, 120),
87
+ width: rand(1, 2.5)
88
+ };
89
+ },
90
+ []
91
+ );
92
+ useEffect(() => {
93
+ const canvas = canvasRef.current;
94
+ if (!canvas) return;
95
+ const ctx = canvas.getContext("2d");
96
+ if (!ctx) return;
97
+ const dpr = window.devicePixelRatio || 1;
98
+ const resize = () => {
99
+ const rect = canvas.parentElement?.getBoundingClientRect() ?? {
100
+ width: window.innerWidth,
101
+ height: window.innerHeight
102
+ };
103
+ const w2 = rect.width;
104
+ const h2 = rect.height;
105
+ canvas.width = w2 * dpr;
106
+ canvas.height = h2 * dpr;
107
+ canvas.style.width = `${w2}px`;
108
+ canvas.style.height = `${h2}px`;
109
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
110
+ sizeRef.current = { w: w2, h: h2 };
111
+ };
112
+ resize();
113
+ const { w, h } = sizeRef.current;
114
+ starsRef.current = Array.from({ length: starCount }, () => makeStar(w, h));
115
+ const interval = fps > 0 ? 1e3 / fps : 0;
116
+ let last = 0;
117
+ const draw = (now) => {
118
+ animRef.current = requestAnimationFrame(draw);
119
+ if (interval > 0 && now - last < interval) return;
120
+ last = now;
121
+ const { w: cw, h: ch } = sizeRef.current;
122
+ if (cw === 0 || ch === 0) return;
123
+ ctx.fillStyle = bgColor;
124
+ ctx.fillRect(0, 0, cw, ch);
125
+ const stars = starsRef.current;
126
+ for (let i = 0; i < stars.length; i++) {
127
+ const s = stars[i];
128
+ s.x += s.vx;
129
+ s.y += s.vy;
130
+ if (s.x < -4) s.x = cw + 4;
131
+ if (s.x > cw + 4) s.x = -4;
132
+ if (s.y < -4) s.y = ch + 4;
133
+ if (s.y > ch + 4) s.y = -4;
134
+ let alpha = s.opacity;
135
+ if (twinkle) {
136
+ s.phase += s.twinkleRate;
137
+ alpha = s.opacity * (0.5 + 0.5 * Math.sin(s.phase));
138
+ }
139
+ const { r, g, b } = hexToRgb(s.color);
140
+ ctx.fillStyle = `rgba(${r},${g},${b},${alpha.toFixed(3)})`;
141
+ if (s.r > 1.2) {
142
+ ctx.shadowBlur = s.r * 3;
143
+ ctx.shadowColor = `rgba(${r},${g},${b},${(alpha * 0.5).toFixed(3)})`;
144
+ } else {
145
+ ctx.shadowBlur = 0;
146
+ }
147
+ ctx.beginPath();
148
+ ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
149
+ ctx.fill();
150
+ }
151
+ ctx.shadowBlur = 0;
152
+ if (shootingStars) {
153
+ if (Math.random() < shootingStarFrequency) {
154
+ shootingRef.current.push(makeShootingStar(cw, ch));
155
+ }
156
+ const shots = shootingRef.current;
157
+ for (let i = shots.length - 1; i >= 0; i--) {
158
+ const ss = shots[i];
159
+ ss.x += ss.vx;
160
+ ss.y += ss.vy;
161
+ ss.life -= ss.decay;
162
+ if (ss.life <= 0) {
163
+ shots.splice(i, 1);
164
+ continue;
165
+ }
166
+ const tailX = ss.x - ss.vx / Math.hypot(ss.vx, ss.vy) * ss.length;
167
+ const tailY = ss.y - ss.vy / Math.hypot(ss.vx, ss.vy) * ss.length;
168
+ const grad = ctx.createLinearGradient(tailX, tailY, ss.x, ss.y);
169
+ grad.addColorStop(0, `rgba(255,255,255,0)`);
170
+ grad.addColorStop(1, `rgba(255,255,255,${ss.life.toFixed(3)})`);
171
+ ctx.strokeStyle = grad;
172
+ ctx.lineWidth = ss.width;
173
+ ctx.lineCap = "round";
174
+ ctx.beginPath();
175
+ ctx.moveTo(tailX, tailY);
176
+ ctx.lineTo(ss.x, ss.y);
177
+ ctx.stroke();
178
+ }
179
+ }
180
+ };
181
+ animRef.current = requestAnimationFrame(draw);
182
+ const ro = new ResizeObserver(() => {
183
+ resize();
184
+ const { w: nw, h: nh } = sizeRef.current;
185
+ starsRef.current.forEach((s) => {
186
+ if (s.x > nw) s.x = rand(0, nw);
187
+ if (s.y > nh) s.y = rand(0, nh);
188
+ });
189
+ });
190
+ if (canvas.parentElement) ro.observe(canvas.parentElement);
191
+ return () => {
192
+ cancelAnimationFrame(animRef.current);
193
+ ro.disconnect();
194
+ };
195
+ }, [
196
+ bgColor,
197
+ starCount,
198
+ twinkle,
199
+ shootingStars,
200
+ shootingStarFrequency,
201
+ fps,
202
+ makeStar,
203
+ makeShootingStar
204
+ ]);
205
+ return /* @__PURE__ */ jsx(
206
+ "canvas",
207
+ {
208
+ ref: canvasRef,
209
+ className: `absolute inset-0 ${className}`,
210
+ style: { pointerEvents: "none", ...style },
211
+ "aria-hidden": "true"
212
+ }
213
+ );
214
+ }
215
+ export {
216
+ StarfieldBackground
217
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@sublimee/backgrounds",
3
+ "version": "0.1.0",
4
+ "description": "Animated background components for Sublime",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.js"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/juancrfig/sublime.git",
26
+ "directory": "packages/backgrounds"
27
+ },
28
+ "keywords": [
29
+ "react",
30
+ "background",
31
+ "animation",
32
+ "canvas",
33
+ "starfield",
34
+ "ui",
35
+ "components"
36
+ ],
37
+ "author": "Sublime Team",
38
+ "license": "MIT",
39
+ "peerDependencies": {
40
+ "react": "^19.0.0",
41
+ "react-dom": "^19.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/react": "^19",
45
+ "@types/react-dom": "^19",
46
+ "tsup": "^8",
47
+ "typescript": "^5"
48
+ },
49
+ "scripts": {
50
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
51
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { StarfieldBackground } from "./starfield-background";
2
+ export type { StarfieldBackgroundProps } from "./starfield-background";
@@ -0,0 +1,317 @@
1
+ "use client";
2
+
3
+ import { useRef, useEffect, useCallback, CSSProperties } from "react";
4
+
5
+ export interface StarfieldBackgroundProps {
6
+ /** Canvas background color */
7
+ bgColor?: string;
8
+ /** Base star RGB color */
9
+ starColor?: string;
10
+ /** Secondary star RGB color (mixed in) */
11
+ accentColor?: string;
12
+ /** 0-1, fraction of accent stars */
13
+ accentRatio?: number;
14
+ /** Total number of stars */
15
+ starCount?: number;
16
+ /** Maximum drift speed (px/frame) */
17
+ maxSpeed?: number;
18
+ /** Smallest star radius */
19
+ minRadius?: number;
20
+ /** Largest star radius */
21
+ maxRadius?: number;
22
+ /** Enable opacity oscillation */
23
+ twinkle?: boolean;
24
+ /** Oscillation speed multiplier */
25
+ twinkleSpeed?: number;
26
+ /** Enable parallax size layers */
27
+ depth?: boolean;
28
+ /** Enable random shooting stars */
29
+ shootingStars?: boolean;
30
+ /** 0-1, spawn chance per frame */
31
+ shootingStarFrequency?: number;
32
+ /** Extra Tailwind / CSS classes */
33
+ className?: string;
34
+ /** Extra inline styles */
35
+ style?: CSSProperties;
36
+ /** Target frame rate (0 = uncapped) */
37
+ fps?: number;
38
+ }
39
+
40
+ interface Star {
41
+ x: number;
42
+ y: number;
43
+ r: number;
44
+ vx: number;
45
+ vy: number;
46
+ opacity: number;
47
+ phase: number;
48
+ twinkleRate: number;
49
+ color: string;
50
+ layer: number;
51
+ }
52
+
53
+ interface ShootingStar {
54
+ x: number;
55
+ y: number;
56
+ vx: number;
57
+ vy: number;
58
+ life: number;
59
+ decay: number;
60
+ length: number;
61
+ width: number;
62
+ }
63
+
64
+ // ── helpers ──────────────────────────────────────────────────────
65
+
66
+ const HEX_REGEX = /^#?([a-fA-F0-9]{6})$/;
67
+
68
+ function hexToRgb(hex: string): { r: number; g: number; b: number } {
69
+ const match = hex.trim().match(HEX_REGEX);
70
+ if (!match) {
71
+ console.warn(`[StarfieldBackground] Invalid hex color "${hex}", defaulting to white`);
72
+ return { r: 255, g: 255, b: 255 };
73
+ }
74
+ const h = match[1];
75
+ return {
76
+ r: parseInt(h.substring(0, 2), 16),
77
+ g: parseInt(h.substring(2, 4), 16),
78
+ b: parseInt(h.substring(4, 6), 16),
79
+ };
80
+ }
81
+
82
+ function lerp(a: number, b: number, t: number): number {
83
+ return a + (b - a) * t;
84
+ }
85
+
86
+ function rand(min: number, max: number): number {
87
+ return Math.random() * (max - min) + min;
88
+ }
89
+
90
+ // ── component ────────────────────────────────────────────────────
91
+
92
+ export function StarfieldBackground({
93
+ bgColor = "#000000",
94
+ starColor = "#ffffff",
95
+ accentColor = "#facc15",
96
+ accentRatio = 0.15,
97
+ starCount = 600,
98
+ maxSpeed = 0.35,
99
+ minRadius = 0.4,
100
+ maxRadius = 2.2,
101
+ twinkle = true,
102
+ twinkleSpeed = 1,
103
+ depth = true,
104
+ shootingStars = true,
105
+ shootingStarFrequency = 0.003,
106
+ className = "",
107
+ style = {},
108
+ fps = 60,
109
+ }: StarfieldBackgroundProps) {
110
+ const canvasRef = useRef<HTMLCanvasElement>(null);
111
+ const animRef = useRef<number>(0);
112
+ const starsRef = useRef<Star[]>([]);
113
+ const shootingRef = useRef<ShootingStar[]>([]);
114
+ const sizeRef = useRef({ w: 0, h: 0 });
115
+
116
+ // ── star factory ────────────────────────────────────────────
117
+
118
+ const makeStar = useCallback(
119
+ (w: number, h: number): Star => {
120
+ const isAccent = Math.random() < accentRatio;
121
+ const layer = depth ? rand(0.3, 1) : 1;
122
+ return {
123
+ x: rand(0, w),
124
+ y: rand(0, h),
125
+ r: lerp(minRadius, maxRadius, layer),
126
+ vx: rand(-maxSpeed, maxSpeed) * layer,
127
+ vy: rand(-maxSpeed, maxSpeed) * layer,
128
+ opacity: rand(0.4, 1),
129
+ phase: rand(0, Math.PI * 2),
130
+ twinkleRate: rand(0.005, 0.025) * twinkleSpeed,
131
+ color: isAccent ? accentColor : starColor,
132
+ layer,
133
+ };
134
+ },
135
+ [
136
+ accentRatio,
137
+ starColor,
138
+ accentColor,
139
+ minRadius,
140
+ maxRadius,
141
+ maxSpeed,
142
+ depth,
143
+ twinkleSpeed,
144
+ ]
145
+ );
146
+
147
+ // ── shooting star factory ───────────────────────────────────
148
+
149
+ const makeShootingStar = useCallback(
150
+ (w: number, h: number): ShootingStar => {
151
+ const angle = rand(Math.PI * 0.15, Math.PI * 0.35);
152
+ const speed = rand(6, 14);
153
+ return {
154
+ x: rand(-w * 0.1, w * 0.7),
155
+ y: rand(-h * 0.1, h * 0.3),
156
+ vx: Math.cos(angle) * speed,
157
+ vy: Math.sin(angle) * speed,
158
+ life: 1,
159
+ decay: rand(0.008, 0.02),
160
+ length: rand(40, 120),
161
+ width: rand(1, 2.5),
162
+ };
163
+ },
164
+ []
165
+ );
166
+
167
+ // ── main effect ─────────────────────────────────────────────
168
+
169
+ useEffect(() => {
170
+ const canvas = canvasRef.current;
171
+ if (!canvas) return;
172
+ const ctx = canvas.getContext("2d");
173
+ if (!ctx) return;
174
+
175
+ const dpr = window.devicePixelRatio || 1;
176
+
177
+ const resize = () => {
178
+ const rect = canvas.parentElement?.getBoundingClientRect() ?? {
179
+ width: window.innerWidth,
180
+ height: window.innerHeight,
181
+ };
182
+ const w = rect.width;
183
+ const h = rect.height;
184
+ canvas.width = w * dpr;
185
+ canvas.height = h * dpr;
186
+ canvas.style.width = `${w}px`;
187
+ canvas.style.height = `${h}px`;
188
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
189
+ sizeRef.current = { w, h };
190
+ };
191
+
192
+ resize();
193
+
194
+ const { w, h } = sizeRef.current;
195
+ starsRef.current = Array.from({ length: starCount }, () => makeStar(w, h));
196
+
197
+ const interval = fps > 0 ? 1000 / fps : 0;
198
+ let last = 0;
199
+
200
+ const draw = (now: number) => {
201
+ animRef.current = requestAnimationFrame(draw);
202
+
203
+ if (interval > 0 && now - last < interval) return;
204
+ last = now;
205
+
206
+ const { w: cw, h: ch } = sizeRef.current;
207
+ if (cw === 0 || ch === 0) return;
208
+
209
+ ctx.fillStyle = bgColor;
210
+ ctx.fillRect(0, 0, cw, ch);
211
+
212
+ const stars = starsRef.current;
213
+ for (let i = 0; i < stars.length; i++) {
214
+ const s = stars[i];
215
+
216
+ s.x += s.vx;
217
+ s.y += s.vy;
218
+
219
+ if (s.x < -4) s.x = cw + 4;
220
+ if (s.x > cw + 4) s.x = -4;
221
+ if (s.y < -4) s.y = ch + 4;
222
+ if (s.y > ch + 4) s.y = -4;
223
+
224
+ let alpha = s.opacity;
225
+ if (twinkle) {
226
+ s.phase += s.twinkleRate;
227
+ alpha = s.opacity * (0.5 + 0.5 * Math.sin(s.phase));
228
+ }
229
+
230
+ const { r, g, b } = hexToRgb(s.color);
231
+ ctx.fillStyle = `rgba(${r},${g},${b},${alpha.toFixed(3)})`;
232
+
233
+ if (s.r > 1.2) {
234
+ ctx.shadowBlur = s.r * 3;
235
+ ctx.shadowColor = `rgba(${r},${g},${b},${(alpha * 0.5).toFixed(3)})`;
236
+ } else {
237
+ ctx.shadowBlur = 0;
238
+ }
239
+
240
+ ctx.beginPath();
241
+ ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
242
+ ctx.fill();
243
+ }
244
+
245
+ ctx.shadowBlur = 0;
246
+
247
+ if (shootingStars) {
248
+ if (Math.random() < shootingStarFrequency) {
249
+ shootingRef.current.push(makeShootingStar(cw, ch));
250
+ }
251
+
252
+ const shots = shootingRef.current;
253
+ for (let i = shots.length - 1; i >= 0; i--) {
254
+ const ss = shots[i];
255
+ ss.x += ss.vx;
256
+ ss.y += ss.vy;
257
+ ss.life -= ss.decay;
258
+
259
+ if (ss.life <= 0) {
260
+ shots.splice(i, 1);
261
+ continue;
262
+ }
263
+
264
+ const tailX = ss.x - (ss.vx / Math.hypot(ss.vx, ss.vy)) * ss.length;
265
+ const tailY = ss.y - (ss.vy / Math.hypot(ss.vx, ss.vy)) * ss.length;
266
+
267
+ const grad = ctx.createLinearGradient(tailX, tailY, ss.x, ss.y);
268
+ grad.addColorStop(0, `rgba(255,255,255,0)`);
269
+ grad.addColorStop(1, `rgba(255,255,255,${ss.life.toFixed(3)})`);
270
+
271
+ ctx.strokeStyle = grad;
272
+ ctx.lineWidth = ss.width;
273
+ ctx.lineCap = "round";
274
+ ctx.beginPath();
275
+ ctx.moveTo(tailX, tailY);
276
+ ctx.lineTo(ss.x, ss.y);
277
+ ctx.stroke();
278
+ }
279
+ }
280
+ };
281
+
282
+ animRef.current = requestAnimationFrame(draw);
283
+
284
+ const ro = new ResizeObserver(() => {
285
+ resize();
286
+ const { w: nw, h: nh } = sizeRef.current;
287
+ starsRef.current.forEach((s) => {
288
+ if (s.x > nw) s.x = rand(0, nw);
289
+ if (s.y > nh) s.y = rand(0, nh);
290
+ });
291
+ });
292
+ if (canvas.parentElement) ro.observe(canvas.parentElement);
293
+
294
+ return () => {
295
+ cancelAnimationFrame(animRef.current);
296
+ ro.disconnect();
297
+ };
298
+ }, [
299
+ bgColor,
300
+ starCount,
301
+ twinkle,
302
+ shootingStars,
303
+ shootingStarFrequency,
304
+ fps,
305
+ makeStar,
306
+ makeShootingStar,
307
+ ]);
308
+
309
+ return (
310
+ <canvas
311
+ ref={canvasRef}
312
+ className={`absolute inset-0 ${className}`}
313
+ style={{ pointerEvents: "none", ...style }}
314
+ aria-hidden="true"
315
+ />
316
+ );
317
+ }