@fluritheme/confetti 0.0.0-stage → 1.0.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/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +103 -2
- package/dist/confetti.d.ts +24 -0
- package/dist/confetti.global.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +253 -0
- package/dist/overlay.d.ts +26 -0
- package/media/preview.png +0 -0
- package/package.json +49 -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,104 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Confetti by Fluri
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Every click deserves a party. Confetti for any website.
|
|
4
|
+
|
|
5
|
+
**[Live demo →](https://fluri.xyz/confetti/)** · [All Fluri themes](https://fluri.xyz/themes/) · No-code install for WordPress, Shopify, Wix & more at [fluri.xyz](https://fluri.xyz/confetti/)
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
- Zero dependencies, about 5 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/confetti@1/dist/confetti.global.js" data-auto></script>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Pass options as JSON:
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<script src="https://cdn.jsdelivr.net/npm/@fluritheme/confetti@1/dist/confetti.global.js" data-auto data-options='{"welcome":true}'></script>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The running effect is available as `window.FluriConfetti.instance`.
|
|
30
|
+
|
|
31
|
+
## Install with npm
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install @fluritheme/confetti
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createConfetti } from "@fluritheme/confetti";
|
|
39
|
+
|
|
40
|
+
const confetti = createConfetti({
|
|
41
|
+
triggers: "button, [data-fluri-confetti]", // elements that burst when clicked
|
|
42
|
+
palette: "party", // "pastel" | "gold" | "brand"
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
confetti.burst(); // one big burst from the centre
|
|
46
|
+
confetti.cannons(); // fire from both bottom corners
|
|
47
|
+
confetti.shower(4); // rain confetti for 4 seconds
|
|
48
|
+
|
|
49
|
+
// When leaving the page or unmounting a component:
|
|
50
|
+
confetti.destroy();
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### React
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { useEffect } from "react";
|
|
57
|
+
import { createConfetti } from "@fluritheme/confetti";
|
|
58
|
+
|
|
59
|
+
export function Confetti() {
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
const effect = createConfetti();
|
|
62
|
+
return () => effect.destroy();
|
|
63
|
+
}, []);
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Options
|
|
69
|
+
|
|
70
|
+
| Option | What it does | Default |
|
|
71
|
+
| --- | --- | --- |
|
|
72
|
+
| `triggers` | CSS selector of elements that burst when clicked | buttons, submit and add-to-cart buttons, `[data-fluri-confetti]` |
|
|
73
|
+
| `palette` | "party", "pastel", "gold" or "brand" | `"party"` |
|
|
74
|
+
| `brandColor` | Your colour for the "brand" palette | `"#6e56cf"` |
|
|
75
|
+
| `shapes` | "mixed", "paper", "stars" or "hearts" | `"mixed"` |
|
|
76
|
+
| `amount` | How much confetti, 0.2–2 | `1` |
|
|
77
|
+
| `gravity` | How fast it falls, 0.3–2 | `1` |
|
|
78
|
+
| `welcome` | Small celebration when the page opens | `false` |
|
|
79
|
+
| `root` | Limit the effect to one element | document |
|
|
80
|
+
| `zIndex` | Stacking order of the canvas | `2147483000` |
|
|
81
|
+
|
|
82
|
+
## Methods
|
|
83
|
+
|
|
84
|
+
- `burst(x?, y?)`
|
|
85
|
+
- `cannons()`
|
|
86
|
+
- `shower(seconds?)`
|
|
87
|
+
- `update(options)`
|
|
88
|
+
- `pause()`
|
|
89
|
+
- `resume()`
|
|
90
|
+
- `destroy()`
|
|
91
|
+
|
|
92
|
+
## Great for
|
|
93
|
+
|
|
94
|
+
- Celebrate sign-ups, orders and “add to cart”
|
|
95
|
+
- Thank-you pages and launches
|
|
96
|
+
- Birthdays, weddings and New Year’s Eve
|
|
97
|
+
|
|
98
|
+
## No code? Use Fluri
|
|
99
|
+
|
|
100
|
+
Don’t want to touch code? [Fluri](https://fluri.xyz/confetti/) installs Confetti 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/)
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT © František Gejdoš · [fluri.xyz](https://fluri.xyz)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface ConfettiOptions {
|
|
2
|
+
root?: HTMLElement;
|
|
3
|
+
triggers?: string;
|
|
4
|
+
palette?: "party" | "pastel" | "gold" | "brand";
|
|
5
|
+
brandColor?: string;
|
|
6
|
+
shapes?: "mixed" | "paper" | "stars" | "hearts";
|
|
7
|
+
amount?: number;
|
|
8
|
+
gravity?: number;
|
|
9
|
+
welcome?: boolean;
|
|
10
|
+
zIndex?: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function createConfetti(options?: ConfettiOptions): {
|
|
13
|
+
pause: () => void;
|
|
14
|
+
resume: () => void;
|
|
15
|
+
destroy(): void;
|
|
16
|
+
/** Burst at a point (overlay coordinates) or from the centre. */
|
|
17
|
+
burst(x?: number, y?: number): void;
|
|
18
|
+
cannons(): void;
|
|
19
|
+
shower(seconds?: number): void;
|
|
20
|
+
update(next: Partial<ConfettiOptions>): void;
|
|
21
|
+
getState: () => {
|
|
22
|
+
pieces: number;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! @fluritheme/confetti v1.0.0 | MIT | https://fluri.xyz/confetti/ */
|
|
2
|
+
(()=>{function L(i={}){var t,n,d,m;let r=document.createElement("canvas");r.setAttribute("aria-hidden","true"),r.dataset.fluri="overlay",Object.assign(r.style,{position:i.root?"absolute":"fixed",inset:"0",width:"100%",height:"100%",pointerEvents:"none",zIndex:String((t=i.zIndex)!=null?t:2147483e3),contain:"strict"});let a=(n=i.root)!=null?n:document.body;i.root&&getComputedStyle(i.root).position==="static"&&(i.root.style.position="relative"),a.append(r);let o=r.getContext("2d"),f=matchMedia("(prefers-reduced-motion: reduce)").matches,u=0,h=0,v=!1,p=!1,M=null,w=1e3/((d=i.maxFPS)!=null?d:60)-1,x={canvas:r,ctx:o,width:0,height:0,reduced:f,origin(){if(!i.root)return{x:0,y:0};let s=i.root.getBoundingClientRect();return{x:s.left,y:s.top}},start(s){M=s,g(performance.now())},pause(){v=!0,cancelAnimationFrame(u)},resume(){p||!v||(v=!1,h=performance.now(),g(h))},destroy(){p=!0,cancelAnimationFrame(u),l.disconnect(),removeEventListener("resize",b),document.removeEventListener("visibilitychange",e),r.remove()}};function b(){let s=Math.min(2,devicePixelRatio||1),y=i.root?i.root.clientWidth:innerWidth,C=i.root?i.root.clientHeight:innerHeight;x.width=y,x.height=C,r.width=Math.round(y*s),r.height=Math.round(C*s),o.setTransform(s,0,0,s,0,0)}function g(s){if(v||p||(u=requestAnimationFrame(g),s-h<w))return;let y=Math.min(.05,(s-(h||s))/1e3);h=s,M==null||M(y,s/1e3)}function e(){document.hidden?cancelAnimationFrame(u):!v&&!p&&(h=performance.now(),g(h))}let l=new ResizeObserver(b);return l.observe((m=i.root)!=null?m:document.documentElement),addEventListener("resize",b),document.addEventListener("visibilitychange",e),b(),x}var c=(i,r)=>i+Math.random()*(r-i);var O={party:["#ff595e","#ffca3a","#8ac926","#1982c4","#6a4c93","#ff924c"],pastel:["#ffadad","#ffd6a5","#fdffb6","#caffbf","#9bf6ff","#bdb2ff"],gold:["#f9d976","#f39f86","#ffe8a3","#d4af37","#fff3c4"]};function E(i={}){var g;let r={triggers:"button,[type=submit],.btn,.button,a.button,[data-fluri-confetti],[name=add],.add-to-cart,.product-form__submit",palette:"party",brandColor:"#6e56cf",shapes:"mixed",amount:1,gravity:1,welcome:!1,...i},a=L({root:r.root,zIndex:r.zIndex}),{ctx:o}=a,f=[],u=0,h=()=>{var e;return r.palette==="brand"?[r.brandColor,"#ffffff",z(r.brandColor,.35),z(r.brandColor,-.25)]:(e=O[r.palette])!=null?e:O.party},v=()=>r.shapes==="paper"?Math.random()<.8?0:4:r.shapes==="stars"?2:r.shapes==="hearts"?3:[0,0,1,2,4][Math.floor(Math.random()*5)];function p(e,l,t=70,n=Math.PI*.9,d=-Math.PI/2,m=1){a.reduced&&(t=Math.round(t*.25));let s=h();for(let y=0;y<Math.round(t*r.amount);y++){let C=d+c(-n/2,n/2),P=c(260,620)*m;f.push({x:e,y:l,vx:Math.cos(C)*P,vy:Math.sin(C)*P,r:c(0,Math.PI*2),vr:c(-8,8),flip:c(0,Math.PI*2),vf:c(4,12),size:c(5,10),color:s[Math.floor(Math.random()*s.length)],shape:v(),life:c(2.6,4)})}f.length>900&&f.splice(0,f.length-900)}function M(e){let l=Math.cos(e.flip);o.save(),o.translate(e.x,e.y),o.rotate(e.r),o.scale(1,l),o.globalAlpha=Math.min(1,e.life),o.fillStyle=e.color;let t=e.size;if(e.shape===0)o.fillRect(-t/2,-t*.3,t,t*.6);else if(e.shape===1)o.beginPath(),o.arc(0,0,t*.4,0,Math.PI*2),o.fill();else if(e.shape===2){o.beginPath();for(let n=0;n<10;n++){let d=n%2?t*.25:t*.6,m=n*Math.PI/5;o.lineTo(Math.cos(m)*d,Math.sin(m)*d)}o.fill()}else e.shape===3?(o.beginPath(),o.moveTo(0,t*.35),o.bezierCurveTo(-t*.7,-t*.15,-t*.25,-t*.65,0,-t*.25),o.bezierCurveTo(t*.25,-t*.65,t*.7,-t*.15,0,t*.35),o.fill()):o.fillRect(-t*.15,-t,t*.3,t*2);o.restore()}a.start(e=>{if(o.clearRect(0,0,a.width,a.height),u>0){u-=e;let t=h();for(let n=0;n<3*r.amount;n++)f.push({x:c(0,a.width),y:-12,vx:c(-30,30),vy:c(40,120),r:c(0,6),vr:c(-4,4),flip:c(0,6),vf:c(3,8),size:c(5,9),color:t[Math.floor(Math.random()*t.length)],shape:v(),life:6})}let l=520*r.gravity;for(let t=f.length-1;t>=0;t--){let n=f[t];n.vy+=l*e,n.vx*=1-Math.min(1,e*1.6),n.vy*=1-Math.min(1,e*(n.vy>0?2.6:1.2)),n.vx+=Math.sin(n.flip)*12*e,n.x+=n.vx*e,n.y+=n.vy*e,n.r+=n.vr*e,n.flip+=n.vf*e,n.life-=n.y>a.height?1:e*.35,n.life<=0||n.y>a.height+40?f.splice(t,1):M(n)}});let w=e=>{var d,m;let l=(m=(d=e.target)==null?void 0:d.closest)==null?void 0:m.call(d,r.triggers);if(!l)return;let t=a.origin(),n=l.getBoundingClientRect();p(n.left+n.width/2-t.x,n.top+n.height/2-t.y,80)},x=(g=r.root)!=null?g:document;x.addEventListener("click",w,{passive:!0}),r.welcome&&!a.reduced&&setTimeout(()=>b.cannons(),700);let b={pause:()=>a.pause(),resume:()=>a.resume(),destroy(){x.removeEventListener("click",w),a.destroy()},burst(e,l){p(e!=null?e:a.width/2,l!=null?l:a.height*.6,120,Math.PI*1.2)},cannons(){p(0,a.height,110,Math.PI/3,-Math.PI/3,1.25),p(a.width,a.height,110,Math.PI/3,-Math.PI*2/3,1.25)},shower(e=4){u=e},update(e){Object.assign(r,e)},getState:()=>({pieces:f.length})};return b}function z(i,r){let a=parseInt(i.replace("#","").padEnd(6,"0").slice(0,6),16),o=f=>{let u=a>>f&255;return Math.round(r>0?u+(255-u)*r:u*(1+r))};return`rgb(${o(16)},${o(8)},${o(0)})`}var S={createConfetti:E};window.FluriConfetti=S;var I=document.currentScript;if(I&&I.hasAttribute("data-auto")){let i=()=>{let r={};try{r=JSON.parse(I.getAttribute("data-options")||"{}")}catch{}S.instance=E(r)};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",i,{once:!0}):i()}})();
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./confetti.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/*! @fluritheme/confetti v1.0.0 | MIT | https://fluri.xyz/confetti/ */
|
|
2
|
+
|
|
3
|
+
// packages/confetti/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
|
+
var rand = (a, b) => a + Math.random() * (b - a);
|
|
92
|
+
|
|
93
|
+
// packages/confetti/src/confetti.ts
|
|
94
|
+
var palettes = {
|
|
95
|
+
party: ["#ff595e", "#ffca3a", "#8ac926", "#1982c4", "#6a4c93", "#ff924c"],
|
|
96
|
+
pastel: ["#ffadad", "#ffd6a5", "#fdffb6", "#caffbf", "#9bf6ff", "#bdb2ff"],
|
|
97
|
+
gold: ["#f9d976", "#f39f86", "#ffe8a3", "#d4af37", "#fff3c4"]
|
|
98
|
+
};
|
|
99
|
+
function createConfetti(options = {}) {
|
|
100
|
+
const o = {
|
|
101
|
+
triggers: "button,[type=submit],.btn,.button,a.button,[data-fluri-confetti],[name=add],.add-to-cart,.product-form__submit",
|
|
102
|
+
palette: "party",
|
|
103
|
+
brandColor: "#6e56cf",
|
|
104
|
+
shapes: "mixed",
|
|
105
|
+
amount: 1,
|
|
106
|
+
gravity: 1,
|
|
107
|
+
welcome: false,
|
|
108
|
+
...options
|
|
109
|
+
};
|
|
110
|
+
const overlay = createOverlay({ root: o.root, zIndex: o.zIndex });
|
|
111
|
+
const { ctx } = overlay;
|
|
112
|
+
const bits = [];
|
|
113
|
+
let shower = 0;
|
|
114
|
+
const colors = () => o.palette === "brand" ? [o.brandColor, "#ffffff", shade(o.brandColor, 0.35), shade(o.brandColor, -0.25)] : palettes[o.palette] ?? palettes.party;
|
|
115
|
+
const shapeFor = () => o.shapes === "paper" ? Math.random() < 0.8 ? 0 : 4 : o.shapes === "stars" ? 2 : o.shapes === "hearts" ? 3 : [0, 0, 1, 2, 4][Math.floor(Math.random() * 5)];
|
|
116
|
+
function burst(x, y, count = 70, spread = Math.PI * 0.9, angle = -Math.PI / 2, power = 1) {
|
|
117
|
+
if (overlay.reduced) count = Math.round(count * 0.25);
|
|
118
|
+
const c = colors();
|
|
119
|
+
for (let i = 0; i < Math.round(count * o.amount); i++) {
|
|
120
|
+
const a = angle + rand(-spread / 2, spread / 2), v = rand(260, 620) * power;
|
|
121
|
+
bits.push({
|
|
122
|
+
x,
|
|
123
|
+
y,
|
|
124
|
+
vx: Math.cos(a) * v,
|
|
125
|
+
vy: Math.sin(a) * v,
|
|
126
|
+
r: rand(0, Math.PI * 2),
|
|
127
|
+
vr: rand(-8, 8),
|
|
128
|
+
flip: rand(0, Math.PI * 2),
|
|
129
|
+
vf: rand(4, 12),
|
|
130
|
+
size: rand(5, 10),
|
|
131
|
+
color: c[Math.floor(Math.random() * c.length)],
|
|
132
|
+
shape: shapeFor(),
|
|
133
|
+
life: rand(2.6, 4)
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if (bits.length > 900) bits.splice(0, bits.length - 900);
|
|
137
|
+
}
|
|
138
|
+
function drawBit(b) {
|
|
139
|
+
const sy = Math.cos(b.flip);
|
|
140
|
+
ctx.save();
|
|
141
|
+
ctx.translate(b.x, b.y);
|
|
142
|
+
ctx.rotate(b.r);
|
|
143
|
+
ctx.scale(1, sy);
|
|
144
|
+
ctx.globalAlpha = Math.min(1, b.life);
|
|
145
|
+
ctx.fillStyle = b.color;
|
|
146
|
+
const s = b.size;
|
|
147
|
+
if (b.shape === 0) ctx.fillRect(-s / 2, -s * 0.3, s, s * 0.6);
|
|
148
|
+
else if (b.shape === 1) {
|
|
149
|
+
ctx.beginPath();
|
|
150
|
+
ctx.arc(0, 0, s * 0.4, 0, Math.PI * 2);
|
|
151
|
+
ctx.fill();
|
|
152
|
+
} else if (b.shape === 2) {
|
|
153
|
+
ctx.beginPath();
|
|
154
|
+
for (let i = 0; i < 10; i++) {
|
|
155
|
+
const rr = i % 2 ? s * 0.25 : s * 0.6, a = i * Math.PI / 5;
|
|
156
|
+
ctx.lineTo(Math.cos(a) * rr, Math.sin(a) * rr);
|
|
157
|
+
}
|
|
158
|
+
ctx.fill();
|
|
159
|
+
} else if (b.shape === 3) {
|
|
160
|
+
ctx.beginPath();
|
|
161
|
+
ctx.moveTo(0, s * 0.35);
|
|
162
|
+
ctx.bezierCurveTo(-s * 0.7, -s * 0.15, -s * 0.25, -s * 0.65, 0, -s * 0.25);
|
|
163
|
+
ctx.bezierCurveTo(s * 0.25, -s * 0.65, s * 0.7, -s * 0.15, 0, s * 0.35);
|
|
164
|
+
ctx.fill();
|
|
165
|
+
} else {
|
|
166
|
+
ctx.fillRect(-s * 0.15, -s, s * 0.3, s * 2);
|
|
167
|
+
}
|
|
168
|
+
ctx.restore();
|
|
169
|
+
}
|
|
170
|
+
overlay.start((dt) => {
|
|
171
|
+
ctx.clearRect(0, 0, overlay.width, overlay.height);
|
|
172
|
+
if (shower > 0) {
|
|
173
|
+
shower -= dt;
|
|
174
|
+
const c = colors();
|
|
175
|
+
for (let i = 0; i < 3 * o.amount; i++)
|
|
176
|
+
bits.push({
|
|
177
|
+
x: rand(0, overlay.width),
|
|
178
|
+
y: -12,
|
|
179
|
+
vx: rand(-30, 30),
|
|
180
|
+
vy: rand(40, 120),
|
|
181
|
+
r: rand(0, 6),
|
|
182
|
+
vr: rand(-4, 4),
|
|
183
|
+
flip: rand(0, 6),
|
|
184
|
+
vf: rand(3, 8),
|
|
185
|
+
size: rand(5, 9),
|
|
186
|
+
color: c[Math.floor(Math.random() * c.length)],
|
|
187
|
+
shape: shapeFor(),
|
|
188
|
+
life: 6
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
const g = 520 * o.gravity;
|
|
192
|
+
for (let i = bits.length - 1; i >= 0; i--) {
|
|
193
|
+
const b = bits[i];
|
|
194
|
+
b.vy += g * dt;
|
|
195
|
+
b.vx *= 1 - Math.min(1, dt * 1.6);
|
|
196
|
+
b.vy *= 1 - Math.min(1, dt * (b.vy > 0 ? 2.6 : 1.2));
|
|
197
|
+
b.vx += Math.sin(b.flip) * 12 * dt;
|
|
198
|
+
b.x += b.vx * dt;
|
|
199
|
+
b.y += b.vy * dt;
|
|
200
|
+
b.r += b.vr * dt;
|
|
201
|
+
b.flip += b.vf * dt;
|
|
202
|
+
b.life -= b.y > overlay.height ? 1 : dt * 0.35;
|
|
203
|
+
if (b.life <= 0 || b.y > overlay.height + 40) bits.splice(i, 1);
|
|
204
|
+
else drawBit(b);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
const click = (e) => {
|
|
208
|
+
const target = e.target?.closest?.(o.triggers);
|
|
209
|
+
if (!target) return;
|
|
210
|
+
const og = overlay.origin();
|
|
211
|
+
const r = target.getBoundingClientRect();
|
|
212
|
+
burst(r.left + r.width / 2 - og.x, r.top + r.height / 2 - og.y, 80);
|
|
213
|
+
};
|
|
214
|
+
const scope = o.root ?? document;
|
|
215
|
+
scope.addEventListener("click", click, { passive: true });
|
|
216
|
+
if (o.welcome && !overlay.reduced)
|
|
217
|
+
setTimeout(() => api.cannons(), 700);
|
|
218
|
+
const api = {
|
|
219
|
+
pause: () => overlay.pause(),
|
|
220
|
+
resume: () => overlay.resume(),
|
|
221
|
+
destroy() {
|
|
222
|
+
scope.removeEventListener("click", click);
|
|
223
|
+
overlay.destroy();
|
|
224
|
+
},
|
|
225
|
+
/** Burst at a point (overlay coordinates) or from the centre. */
|
|
226
|
+
burst(x, y) {
|
|
227
|
+
burst(x ?? overlay.width / 2, y ?? overlay.height * 0.6, 120, Math.PI * 1.2);
|
|
228
|
+
},
|
|
229
|
+
cannons() {
|
|
230
|
+
burst(0, overlay.height, 110, Math.PI / 3, -Math.PI / 3, 1.25);
|
|
231
|
+
burst(overlay.width, overlay.height, 110, Math.PI / 3, -Math.PI * 2 / 3, 1.25);
|
|
232
|
+
},
|
|
233
|
+
shower(seconds = 4) {
|
|
234
|
+
shower = seconds;
|
|
235
|
+
},
|
|
236
|
+
update(next) {
|
|
237
|
+
Object.assign(o, next);
|
|
238
|
+
},
|
|
239
|
+
getState: () => ({ pieces: bits.length })
|
|
240
|
+
};
|
|
241
|
+
return api;
|
|
242
|
+
}
|
|
243
|
+
function shade(hex, amount) {
|
|
244
|
+
const n = parseInt(hex.replace("#", "").padEnd(6, "0").slice(0, 6), 16);
|
|
245
|
+
const f = (s) => {
|
|
246
|
+
const c = n >> s & 255;
|
|
247
|
+
return Math.round(amount > 0 ? c + (255 - c) * amount : c * (1 + amount));
|
|
248
|
+
};
|
|
249
|
+
return `rgb(${f(16)},${f(8)},${f(0)})`;
|
|
250
|
+
}
|
|
251
|
+
export {
|
|
252
|
+
createConfetti
|
|
253
|
+
};
|
|
@@ -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;
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluritheme/confetti",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Confetti for any website: paper, stars and hearts burst from buttons, cannons fire from the corners and a shower rains down. One line, zero dependencies, ~5 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/confetti/",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"confetti",
|
|
15
|
+
"canvas-confetti",
|
|
16
|
+
"party",
|
|
17
|
+
"celebration",
|
|
18
|
+
"confetti-button",
|
|
19
|
+
"success-animation",
|
|
20
|
+
"animation",
|
|
21
|
+
"canvas",
|
|
22
|
+
"effects",
|
|
23
|
+
"website-effects",
|
|
24
|
+
"fluri",
|
|
25
|
+
"zero-dependencies"
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"media",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./global": "./dist/confetti.global.js",
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"main": "./dist/index.js",
|
|
44
|
+
"module": "./dist/index.js",
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"unpkg": "./dist/confetti.global.js",
|
|
47
|
+
"jsdelivr": "./dist/confetti.global.js",
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|