@fluritheme/fairy-lights 0.0.0-stage → 1.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.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +106 -2
- package/dist/fairy-lights.d.ts +26 -0
- package/dist/fairy-lights.global.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +327 -0
- package/dist/overlay.d.ts +26 -0
- package/package.json +48 -4
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 František Gejdoš (Fluri, https://fluri.xyz)
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Fairy Lights by Fluri
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cosy, glowing and a little festive. Christmas fairy lights for any website.
|
|
4
|
+
|
|
5
|
+
**[Live demo →](https://fluri.xyz/fairy-lights/)** · [All Fluri themes](https://fluri.xyz/themes/) · No-code install for WordPress, Shopify, Wix & more at [fluri.xyz](https://fluri.xyz/fairy-lights/)
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
- Zero dependencies, about 6 KB minified
|
|
10
|
+
- One canvas with `pointer-events: none`: never blocks clicks, scrolling or text selection
|
|
11
|
+
- Respects `prefers-reduced-motion` and pauses when the tab is hidden
|
|
12
|
+
- Works with plain HTML, React, Vue, Svelte, Astro, Next.js and anything else
|
|
13
|
+
- TypeScript types included
|
|
14
|
+
|
|
15
|
+
## Quick start: one line of HTML
|
|
16
|
+
|
|
17
|
+
Paste this before `</body>`:
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<script src="https://cdn.jsdelivr.net/npm/@fluritheme/fairy-lights@1/dist/fairy-lights.global.js" data-auto></script>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Pass options as JSON:
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<script src="https://cdn.jsdelivr.net/npm/@fluritheme/fairy-lights@1/dist/fairy-lights.global.js" data-auto data-options='{"palette":"multicolor"}'></script>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The running effect is available as `window.FluriFairyLights.instance`.
|
|
30
|
+
|
|
31
|
+
## Install with npm
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install @fluritheme/fairy-lights
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createFairyLights } from "@fluritheme/fairy-lights";
|
|
39
|
+
|
|
40
|
+
const lights = createFairyLights({
|
|
41
|
+
palette: "warm", // "multicolor" | "candy" | "icy"
|
|
42
|
+
surfaces: "h1, .card, [data-fluri-surface]", // what the lights hang on
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
lights.twinkle(); // a wave of sparkle
|
|
46
|
+
lights.chase(); // classic chasing lights (call again to stop)
|
|
47
|
+
lights.cycleColors(); // next palette
|
|
48
|
+
lights.toggle(); // switch all strings on/off
|
|
49
|
+
|
|
50
|
+
// When leaving the page or unmounting a component:
|
|
51
|
+
lights.destroy();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### React
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useEffect } from "react";
|
|
58
|
+
import { createFairyLights } from "@fluritheme/fairy-lights";
|
|
59
|
+
|
|
60
|
+
export function FairyLights() {
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const effect = createFairyLights();
|
|
63
|
+
return () => effect.destroy();
|
|
64
|
+
}, []);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Options
|
|
70
|
+
|
|
71
|
+
| Option | What it does | Default |
|
|
72
|
+
| --- | --- | --- |
|
|
73
|
+
| `surfaces` | CSS selector of elements the strings hang on | `"h1,.card,article,[data-fluri-surface]"` |
|
|
74
|
+
| `palette` | "warm", "multicolor", "candy" or "icy" | `"warm"` |
|
|
75
|
+
| `garland` | A string across the top of the screen | `true` |
|
|
76
|
+
| `density` | Bulbs per 100 px | `3.2` |
|
|
77
|
+
| `sag` | How much the strings droop, 0–1 | `0.5` |
|
|
78
|
+
| `twinkle` | Twinkle strength, 0–1 | `0.6` |
|
|
79
|
+
| `bulbSize` | Bulb size multiplier | `1` |
|
|
80
|
+
| `interactive` | Swing at the cursor, click a string to switch it | `true` |
|
|
81
|
+
| `root` | Limit the effect to one element | document |
|
|
82
|
+
| `zIndex` | Stacking order of the canvas | `2147483000` |
|
|
83
|
+
|
|
84
|
+
## Methods
|
|
85
|
+
|
|
86
|
+
- `twinkle()`
|
|
87
|
+
- `chase()`
|
|
88
|
+
- `cycleColors()`
|
|
89
|
+
- `toggle()`
|
|
90
|
+
- `update(options)`
|
|
91
|
+
- `pause()`
|
|
92
|
+
- `resume()`
|
|
93
|
+
- `destroy()`
|
|
94
|
+
|
|
95
|
+
## Great for
|
|
96
|
+
|
|
97
|
+
- Christmas and holiday sales
|
|
98
|
+
- Cafés, bars and restaurants
|
|
99
|
+
- Wedding, party and event pages
|
|
100
|
+
|
|
101
|
+
## No code? Use Fluri
|
|
102
|
+
|
|
103
|
+
Don’t want to touch code? [Fluri](https://fluri.xyz/fairy-lights/) installs Fairy Lights on WordPress, Shopify, Webflow, Wix, Squarespace and Framer with one snippet, with live settings and a dashboard. There are more than a dozen animated themes, from a pet dragon to a koi pond. [See them all →](https://fluri.xyz/themes/)
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT © František Gejdoš · [fluri.xyz](https://fluri.xyz)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface FairyLightsOptions {
|
|
2
|
+
root?: HTMLElement;
|
|
3
|
+
surfaces?: string;
|
|
4
|
+
palette?: "warm" | "multicolor" | "candy" | "icy";
|
|
5
|
+
density?: number;
|
|
6
|
+
sag?: number;
|
|
7
|
+
twinkle?: number;
|
|
8
|
+
bulbSize?: number;
|
|
9
|
+
garland?: boolean;
|
|
10
|
+
interactive?: boolean;
|
|
11
|
+
zIndex?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function createFairyLights(options?: FairyLightsOptions): {
|
|
14
|
+
pause: () => void;
|
|
15
|
+
resume: () => void;
|
|
16
|
+
destroy(): void;
|
|
17
|
+
twinkle(): void;
|
|
18
|
+
chase(): void;
|
|
19
|
+
toggle(): void;
|
|
20
|
+
cycleColors(): void;
|
|
21
|
+
update(next: Partial<FairyLightsOptions>): void;
|
|
22
|
+
getState: () => {
|
|
23
|
+
strands: number;
|
|
24
|
+
mode: "steady" | "chase" | "wave";
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! @fluritheme/fairy-lights v1.0.0 | MIT | https://fluri.xyz/fairy-lights/ */
|
|
2
|
+
(()=>{function j(i={}){var E,S,F,n;let e=document.createElement("canvas");e.setAttribute("aria-hidden","true"),e.dataset.fluri="overlay",Object.assign(e.style,{position:i.root?"absolute":"fixed",inset:"0",width:"100%",height:"100%",pointerEvents:"none",zIndex:String((E=i.zIndex)!=null?E:2147483e3),contain:"strict"});let a=(S=i.root)!=null?S:document.body;i.root&&getComputedStyle(i.root).position==="static"&&(i.root.style.position="relative"),a.append(e);let r=e.getContext("2d"),d=matchMedia("(prefers-reduced-motion: reduce)").matches,u=0,f=0,h=!1,y=!1,v=null,w=1e3/((F=i.maxFPS)!=null?F:60)-1,M={canvas:e,ctx:r,width:0,height:0,reduced:d,origin(){if(!i.root)return{x:0,y:0};let t=i.root.getBoundingClientRect();return{x:t.left,y:t.top}},start(t){v=t,b(performance.now())},pause(){h=!0,cancelAnimationFrame(u)},resume(){y||!h||(h=!1,f=performance.now(),b(f))},destroy(){y=!0,cancelAnimationFrame(u),L.disconnect(),removeEventListener("resize",x),document.removeEventListener("visibilitychange",k),e.remove()}};function x(){let t=Math.min(2,devicePixelRatio||1),s=i.root?i.root.clientWidth:innerWidth,c=i.root?i.root.clientHeight:innerHeight;M.width=s,M.height=c,e.width=Math.round(s*t),e.height=Math.round(c*t),r.setTransform(t,0,0,t,0,0)}function b(t){if(h||y||(u=requestAnimationFrame(b),t-f<w))return;let s=Math.min(.05,(t-(f||t))/1e3);f=t,v==null||v(s,t/1e3)}function k(){document.hidden?cancelAnimationFrame(u):!h&&!y&&(f=performance.now(),b(f))}let L=new ResizeObserver(x);return L.observe((n=i.root)!=null?n:document.documentElement),addEventListener("resize",x),document.addEventListener("visibilitychange",k),x(),M}function D(i,e,a=8){let r=[];try{r=[...(e!=null?e:document).querySelectorAll(i)]}catch{r=[]}return r.filter(d=>!d.closest("[data-fluri-ignore]")&&d.offsetWidth>40).slice(0,a)}var A=(i,e)=>i+Math.random()*(e-i),H=(i,e,a)=>Math.max(e,Math.min(a,i));var I={warm:["#ffd27a","#ffc061","#ffe3a3","#ffb347"],multicolor:["#ff5a5f","#ffd23f","#3ec300","#2fb5ff","#b77cff","#ff8c42"],candy:["#ff4d6d","#ffffff","#ff8fab","#ffe5ec"],icy:["#bde0fe","#ffffff","#a2d2ff","#cdb4db"]};function R(i={}){var S,F;let e={palette:"warm",density:3.2,sag:.5,twinkle:.6,bulbSize:1,garland:!0,interactive:!0,surfaces:"h1,.card,article,[data-fluri-surface]",...i},a=j({root:e.root,zIndex:e.zIndex}),{ctx:r}=a,d=(S=I[e.palette])!=null?S:I.warm,u=[],f={x:-999,y:-999},h="steady",y=0;function v(n){let t=H(Math.round(n/100*e.density),4,40);return Array.from({length:t},(s,c)=>({t:(c+.5)/t,color:d[c%d.length],phase:A(0,Math.PI*2),speed:A(.6,1.6),swing:0,vs:0,glow:0}))}function w(){var c;let n=D(e.surfaces,e.root,12),t=n.filter(l=>!n.some(o=>o!==l&&o.contains(l))).slice(0,6),s=[];e.garland&&s.push((c=u.find(l=>l.el===null))!=null?c:{el:null,bulbs:v(e.root?e.root.clientWidth:innerWidth),on:!0,power:0});for(let l of t){let o=u.find(m=>m.el===l);s.push(o!=null?o:{el:l,bulbs:v(l.offsetWidth),on:!0,power:0})}u=s}w();let M=setInterval(w,2500);function x(n){let t=a.origin();if(!n.el)return{x1:-10,y1:6,x2:a.width+10,y2:6,sag:26*(.5+e.sag)};let s=n.el.getBoundingClientRect(),c=s.left-t.x+4,l=s.right-t.x-4,o=s.top-t.y-12;return{x1:c,y1:o,x2:l,y2:o,sag:Math.min(16,(l-c)*.03)*(.5+e.sag)}}function b(n,t){return{x:n.x1+(n.x2-n.x1)*t,y:n.y1+(n.y2-n.y1)*t+4*n.sag*t*(1-t)}}function k(n,t){r.clearRect(0,0,a.width,a.height),y+=n;let s=5.4*e.bulbSize;for(let c of u){c.power+=((c.on?1:0)-c.power)*Math.min(1,n*6);let l=x(c);if(!(l.y1<-60||l.y1>a.height+60)){r.beginPath();for(let o=0;o<=24;o++){let m=b(l,o/24);o?r.lineTo(m.x,m.y):r.moveTo(m.x,m.y)}r.strokeStyle="rgba(40,48,40,.55)",r.lineWidth=1.2,r.stroke(),c.bulbs.forEach((o,m)=>{let g=b(l,o.t),C=f.x-g.x,$=f.y-g.y,B=e.interactive&&!a.reduced&&C*C+$*$<2500;B&&(o.vs+=(C>0?-1:1)*6*n*10),o.vs+=-o.swing*40*n,o.vs*=1-Math.min(1,n*3),o.swing=H(o.swing+o.vs*n,-.6,.6),o.glow+=((B?1:0)-o.glow)*Math.min(1,n*8);let p=1;if(!a.reduced){let T=.5+.5*Math.sin(t*2.2*o.speed+o.phase);p=1-e.twinkle*.55*(1-T),h==="chase"&&(p=(m+Math.floor(y*8))%3===0?1:.25),h==="wave"&&(p=.3+.7*Math.max(0,Math.sin(y*4-m*.5)))}p=p*c.power+o.glow*.5;let O=o.swing,z=g.x+Math.sin(O)*9,P=g.y+Math.cos(O)*9;if(r.strokeStyle="rgba(40,48,40,.7)",r.lineWidth=1.4,r.beginPath(),r.moveTo(g.x,g.y),r.lineTo(g.x+Math.sin(O)*4,g.y+Math.cos(O)*4),r.stroke(),p>.05){let T=r.createRadialGradient(z,P,0,z,P,s*5);T.addColorStop(0,q(o.color,.55*p)),T.addColorStop(1,q(o.color,0)),r.fillStyle=T,r.beginPath(),r.arc(z,P,s*5,0,Math.PI*2),r.fill()}r.save(),r.translate(z,P),r.rotate(-O),r.beginPath(),r.ellipse(0,0,s*.75,s*1.15,0,0,Math.PI*2),r.fillStyle=p>.15?Y(o.color,"#ffffff",.25*p):"rgba(120,120,110,.55)",r.globalAlpha=.45+.55*Math.min(1,p+.1),r.fill(),r.restore()})}}}a.start(k);let L=n=>{let t=a.origin();f={x:n.clientX-t.x,y:n.clientY-t.y}},E=n=>{if(!e.interactive)return;let t=a.origin(),s=n.clientX-t.x,c=n.clientY-t.y;for(let l of u){let o=x(l);for(let m=0;m<=20;m++){let g=b(o,m/20);if((g.x-s)**2+(g.y+9-c)**2<220){l.on=!l.on;return}}}};return addEventListener("pointermove",L,{passive:!0}),((F=e.root)!=null?F:document).addEventListener("click",E),{pause:()=>a.pause(),resume:()=>a.resume(),destroy(){var n;clearInterval(M),removeEventListener("pointermove",L),((n=e.root)!=null?n:document).removeEventListener("click",E),a.destroy()},twinkle(){h="wave",y=0,setTimeout(()=>h="steady",2600)},chase(){h=h==="chase"?"steady":"chase",y=0},toggle(){let n=!u.some(t=>t.on);u.forEach(t=>t.on=n)},cycleColors(){let n=Object.keys(I);e.palette=n[(n.indexOf(e.palette)+1)%n.length],d=I[e.palette],u.forEach(t=>t.bulbs.forEach((s,c)=>s.color=d[c%d.length]))},update(n){var t;Object.assign(e,n),n.palette&&(d=(t=I[n.palette])!=null?t:d),u=[],w()},getState:()=>({strands:u.length,mode:h})}}function q(i,e){let a=parseInt(i.slice(1),16);return`rgba(${a>>16&255},${a>>8&255},${a&255},${e})`}function Y(i,e,a){let r=parseInt(i.slice(1),16),d=parseInt(e.slice(1),16),u=f=>Math.round((r>>f&255)*(1-a)+(d>>f&255)*a);return`rgb(${u(16)},${u(8)},${u(0)})`}var X={createFairyLights:R};window.FluriFairyLights=X;var W=document.currentScript;if(W&&W.hasAttribute("data-auto")){let i=()=>{let e={};try{e=JSON.parse(W.getAttribute("data-options")||"{}")}catch{}X.instance=R(e)};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",i,{once:!0}):i()}})();
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./fairy-lights.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/*! @fluritheme/fairy-lights v1.0.0 | MIT | https://fluri.xyz/fairy-lights/ */
|
|
2
|
+
|
|
3
|
+
// packages/fairy-lights/src/overlay.ts
|
|
4
|
+
function createOverlay(o = {}) {
|
|
5
|
+
const canvas = document.createElement("canvas");
|
|
6
|
+
canvas.setAttribute("aria-hidden", "true");
|
|
7
|
+
canvas.dataset.fluri = "overlay";
|
|
8
|
+
Object.assign(canvas.style, {
|
|
9
|
+
position: o.root ? "absolute" : "fixed",
|
|
10
|
+
inset: "0",
|
|
11
|
+
width: "100%",
|
|
12
|
+
height: "100%",
|
|
13
|
+
pointerEvents: "none",
|
|
14
|
+
zIndex: String(o.zIndex ?? 2147483e3),
|
|
15
|
+
contain: "strict"
|
|
16
|
+
});
|
|
17
|
+
const host = o.root ?? document.body;
|
|
18
|
+
if (o.root && getComputedStyle(o.root).position === "static")
|
|
19
|
+
o.root.style.position = "relative";
|
|
20
|
+
host.append(canvas);
|
|
21
|
+
const ctx = canvas.getContext("2d");
|
|
22
|
+
const reduced = matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
23
|
+
let raf = 0, last = 0, paused = false, destroyed = false, frame = null;
|
|
24
|
+
const minStep = 1e3 / (o.maxFPS ?? 60) - 1;
|
|
25
|
+
const overlay = {
|
|
26
|
+
canvas,
|
|
27
|
+
ctx,
|
|
28
|
+
width: 0,
|
|
29
|
+
height: 0,
|
|
30
|
+
reduced,
|
|
31
|
+
origin() {
|
|
32
|
+
if (!o.root) return { x: 0, y: 0 };
|
|
33
|
+
const r = o.root.getBoundingClientRect();
|
|
34
|
+
return { x: r.left, y: r.top };
|
|
35
|
+
},
|
|
36
|
+
start(f) {
|
|
37
|
+
frame = f;
|
|
38
|
+
loop(performance.now());
|
|
39
|
+
},
|
|
40
|
+
pause() {
|
|
41
|
+
paused = true;
|
|
42
|
+
cancelAnimationFrame(raf);
|
|
43
|
+
},
|
|
44
|
+
resume() {
|
|
45
|
+
if (destroyed || !paused) return;
|
|
46
|
+
paused = false;
|
|
47
|
+
last = performance.now();
|
|
48
|
+
loop(last);
|
|
49
|
+
},
|
|
50
|
+
destroy() {
|
|
51
|
+
destroyed = true;
|
|
52
|
+
cancelAnimationFrame(raf);
|
|
53
|
+
ro.disconnect();
|
|
54
|
+
removeEventListener("resize", size);
|
|
55
|
+
document.removeEventListener("visibilitychange", vis);
|
|
56
|
+
canvas.remove();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function size() {
|
|
60
|
+
const dpr = Math.min(2, devicePixelRatio || 1);
|
|
61
|
+
const w = o.root ? o.root.clientWidth : innerWidth;
|
|
62
|
+
const h = o.root ? o.root.clientHeight : innerHeight;
|
|
63
|
+
overlay.width = w;
|
|
64
|
+
overlay.height = h;
|
|
65
|
+
canvas.width = Math.round(w * dpr);
|
|
66
|
+
canvas.height = Math.round(h * dpr);
|
|
67
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
68
|
+
}
|
|
69
|
+
function loop(t) {
|
|
70
|
+
if (paused || destroyed) return;
|
|
71
|
+
raf = requestAnimationFrame(loop);
|
|
72
|
+
if (t - last < minStep) return;
|
|
73
|
+
const dt = Math.min(0.05, (t - (last || t)) / 1e3);
|
|
74
|
+
last = t;
|
|
75
|
+
frame?.(dt, t / 1e3);
|
|
76
|
+
}
|
|
77
|
+
function vis() {
|
|
78
|
+
if (document.hidden) cancelAnimationFrame(raf);
|
|
79
|
+
else if (!paused && !destroyed) {
|
|
80
|
+
last = performance.now();
|
|
81
|
+
loop(last);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const ro = new ResizeObserver(size);
|
|
85
|
+
ro.observe(o.root ?? document.documentElement);
|
|
86
|
+
addEventListener("resize", size);
|
|
87
|
+
document.addEventListener("visibilitychange", vis);
|
|
88
|
+
size();
|
|
89
|
+
return overlay;
|
|
90
|
+
}
|
|
91
|
+
function surfacesIn(selector, root, limit = 8) {
|
|
92
|
+
let list = [];
|
|
93
|
+
try {
|
|
94
|
+
list = [...(root ?? document).querySelectorAll(selector)];
|
|
95
|
+
} catch {
|
|
96
|
+
list = [];
|
|
97
|
+
}
|
|
98
|
+
return list.filter((el) => !el.closest("[data-fluri-ignore]") && el.offsetWidth > 40).slice(0, limit);
|
|
99
|
+
}
|
|
100
|
+
var rand = (a, b) => a + Math.random() * (b - a);
|
|
101
|
+
var clamp = (v, a, b) => Math.max(a, Math.min(b, v));
|
|
102
|
+
|
|
103
|
+
// packages/fairy-lights/src/fairy-lights.ts
|
|
104
|
+
var palettes = {
|
|
105
|
+
warm: ["#ffd27a", "#ffc061", "#ffe3a3", "#ffb347"],
|
|
106
|
+
multicolor: [
|
|
107
|
+
"#ff5a5f",
|
|
108
|
+
"#ffd23f",
|
|
109
|
+
"#3ec300",
|
|
110
|
+
"#2fb5ff",
|
|
111
|
+
"#b77cff",
|
|
112
|
+
"#ff8c42"
|
|
113
|
+
],
|
|
114
|
+
candy: ["#ff4d6d", "#ffffff", "#ff8fab", "#ffe5ec"],
|
|
115
|
+
icy: ["#bde0fe", "#ffffff", "#a2d2ff", "#cdb4db"]
|
|
116
|
+
};
|
|
117
|
+
function createFairyLights(options = {}) {
|
|
118
|
+
const o = {
|
|
119
|
+
palette: "warm",
|
|
120
|
+
density: 3.2,
|
|
121
|
+
sag: 0.5,
|
|
122
|
+
twinkle: 0.6,
|
|
123
|
+
bulbSize: 1,
|
|
124
|
+
garland: true,
|
|
125
|
+
interactive: true,
|
|
126
|
+
surfaces: "h1,.card,article,[data-fluri-surface]",
|
|
127
|
+
...options
|
|
128
|
+
};
|
|
129
|
+
const overlay = createOverlay({ root: o.root, zIndex: o.zIndex });
|
|
130
|
+
const { ctx } = overlay;
|
|
131
|
+
let colors = palettes[o.palette] ?? palettes.warm;
|
|
132
|
+
let strands = [];
|
|
133
|
+
let pointer = { x: -999, y: -999 };
|
|
134
|
+
let mode = "steady";
|
|
135
|
+
let modeTime = 0;
|
|
136
|
+
function makeBulbs(length) {
|
|
137
|
+
const n = clamp(Math.round(length / 100 * o.density), 4, 40);
|
|
138
|
+
return Array.from({ length: n }, (_, i) => ({
|
|
139
|
+
t: (i + 0.5) / n,
|
|
140
|
+
color: colors[i % colors.length],
|
|
141
|
+
phase: rand(0, Math.PI * 2),
|
|
142
|
+
speed: rand(0.6, 1.6),
|
|
143
|
+
swing: 0,
|
|
144
|
+
vs: 0,
|
|
145
|
+
glow: 0
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
function scan() {
|
|
149
|
+
const found = surfacesIn(o.surfaces, o.root, 12);
|
|
150
|
+
const els = found.filter((el) => !found.some((p) => p !== el && p.contains(el))).slice(0, 6);
|
|
151
|
+
const next = [];
|
|
152
|
+
if (o.garland)
|
|
153
|
+
next.push(
|
|
154
|
+
strands.find((s) => s.el === null) ?? {
|
|
155
|
+
el: null,
|
|
156
|
+
bulbs: makeBulbs(o.root ? o.root.clientWidth : innerWidth),
|
|
157
|
+
on: true,
|
|
158
|
+
power: 0
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
for (const el of els) {
|
|
162
|
+
const old = strands.find((s) => s.el === el);
|
|
163
|
+
next.push(
|
|
164
|
+
old ?? { el, bulbs: makeBulbs(el.offsetWidth), on: true, power: 0 }
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
strands = next;
|
|
168
|
+
}
|
|
169
|
+
scan();
|
|
170
|
+
const rescan = setInterval(scan, 2500);
|
|
171
|
+
function line(s) {
|
|
172
|
+
const origin = overlay.origin();
|
|
173
|
+
if (!s.el) {
|
|
174
|
+
const w = overlay.width;
|
|
175
|
+
return { x1: -10, y1: 6, x2: w + 10, y2: 6, sag: 26 * (0.5 + o.sag) };
|
|
176
|
+
}
|
|
177
|
+
const r = s.el.getBoundingClientRect();
|
|
178
|
+
const x1 = r.left - origin.x + 4, x2 = r.right - origin.x - 4, y = r.top - origin.y - 12;
|
|
179
|
+
return {
|
|
180
|
+
x1,
|
|
181
|
+
y1: y,
|
|
182
|
+
x2,
|
|
183
|
+
y2: y,
|
|
184
|
+
sag: Math.min(16, (x2 - x1) * 0.03) * (0.5 + o.sag)
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function at(g, t) {
|
|
188
|
+
return {
|
|
189
|
+
x: g.x1 + (g.x2 - g.x1) * t,
|
|
190
|
+
y: g.y1 + (g.y2 - g.y1) * t + 4 * g.sag * t * (1 - t)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function draw(dt, time) {
|
|
194
|
+
ctx.clearRect(0, 0, overlay.width, overlay.height);
|
|
195
|
+
modeTime += dt;
|
|
196
|
+
const size = 5.4 * o.bulbSize;
|
|
197
|
+
for (const s of strands) {
|
|
198
|
+
s.power += ((s.on ? 1 : 0) - s.power) * Math.min(1, dt * 6);
|
|
199
|
+
const g = line(s);
|
|
200
|
+
if (g.y1 < -60 || g.y1 > overlay.height + 60) continue;
|
|
201
|
+
ctx.beginPath();
|
|
202
|
+
for (let i = 0; i <= 24; i++) {
|
|
203
|
+
const p = at(g, i / 24);
|
|
204
|
+
i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y);
|
|
205
|
+
}
|
|
206
|
+
ctx.strokeStyle = "rgba(40,48,40,.55)";
|
|
207
|
+
ctx.lineWidth = 1.2;
|
|
208
|
+
ctx.stroke();
|
|
209
|
+
s.bulbs.forEach((b, i) => {
|
|
210
|
+
const p = at(g, b.t);
|
|
211
|
+
const dx = pointer.x - p.x, dy = pointer.y - p.y, near = o.interactive && !overlay.reduced && dx * dx + dy * dy < 2500;
|
|
212
|
+
if (near) b.vs += (dx > 0 ? -1 : 1) * 6 * dt * 10;
|
|
213
|
+
b.vs += -b.swing * 40 * dt;
|
|
214
|
+
b.vs *= 1 - Math.min(1, dt * 3);
|
|
215
|
+
b.swing = clamp(b.swing + b.vs * dt, -0.6, 0.6);
|
|
216
|
+
b.glow += ((near ? 1 : 0) - b.glow) * Math.min(1, dt * 8);
|
|
217
|
+
let level = 1;
|
|
218
|
+
if (!overlay.reduced) {
|
|
219
|
+
const tw = 0.5 + 0.5 * Math.sin(time * 2.2 * b.speed + b.phase);
|
|
220
|
+
level = 1 - o.twinkle * 0.55 * (1 - tw);
|
|
221
|
+
if (mode === "chase")
|
|
222
|
+
level = (i + Math.floor(modeTime * 8)) % 3 === 0 ? 1 : 0.25;
|
|
223
|
+
if (mode === "wave")
|
|
224
|
+
level = 0.3 + 0.7 * Math.max(0, Math.sin(modeTime * 4 - i * 0.5));
|
|
225
|
+
}
|
|
226
|
+
level = level * s.power + b.glow * 0.5;
|
|
227
|
+
const ang = b.swing, bx = p.x + Math.sin(ang) * 9, by = p.y + Math.cos(ang) * 9;
|
|
228
|
+
ctx.strokeStyle = "rgba(40,48,40,.7)";
|
|
229
|
+
ctx.lineWidth = 1.4;
|
|
230
|
+
ctx.beginPath();
|
|
231
|
+
ctx.moveTo(p.x, p.y);
|
|
232
|
+
ctx.lineTo(p.x + Math.sin(ang) * 4, p.y + Math.cos(ang) * 4);
|
|
233
|
+
ctx.stroke();
|
|
234
|
+
if (level > 0.05) {
|
|
235
|
+
const halo = ctx.createRadialGradient(bx, by, 0, bx, by, size * 5);
|
|
236
|
+
halo.addColorStop(0, hexA(b.color, 0.55 * level));
|
|
237
|
+
halo.addColorStop(1, hexA(b.color, 0));
|
|
238
|
+
ctx.fillStyle = halo;
|
|
239
|
+
ctx.beginPath();
|
|
240
|
+
ctx.arc(bx, by, size * 5, 0, Math.PI * 2);
|
|
241
|
+
ctx.fill();
|
|
242
|
+
}
|
|
243
|
+
ctx.save();
|
|
244
|
+
ctx.translate(bx, by);
|
|
245
|
+
ctx.rotate(-ang);
|
|
246
|
+
ctx.beginPath();
|
|
247
|
+
ctx.ellipse(0, 0, size * 0.75, size * 1.15, 0, 0, Math.PI * 2);
|
|
248
|
+
ctx.fillStyle = level > 0.15 ? mix(b.color, "#ffffff", 0.25 * level) : "rgba(120,120,110,.55)";
|
|
249
|
+
ctx.globalAlpha = 0.45 + 0.55 * Math.min(1, level + 0.1);
|
|
250
|
+
ctx.fill();
|
|
251
|
+
ctx.restore();
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
overlay.start(draw);
|
|
256
|
+
const move = (e) => {
|
|
257
|
+
const og = overlay.origin();
|
|
258
|
+
pointer = { x: e.clientX - og.x, y: e.clientY - og.y };
|
|
259
|
+
};
|
|
260
|
+
const click = (e) => {
|
|
261
|
+
if (!o.interactive) return;
|
|
262
|
+
const og = overlay.origin();
|
|
263
|
+
const x = e.clientX - og.x, y = e.clientY - og.y;
|
|
264
|
+
for (const s of strands) {
|
|
265
|
+
const g = line(s);
|
|
266
|
+
for (let i = 0; i <= 20; i++) {
|
|
267
|
+
const p = at(g, i / 20);
|
|
268
|
+
if ((p.x - x) ** 2 + (p.y + 9 - y) ** 2 < 220) {
|
|
269
|
+
s.on = !s.on;
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
addEventListener("pointermove", move, { passive: true });
|
|
276
|
+
(o.root ?? document).addEventListener("click", click);
|
|
277
|
+
return {
|
|
278
|
+
pause: () => overlay.pause(),
|
|
279
|
+
resume: () => overlay.resume(),
|
|
280
|
+
destroy() {
|
|
281
|
+
clearInterval(rescan);
|
|
282
|
+
removeEventListener("pointermove", move);
|
|
283
|
+
(o.root ?? document).removeEventListener("click", click);
|
|
284
|
+
overlay.destroy();
|
|
285
|
+
},
|
|
286
|
+
twinkle() {
|
|
287
|
+
mode = "wave";
|
|
288
|
+
modeTime = 0;
|
|
289
|
+
setTimeout(() => mode = "steady", 2600);
|
|
290
|
+
},
|
|
291
|
+
chase() {
|
|
292
|
+
mode = mode === "chase" ? "steady" : "chase";
|
|
293
|
+
modeTime = 0;
|
|
294
|
+
},
|
|
295
|
+
toggle() {
|
|
296
|
+
const on = !strands.some((s) => s.on);
|
|
297
|
+
strands.forEach((s) => s.on = on);
|
|
298
|
+
},
|
|
299
|
+
cycleColors() {
|
|
300
|
+
const keys = Object.keys(palettes);
|
|
301
|
+
o.palette = keys[(keys.indexOf(o.palette) + 1) % keys.length];
|
|
302
|
+
colors = palettes[o.palette];
|
|
303
|
+
strands.forEach(
|
|
304
|
+
(s) => s.bulbs.forEach((b, i) => b.color = colors[i % colors.length])
|
|
305
|
+
);
|
|
306
|
+
},
|
|
307
|
+
update(next) {
|
|
308
|
+
Object.assign(o, next);
|
|
309
|
+
if (next.palette) colors = palettes[next.palette] ?? colors;
|
|
310
|
+
strands = [];
|
|
311
|
+
scan();
|
|
312
|
+
},
|
|
313
|
+
getState: () => ({ strands: strands.length, mode })
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
function hexA(hex, a) {
|
|
317
|
+
const n = parseInt(hex.slice(1), 16);
|
|
318
|
+
return `rgba(${n >> 16 & 255},${n >> 8 & 255},${n & 255},${a})`;
|
|
319
|
+
}
|
|
320
|
+
function mix(a, b, t) {
|
|
321
|
+
const x = parseInt(a.slice(1), 16), y = parseInt(b.slice(1), 16);
|
|
322
|
+
const c = (s) => Math.round((x >> s & 255) * (1 - t) + (y >> s & 255) * t);
|
|
323
|
+
return `rgb(${c(16)},${c(8)},${c(0)})`;
|
|
324
|
+
}
|
|
325
|
+
export {
|
|
326
|
+
createFairyLights
|
|
327
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface OverlayOptions {
|
|
2
|
+
root?: HTMLElement | undefined;
|
|
3
|
+
zIndex?: number | undefined;
|
|
4
|
+
maxFPS?: number | undefined;
|
|
5
|
+
}
|
|
6
|
+
export interface Overlay {
|
|
7
|
+
canvas: HTMLCanvasElement;
|
|
8
|
+
ctx: CanvasRenderingContext2D;
|
|
9
|
+
/** CSS pixel size of the drawing area. */
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
/** Area origin in viewport coordinates (for converting client points). */
|
|
13
|
+
origin(): {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
};
|
|
17
|
+
start(frame: (dt: number, t: number) => void): void;
|
|
18
|
+
pause(): void;
|
|
19
|
+
resume(): void;
|
|
20
|
+
destroy(): void;
|
|
21
|
+
reduced: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function createOverlay(o?: OverlayOptions): Overlay;
|
|
24
|
+
export declare function surfacesIn(selector: string, root?: HTMLElement, limit?: number): HTMLElement[];
|
|
25
|
+
export declare const rand: (a: number, b: number) => number;
|
|
26
|
+
export declare const clamp: (v: number, a: number, b: number) => number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluritheme/fairy-lights",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Christmas fairy lights for any website: strings of glowing bulbs drape along headings and cards, twinkle, swing at the cursor and switch off with a click. Zero dependencies, ~6 KB.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "František Gejdoš",
|
|
9
|
+
"url": "https://fluri.xyz/founder/",
|
|
10
|
+
"email": "fluritheme@gmail.com"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://fluri.xyz/fairy-lights/",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"christmas",
|
|
15
|
+
"christmas-lights",
|
|
16
|
+
"fairy-lights",
|
|
17
|
+
"holiday",
|
|
18
|
+
"xmas",
|
|
19
|
+
"festive",
|
|
20
|
+
"string-lights",
|
|
21
|
+
"animation",
|
|
22
|
+
"canvas",
|
|
23
|
+
"website-effects",
|
|
24
|
+
"fluri",
|
|
25
|
+
"zero-dependencies"
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"CHANGELOG.md"
|
|
33
|
+
],
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"import": "./dist/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./global": "./dist/fairy-lights.global.js",
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"main": "./dist/index.js",
|
|
43
|
+
"module": "./dist/index.js",
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"unpkg": "./dist/fairy-lights.global.js",
|
|
46
|
+
"jsdelivr": "./dist/fairy-lights.global.js",
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
}
|
|
50
|
+
}
|