@sarthak03dot/romantic-animations 1.2.0 → 1.2.3

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.
@@ -1,93 +1,93 @@
1
- import { mergeOptions } from '../core/engine.js';
2
-
3
- const DEFAULTS = {
4
- count: 80, // number of sparkles alive at once
5
- minSize: 2,
6
- maxSize: 6,
7
- speed: 0.5,
8
- twinkleSpeed: 0.04,
9
- colors: ['#fff', '#ffe4e8', '#ffb3c1', '#ff85a1', '#ffd6ff', '#e7c6ff'],
10
- glow: true,
11
- };
12
-
13
- export function sparkles(canvas, userOptions = {}) {
14
- const opts = mergeOptions(DEFAULTS, userOptions);
15
- const ctx = canvas.getContext('2d');
16
- const stars = [];
17
- let running = true;
18
-
19
- function createStar() {
20
- return {
21
- x: Math.random() * canvas.width,
22
- y: Math.random() * canvas.height,
23
- size: opts.minSize + Math.random() * (opts.maxSize - opts.minSize),
24
- alpha: Math.random(),
25
- alphaDir: Math.random() > 0.5 ? 1 : -1,
26
- twinkleSpeed: opts.twinkleSpeed * (0.5 + Math.random()),
27
- color: opts.colors[Math.floor(Math.random() * opts.colors.length)],
28
- vx: (Math.random() - 0.5) * opts.speed,
29
- vy: (Math.random() - 0.5) * opts.speed,
30
- };
31
- }
32
-
33
- // Populate initial stars
34
- for (let i = 0; i < opts.count; i++) stars.push(createStar());
35
-
36
- function drawStar(s) {
37
- ctx.save();
38
- ctx.globalAlpha = Math.max(0, Math.min(1, s.alpha));
39
- if (opts.glow) { ctx.shadowColor = s.color; ctx.shadowBlur = s.size * 3; }
40
- ctx.fillStyle = s.color;
41
-
42
- // Cross / sparkle shape
43
- const r = s.size;
44
- ctx.beginPath();
45
- for (let i = 0; i < 4; i++) {
46
- const a = (i * Math.PI) / 2;
47
- ctx.ellipse(
48
- s.x + Math.cos(a) * r * 0.35,
49
- s.y + Math.sin(a) * r * 0.35,
50
- r * 0.15, r * 0.7, a, 0, Math.PI * 2
51
- );
52
- }
53
- ctx.fill();
54
-
55
- // Tiny centre dot
56
- ctx.beginPath();
57
- ctx.arc(s.x, s.y, r * 0.2, 0, Math.PI * 2);
58
- ctx.fill();
59
- ctx.restore();
60
- }
61
-
62
- function animate() {
63
- if (!running) return;
64
- ctx.clearRect(0, 0, canvas.width, canvas.height);
65
-
66
- for (let i = 0; i < stars.length; i++) {
67
- const s = stars[i];
68
- s.x += s.vx;
69
- s.y += s.vy;
70
- s.alpha += s.alphaDir * s.twinkleSpeed;
71
-
72
- if (s.alpha >= 1) { s.alpha = 1; s.alphaDir = -1; }
73
- else if (s.alpha <= 0) { s.alpha = 0; s.alphaDir = 1; }
74
-
75
- // Wrap edges
76
- if (s.x < -10) s.x = canvas.width + 10;
77
- if (s.x > canvas.width + 10) s.x = -10;
78
- if (s.y < -10) s.y = canvas.height + 10;
79
- if (s.y > canvas.height + 10) s.y = -10;
80
-
81
- drawStar(s);
82
- }
83
-
84
- requestAnimationFrame(animate);
85
- }
86
-
87
- animate();
88
-
89
- return function stop() {
90
- running = false;
91
- ctx.clearRect(0, 0, canvas.width, canvas.height);
92
- };
93
- }
1
+ import { mergeOptions } from '../core/engine.js';
2
+
3
+ const DEFAULTS = {
4
+ count: 80, // number of sparkles alive at once
5
+ minSize: 2,
6
+ maxSize: 6,
7
+ speed: 0.5,
8
+ twinkleSpeed: 0.04,
9
+ colors: ['#fff', '#ffe4e8', '#ffb3c1', '#ff85a1', '#ffd6ff', '#e7c6ff'],
10
+ glow: true,
11
+ };
12
+
13
+ export function sparkles(canvas, userOptions = {}) {
14
+ const opts = mergeOptions(DEFAULTS, userOptions);
15
+ const ctx = canvas.getContext('2d');
16
+ const stars = [];
17
+ let running = true;
18
+
19
+ function createStar() {
20
+ return {
21
+ x: Math.random() * canvas.width,
22
+ y: Math.random() * canvas.height,
23
+ size: opts.minSize + Math.random() * (opts.maxSize - opts.minSize),
24
+ alpha: Math.random(),
25
+ alphaDir: Math.random() > 0.5 ? 1 : -1,
26
+ twinkleSpeed: opts.twinkleSpeed * (0.5 + Math.random()),
27
+ color: opts.colors[Math.floor(Math.random() * opts.colors.length)],
28
+ vx: (Math.random() - 0.5) * opts.speed,
29
+ vy: (Math.random() - 0.5) * opts.speed,
30
+ };
31
+ }
32
+
33
+ // Populate initial stars
34
+ for (let i = 0; i < opts.count; i++) stars.push(createStar());
35
+
36
+ function drawStar(s) {
37
+ ctx.save();
38
+ ctx.globalAlpha = Math.max(0, Math.min(1, s.alpha));
39
+ if (opts.glow) { ctx.shadowColor = s.color; ctx.shadowBlur = s.size * 3; }
40
+ ctx.fillStyle = s.color;
41
+
42
+ // Cross / sparkle shape
43
+ const r = s.size;
44
+ ctx.beginPath();
45
+ for (let i = 0; i < 4; i++) {
46
+ const a = (i * Math.PI) / 2;
47
+ ctx.ellipse(
48
+ s.x + Math.cos(a) * r * 0.35,
49
+ s.y + Math.sin(a) * r * 0.35,
50
+ r * 0.15, r * 0.7, a, 0, Math.PI * 2
51
+ );
52
+ }
53
+ ctx.fill();
54
+
55
+ // Tiny centre dot
56
+ ctx.beginPath();
57
+ ctx.arc(s.x, s.y, r * 0.2, 0, Math.PI * 2);
58
+ ctx.fill();
59
+ ctx.restore();
60
+ }
61
+
62
+ function animate() {
63
+ if (!running) return;
64
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
65
+
66
+ for (let i = 0; i < stars.length; i++) {
67
+ const s = stars[i];
68
+ s.x += s.vx;
69
+ s.y += s.vy;
70
+ s.alpha += s.alphaDir * s.twinkleSpeed;
71
+
72
+ if (s.alpha >= 1) { s.alpha = 1; s.alphaDir = -1; }
73
+ else if (s.alpha <= 0) { s.alpha = 0; s.alphaDir = 1; }
74
+
75
+ // Wrap edges
76
+ if (s.x < -10) s.x = canvas.width + 10;
77
+ if (s.x > canvas.width + 10) s.x = -10;
78
+ if (s.y < -10) s.y = canvas.height + 10;
79
+ if (s.y > canvas.height + 10) s.y = -10;
80
+
81
+ drawStar(s);
82
+ }
83
+
84
+ requestAnimationFrame(animate);
85
+ }
86
+
87
+ animate();
88
+
89
+ return function stop() {
90
+ running = false;
91
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
92
+ };
93
+ }
@@ -1,100 +1,100 @@
1
- import { mergeOptions } from '../core/engine.js';
2
-
3
- const DEFAULTS = {
4
- starCount: 120,
5
- speed: 0.4,
6
- colors: ['#ffffff', '#ffe4e8', '#ffc2d1', '#e7c6ff', '#a2d2ff'],
7
- minSize: 1,
8
- maxSize: 3.5,
9
- twinkle: true,
10
- connectDist: 100, // draw faint lines between close stars
11
- connectOpacity: 0.08,
12
- };
13
-
14
- export function starField(canvas, userOptions = {}) {
15
- const opts = mergeOptions(DEFAULTS, userOptions);
16
- const ctx = canvas.getContext('2d');
17
- const stars = [];
18
- let running = true;
19
-
20
- function createStar(randomY = false) {
21
- return {
22
- x: Math.random() * canvas.width,
23
- y: randomY ? Math.random() * canvas.height : Math.random() * canvas.height,
24
- size: opts.minSize + Math.random() * (opts.maxSize - opts.minSize),
25
- alpha: 0.3 + Math.random() * 0.7,
26
- alphaDir: Math.random() > 0.5 ? 1 : -1,
27
- twinkleSpeed: 0.008 + Math.random() * 0.015,
28
- vx: (Math.random() - 0.5) * opts.speed,
29
- vy: (Math.random() - 0.5) * opts.speed,
30
- color: opts.colors[Math.floor(Math.random() * opts.colors.length)],
31
- };
32
- }
33
-
34
- for (let i = 0; i < opts.starCount; i++) stars.push(createStar(true));
35
-
36
- function animate() {
37
- if (!running) return;
38
- ctx.clearRect(0, 0, canvas.width, canvas.height);
39
-
40
- // Draw connections
41
- if (opts.connectDist > 0) {
42
- for (let i = 0; i < stars.length; i++) {
43
- for (let j = i + 1; j < stars.length; j++) {
44
- const dx = stars[i].x - stars[j].x;
45
- const dy = stars[i].y - stars[j].y;
46
- const dist = Math.sqrt(dx * dx + dy * dy);
47
- if (dist < opts.connectDist) {
48
- ctx.save();
49
- ctx.globalAlpha = opts.connectOpacity * (1 - dist / opts.connectDist);
50
- ctx.strokeStyle = '#ffffff';
51
- ctx.lineWidth = 0.5;
52
- ctx.beginPath();
53
- ctx.moveTo(stars[i].x, stars[i].y);
54
- ctx.lineTo(stars[j].x, stars[j].y);
55
- ctx.stroke();
56
- ctx.restore();
57
- }
58
- }
59
- }
60
- }
61
-
62
- // Draw stars
63
- for (let i = 0; i < stars.length; i++) {
64
- const s = stars[i];
65
- s.x += s.vx;
66
- s.y += s.vy;
67
-
68
- if (opts.twinkle) {
69
- s.alpha += s.alphaDir * s.twinkleSpeed;
70
- if (s.alpha >= 1) { s.alpha = 1; s.alphaDir = -1; }
71
- if (s.alpha <= 0.1) { s.alpha = 0.1; s.alphaDir = 1; }
72
- }
73
-
74
- // Wrap
75
- if (s.x < -5) s.x = canvas.width + 5;
76
- if (s.x > canvas.width + 5) s.x = -5;
77
- if (s.y < -5) s.y = canvas.height + 5;
78
- if (s.y > canvas.height + 5) s.y = -5;
79
-
80
- ctx.save();
81
- ctx.globalAlpha = s.alpha;
82
- ctx.shadowColor = s.color;
83
- ctx.shadowBlur = s.size * 3;
84
- ctx.fillStyle = s.color;
85
- ctx.beginPath();
86
- ctx.arc(s.x, s.y, s.size, 0, Math.PI * 2);
87
- ctx.fill();
88
- ctx.restore();
89
- }
90
-
91
- requestAnimationFrame(animate);
92
- }
93
-
94
- animate();
95
-
96
- return function stop() {
97
- running = false;
98
- ctx.clearRect(0, 0, canvas.width, canvas.height);
99
- };
100
- }
1
+ import { mergeOptions } from '../core/engine.js';
2
+
3
+ const DEFAULTS = {
4
+ starCount: 120,
5
+ speed: 0.4,
6
+ colors: ['#ffffff', '#ffe4e8', '#ffc2d1', '#e7c6ff', '#a2d2ff'],
7
+ minSize: 1,
8
+ maxSize: 3.5,
9
+ twinkle: true,
10
+ connectDist: 100, // draw faint lines between close stars
11
+ connectOpacity: 0.08,
12
+ };
13
+
14
+ export function starField(canvas, userOptions = {}) {
15
+ const opts = mergeOptions(DEFAULTS, userOptions);
16
+ const ctx = canvas.getContext('2d');
17
+ const stars = [];
18
+ let running = true;
19
+
20
+ function createStar(randomY = false) {
21
+ return {
22
+ x: Math.random() * canvas.width,
23
+ y: randomY ? Math.random() * canvas.height : Math.random() * canvas.height,
24
+ size: opts.minSize + Math.random() * (opts.maxSize - opts.minSize),
25
+ alpha: 0.3 + Math.random() * 0.7,
26
+ alphaDir: Math.random() > 0.5 ? 1 : -1,
27
+ twinkleSpeed: 0.008 + Math.random() * 0.015,
28
+ vx: (Math.random() - 0.5) * opts.speed,
29
+ vy: (Math.random() - 0.5) * opts.speed,
30
+ color: opts.colors[Math.floor(Math.random() * opts.colors.length)],
31
+ };
32
+ }
33
+
34
+ for (let i = 0; i < opts.starCount; i++) stars.push(createStar(true));
35
+
36
+ function animate() {
37
+ if (!running) return;
38
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
39
+
40
+ // Draw connections
41
+ if (opts.connectDist > 0) {
42
+ for (let i = 0; i < stars.length; i++) {
43
+ for (let j = i + 1; j < stars.length; j++) {
44
+ const dx = stars[i].x - stars[j].x;
45
+ const dy = stars[i].y - stars[j].y;
46
+ const dist = Math.sqrt(dx * dx + dy * dy);
47
+ if (dist < opts.connectDist) {
48
+ ctx.save();
49
+ ctx.globalAlpha = opts.connectOpacity * (1 - dist / opts.connectDist);
50
+ ctx.strokeStyle = '#ffffff';
51
+ ctx.lineWidth = 0.5;
52
+ ctx.beginPath();
53
+ ctx.moveTo(stars[i].x, stars[i].y);
54
+ ctx.lineTo(stars[j].x, stars[j].y);
55
+ ctx.stroke();
56
+ ctx.restore();
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ // Draw stars
63
+ for (let i = 0; i < stars.length; i++) {
64
+ const s = stars[i];
65
+ s.x += s.vx;
66
+ s.y += s.vy;
67
+
68
+ if (opts.twinkle) {
69
+ s.alpha += s.alphaDir * s.twinkleSpeed;
70
+ if (s.alpha >= 1) { s.alpha = 1; s.alphaDir = -1; }
71
+ if (s.alpha <= 0.1) { s.alpha = 0.1; s.alphaDir = 1; }
72
+ }
73
+
74
+ // Wrap
75
+ if (s.x < -5) s.x = canvas.width + 5;
76
+ if (s.x > canvas.width + 5) s.x = -5;
77
+ if (s.y < -5) s.y = canvas.height + 5;
78
+ if (s.y > canvas.height + 5) s.y = -5;
79
+
80
+ ctx.save();
81
+ ctx.globalAlpha = s.alpha;
82
+ ctx.shadowColor = s.color;
83
+ ctx.shadowBlur = s.size * 3;
84
+ ctx.fillStyle = s.color;
85
+ ctx.beginPath();
86
+ ctx.arc(s.x, s.y, s.size, 0, Math.PI * 2);
87
+ ctx.fill();
88
+ ctx.restore();
89
+ }
90
+
91
+ requestAnimationFrame(animate);
92
+ }
93
+
94
+ animate();
95
+
96
+ return function stop() {
97
+ running = false;
98
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
99
+ };
100
+ }
@@ -1,77 +1,77 @@
1
- /**
2
- * romantic-animations — core engine
3
- * Handles canvas creation, sizing, resize observation, and cleanup.
4
- */
5
-
6
- const DEFAULT_OPTIONS = {
7
- zIndex: 0,
8
- };
9
-
10
- /**
11
- * Initialises a canvas inside the given container element.
12
- *
13
- * @param {string|HTMLElement} containerIdOrEl – element id OR element reference
14
- * @param {object} userOptions – optional overrides
15
- * @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D,
16
- * options: object, destroy: Function }}
17
- */
18
- export function initCanvas(containerIdOrEl, userOptions = {}) {
19
- const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
20
-
21
- const container =
22
- typeof containerIdOrEl === 'string'
23
- ? document.getElementById(containerIdOrEl)
24
- : containerIdOrEl;
25
-
26
- if (!container) {
27
- throw new Error(
28
- `[romantic-animations] Container "${containerIdOrEl}" not found in the DOM.`
29
- );
30
- }
31
-
32
- // Remove any pre-existing canvas we created (clean slate when re-triggering)
33
- const old = container.querySelector('canvas[data-ra]');
34
- if (old) old.remove();
35
-
36
- const canvas = document.createElement('canvas');
37
- canvas.setAttribute('data-ra', '1');
38
- canvas.style.cssText = `
39
- position: fixed;
40
- top: 0;
41
- left: 0;
42
- width: 100vw;
43
- height: 100vh;
44
- pointer-events: none;
45
- z-index: ${options.zIndex};
46
- `;
47
-
48
- const resize = () => {
49
- canvas.width = window.innerWidth;
50
- canvas.height = window.innerHeight;
51
- };
52
- resize();
53
-
54
- container.style.position = container.style.position || 'relative';
55
- container.appendChild(canvas);
56
-
57
- // Keep canvas sized to container
58
- const ro = new ResizeObserver(resize);
59
- ro.observe(container);
60
-
61
- const ctx = canvas.getContext('2d');
62
-
63
- /** Tear-down helper – call the returned destroy() to stop & clean up */
64
- function destroy() {
65
- ro.disconnect();
66
- canvas.remove();
67
- }
68
-
69
- return { canvas, ctx, options, destroy };
70
- }
71
-
72
- /**
73
- * Merge user options with animation-specific defaults.
74
- */
75
- export function mergeOptions(defaults, userOptions = {}) {
76
- return Object.assign({}, defaults, userOptions);
77
- }
1
+ /**
2
+ * romantic-animations — core engine
3
+ * Handles canvas creation, sizing, resize observation, and cleanup.
4
+ */
5
+
6
+ const DEFAULT_OPTIONS = {
7
+ zIndex: 0,
8
+ };
9
+
10
+ /**
11
+ * Initialises a canvas inside the given container element.
12
+ *
13
+ * @param {string|HTMLElement} containerIdOrEl – element id OR element reference
14
+ * @param {object} userOptions – optional overrides
15
+ * @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D,
16
+ * options: object, destroy: Function }}
17
+ */
18
+ export function initCanvas(containerIdOrEl, userOptions = {}) {
19
+ const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
20
+
21
+ const container =
22
+ typeof containerIdOrEl === 'string'
23
+ ? document.getElementById(containerIdOrEl)
24
+ : containerIdOrEl;
25
+
26
+ if (!container) {
27
+ throw new Error(
28
+ `[romantic-animations] Container "${containerIdOrEl}" not found in the DOM.`
29
+ );
30
+ }
31
+
32
+ // Remove any pre-existing canvas we created (clean slate when re-triggering)
33
+ const old = container.querySelector('canvas[data-ra]');
34
+ if (old) old.remove();
35
+
36
+ const canvas = document.createElement('canvas');
37
+ canvas.setAttribute('data-ra', '1');
38
+ canvas.style.cssText = `
39
+ position: fixed;
40
+ top: 0;
41
+ left: 0;
42
+ width: 100vw;
43
+ height: 100vh;
44
+ pointer-events: none;
45
+ z-index: ${options.zIndex};
46
+ `;
47
+
48
+ const resize = () => {
49
+ canvas.width = window.innerWidth;
50
+ canvas.height = window.innerHeight;
51
+ };
52
+ resize();
53
+
54
+ container.style.position = container.style.position || 'relative';
55
+ container.appendChild(canvas);
56
+
57
+ // Keep canvas sized to container
58
+ const ro = new ResizeObserver(resize);
59
+ ro.observe(container);
60
+
61
+ const ctx = canvas.getContext('2d');
62
+
63
+ /** Tear-down helper – call the returned destroy() to stop & clean up */
64
+ function destroy() {
65
+ ro.disconnect();
66
+ canvas.remove();
67
+ }
68
+
69
+ return { canvas, ctx, options, destroy };
70
+ }
71
+
72
+ /**
73
+ * Merge user options with animation-specific defaults.
74
+ */
75
+ export function mergeOptions(defaults, userOptions = {}) {
76
+ return Object.assign({}, defaults, userOptions);
77
+ }