@zikojs/canvas-confetti 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 +21 -0
- package/README.md +1 -0
- package/package.json +49 -0
- package/src/canvas/index.js +164 -0
- package/src/effects/index.js +0 -0
- package/src/index.js +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ZAKARIA ELALAOUI
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @zikojs/confetti
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zikojs/canvas-confetti",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"author": "zakaria elalaoui",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/zikojs/addons.git",
|
|
13
|
+
"directory": "packages/canvas-confetti"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"zikojs",
|
|
17
|
+
"canvas-confetti"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./src/index.js",
|
|
22
|
+
"types": "./src/index.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"dist",
|
|
28
|
+
"LICENCE"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@rollup/plugin-commonjs": "^29.0.3",
|
|
33
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
34
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
35
|
+
"cross-env": "^10.1.0",
|
|
36
|
+
"rollup": "^4.63.1"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"canvas-confetti": "^1.9.4",
|
|
40
|
+
"zextra": "^1.15.0",
|
|
41
|
+
"ziko": "2.0.0-beta.9"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
45
|
+
"dev": "cross-env NODE_ENV=development rollup --config rollup.config.js",
|
|
46
|
+
"watch": "cross-env NODE_ENV=development rollup --config rollup.config.js -w",
|
|
47
|
+
"build": "cross-env NODE_ENV=production rollup --config rollup.config.js"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { UIElement } from "ziko/dom";
|
|
2
|
+
import { call_with_optional_props } from "ziko/dom/internal-utils";
|
|
3
|
+
import confetti from "canvas-confetti";
|
|
4
|
+
|
|
5
|
+
export class ConfettiEffect {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
fire(confettiInstance, overrides = {}) {
|
|
11
|
+
if (typeof confettiInstance === "function") {
|
|
12
|
+
confettiInstance({
|
|
13
|
+
...this.options,
|
|
14
|
+
...overrides,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class BurstEffect extends ConfettiEffect {
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
super({
|
|
23
|
+
particleCount: 100,
|
|
24
|
+
spread: 70,
|
|
25
|
+
origin: { y: 0.6 },
|
|
26
|
+
...options,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class SnowEffect extends ConfettiEffect {
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
super({
|
|
34
|
+
particleCount: 1,
|
|
35
|
+
startVelocity: 0,
|
|
36
|
+
ticks: 200,
|
|
37
|
+
gravity: 0.3,
|
|
38
|
+
origin: {
|
|
39
|
+
x: Math.random(),
|
|
40
|
+
y: Math.random() * 0.99 - 0.2,
|
|
41
|
+
},
|
|
42
|
+
colors: ["#ffffff"],
|
|
43
|
+
shapes: ["circle"],
|
|
44
|
+
scalar: 0.8,
|
|
45
|
+
drift: Math.random() - 0.5,
|
|
46
|
+
...options,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class FireworksEffect extends ConfettiEffect {
|
|
52
|
+
fire(confettiInstance) {
|
|
53
|
+
const duration = this.options.duration || 3000;
|
|
54
|
+
const animationEnd = Date.now() + duration;
|
|
55
|
+
const defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 0 };
|
|
56
|
+
|
|
57
|
+
const interval = setInterval(() => {
|
|
58
|
+
const timeLeft = animationEnd - Date.now();
|
|
59
|
+
if (timeLeft <= 0) return clearInterval(interval);
|
|
60
|
+
|
|
61
|
+
const particleCount = 50 * (timeLeft / duration);
|
|
62
|
+
confettiInstance({
|
|
63
|
+
...defaults,
|
|
64
|
+
...this.options,
|
|
65
|
+
particleCount,
|
|
66
|
+
origin: { x: Math.random(), y: Math.random() - 0.2 },
|
|
67
|
+
});
|
|
68
|
+
}, 250);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Component Factory Helpers
|
|
73
|
+
export const Burst = (options) => new BurstEffect(options);
|
|
74
|
+
export const Snow = (options) => new SnowEffect(options);
|
|
75
|
+
export const Fireworks = (options) => new FireworksEffect(options);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Main Confetti Wrapper Component
|
|
79
|
+
*/
|
|
80
|
+
export class UIConfettiCanvas extends UIElement {
|
|
81
|
+
constructor(props = {}, ...effects) {
|
|
82
|
+
super({ element: "canvas" });
|
|
83
|
+
this.props = props;
|
|
84
|
+
this._effects = effects.flat();
|
|
85
|
+
this.confettiInstance = null;
|
|
86
|
+
|
|
87
|
+
const width = props.width || "100%";
|
|
88
|
+
const height = props.height || "100%";
|
|
89
|
+
|
|
90
|
+
this.style({
|
|
91
|
+
position: props.position || "fixed",
|
|
92
|
+
top: "0",
|
|
93
|
+
left: "0",
|
|
94
|
+
width: typeof width === "number" ? `${width}px` : width,
|
|
95
|
+
height: typeof height === "number" ? `${height}px` : height,
|
|
96
|
+
pointerEvents: "none",
|
|
97
|
+
zIndex: props.zIndex ?? 999,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
requestAnimationFrame(() => this.initCanvas());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
initCanvas() {
|
|
104
|
+
this.confettiInstance = confetti.create(this.element, {
|
|
105
|
+
resize: true,
|
|
106
|
+
useWorker: this.props.useWorker ?? true,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Auto-fire initial declarative effects if passed
|
|
110
|
+
this._effects.forEach((effect) => {
|
|
111
|
+
if (effect instanceof ConfettiEffect) {
|
|
112
|
+
effect.fire(this.confettiInstance);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Fires a specific effect preset or raw options object
|
|
119
|
+
*/
|
|
120
|
+
fire(effectOrOptions = {}, overrides = {}) {
|
|
121
|
+
if (!this.confettiInstance) return this;
|
|
122
|
+
|
|
123
|
+
if (effectOrOptions instanceof ConfettiEffect) {
|
|
124
|
+
effectOrOptions.fire(this.confettiInstance, overrides);
|
|
125
|
+
} else if (typeof effectOrOptions === "object") {
|
|
126
|
+
this.confettiInstance({
|
|
127
|
+
particleCount: 80,
|
|
128
|
+
spread: 60,
|
|
129
|
+
...effectOrOptions,
|
|
130
|
+
...overrides,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Triggers a burst origin near a specific DOM element (e.g. Button)
|
|
138
|
+
*/
|
|
139
|
+
fireFromElement(targetElement, options = {}) {
|
|
140
|
+
if (!this.confettiInstance || !targetElement) return this;
|
|
141
|
+
|
|
142
|
+
const rect = targetElement.getBoundingClientRect();
|
|
143
|
+
const x = (rect.left + rect.width / 2) / window.innerWidth;
|
|
144
|
+
const y = (rect.top + rect.height / 2) / window.innerHeight;
|
|
145
|
+
|
|
146
|
+
this.fire(options, { origin: { x, y } });
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
reset() {
|
|
151
|
+
if (this.confettiInstance) {
|
|
152
|
+
this.confettiInstance.reset();
|
|
153
|
+
}
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
destroy() {
|
|
158
|
+
this.reset();
|
|
159
|
+
this.confettiInstance = null;
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export const Confetti = call_with_optional_props(UIConfettiCanvas);
|
|
File without changes
|
package/src/index.js
ADDED