hoorayjs 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/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +31 -0
- package/src/hooray.css +94 -0
- package/src/hooray.js +138 -0
- package/src/images/heart.svg +32 -0
- package/src/images/star.svg +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Clark Fannin
|
|
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
|
+
lightweight, flexible confetti/burst effects with support for custom images/icons!
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hoorayjs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "lightweight confetti/burst effects with image support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/hooray.js",
|
|
7
|
+
"module": "src/hooray.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/hooray.js",
|
|
10
|
+
"./hooray.css": "./src/hooray.css"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"demo": "python3 -m http.server"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"confetti",
|
|
20
|
+
"burst",
|
|
21
|
+
"celebrate",
|
|
22
|
+
"animation",
|
|
23
|
+
"effects"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/clarkfannin/yayjs.git"
|
|
28
|
+
},
|
|
29
|
+
"author": "Clark Fannin",
|
|
30
|
+
"license": "MIT"
|
|
31
|
+
}
|
package/src/hooray.css
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
* {
|
|
2
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
body {
|
|
6
|
+
margin: 0;
|
|
7
|
+
padding: 0;
|
|
8
|
+
display: flex;
|
|
9
|
+
justify-content: center;
|
|
10
|
+
align-items: center;
|
|
11
|
+
position: relative;
|
|
12
|
+
height: 100vh;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#hooray-form {
|
|
16
|
+
background-color: rgb(250, 249, 240);
|
|
17
|
+
border-radius: 16px;
|
|
18
|
+
border: 2px solid black;
|
|
19
|
+
box-shadow: 2px 2px 2px black;
|
|
20
|
+
display: flex;
|
|
21
|
+
flex-direction: column;
|
|
22
|
+
padding: 16px;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#hooray-form-title {
|
|
26
|
+
text-align: center;
|
|
27
|
+
margin: 0 auto 8px;
|
|
28
|
+
font-size: 42px;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#hooray-form-label {
|
|
32
|
+
display: inline-block;
|
|
33
|
+
max-width: 280px;
|
|
34
|
+
text-align: center;
|
|
35
|
+
font-size: 16px;
|
|
36
|
+
margin: 0 auto 16px;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#hooray-form-input {
|
|
40
|
+
border-radius: 8px;
|
|
41
|
+
font-size: 16px;
|
|
42
|
+
padding: 4px;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#hooray-form-input-wrapper {
|
|
46
|
+
margin: 0 auto;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#hooray-form-submit {
|
|
50
|
+
background: black;
|
|
51
|
+
border-radius: 8px;
|
|
52
|
+
color: rgb(250, 249, 240);
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
font-size: 16px;
|
|
55
|
+
padding: 4px;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#hooray-form-submit:active {
|
|
59
|
+
padding: 3px;
|
|
60
|
+
margin: 1px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.hooray-piece {
|
|
64
|
+
background-size: contain;
|
|
65
|
+
background-repeat: no-repeat;
|
|
66
|
+
background-position: center;
|
|
67
|
+
mask-size: contain;
|
|
68
|
+
mask-repeat: no-repeat;
|
|
69
|
+
mask-position: center;
|
|
70
|
+
-webkit-mask-size: contain;
|
|
71
|
+
-webkit-mask-repeat: no-repeat;
|
|
72
|
+
-webkit-mask-position: center;
|
|
73
|
+
display: block;
|
|
74
|
+
position: fixed;
|
|
75
|
+
animation: hooray-fall var(--duration) ease-out both;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@keyframes hooray-fall {
|
|
79
|
+
0% {
|
|
80
|
+
transform: translate(0, 0) rotate(var(--rot));
|
|
81
|
+
animation-timing-function: ease-out;
|
|
82
|
+
opacity: 1;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
15% {
|
|
86
|
+
transform: translate(var(--burst-x), var(--burst-y)) rotate(calc(var(--rot) + 120deg));
|
|
87
|
+
animation-timing-function: ease-in;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
100% {
|
|
91
|
+
transform: translate(calc(var(--burst-x) + var(--fall-x)), calc(100vh + 100%)) rotate(calc(var(--rot) + 540deg));
|
|
92
|
+
opacity: 1;
|
|
93
|
+
}
|
|
94
|
+
}
|
package/src/hooray.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
class Hooray {
|
|
2
|
+
static #isPainting = false;
|
|
3
|
+
|
|
4
|
+
#pieces;
|
|
5
|
+
#target;
|
|
6
|
+
#options;
|
|
7
|
+
|
|
8
|
+
constructor(target, options = {}) {
|
|
9
|
+
this.#pieces = [];
|
|
10
|
+
|
|
11
|
+
this.#target = target;
|
|
12
|
+
|
|
13
|
+
this.#options = {
|
|
14
|
+
count: 100,
|
|
15
|
+
width: target.clientWidth / 4,
|
|
16
|
+
height: (target.clientWidth / 4) * 0.6,
|
|
17
|
+
duration: 3000,
|
|
18
|
+
spread: 200,
|
|
19
|
+
mobileSpread: 50,
|
|
20
|
+
...options,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (this.#options.count > 2500) {
|
|
24
|
+
console.warn(
|
|
25
|
+
`hooray: count capped at 2500 (requested ${this.options.count})`,
|
|
26
|
+
);
|
|
27
|
+
this.options.count = 2500;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.warn = () => {
|
|
31
|
+
if (this.#options.count > 1000)
|
|
32
|
+
console.warn(
|
|
33
|
+
"hooray: a high count > 1000 is likely to impact performance",
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
this.warn();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
paint() {
|
|
40
|
+
if (Hooray.#isPainting) return;
|
|
41
|
+
Hooray.#isPainting = true;
|
|
42
|
+
for (let i = 0; i < this.#options.count; i++) {
|
|
43
|
+
const el = document.createElement("span");
|
|
44
|
+
el.classList.add("hooray-piece");
|
|
45
|
+
|
|
46
|
+
if (this.#options.image) {
|
|
47
|
+
// one value up front to retain image proportions
|
|
48
|
+
const rand = Math.random();
|
|
49
|
+
el.style.width = `${rand * this.#options.width}px`;
|
|
50
|
+
el.style.height = `${rand * this.#options.height}px`;
|
|
51
|
+
|
|
52
|
+
if (this.#options.color) {
|
|
53
|
+
el.style.backgroundColor = this.#options.color;
|
|
54
|
+
el.style.maskImage = `url(${this.#options.image})`;
|
|
55
|
+
} else {
|
|
56
|
+
el.style.backgroundImage = `url(${this.#options.image})`;
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
// randomize size if generating CSS confetti
|
|
60
|
+
el.style.width = `${Math.random() * this.#options.width}px`;
|
|
61
|
+
el.style.height = `${Math.random() * this.#options.height}px`;
|
|
62
|
+
|
|
63
|
+
// randomize color if generating CSS confetti
|
|
64
|
+
el.style.background = `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const rect = this.#target.getBoundingClientRect();
|
|
68
|
+
el.style.left = `${rect.left + rect.width / 2}px`;
|
|
69
|
+
el.style.top = `${rect.top + rect.height / 2}px`;
|
|
70
|
+
|
|
71
|
+
const angle = Math.random() * Math.PI * 2;
|
|
72
|
+
const viewportWidth = window.innerWidth;
|
|
73
|
+
let distance;
|
|
74
|
+
if (viewportWidth < 800) {
|
|
75
|
+
distance = 80 + Math.random() * this.#options.mobileSpread;
|
|
76
|
+
} else {
|
|
77
|
+
distance = 80 + Math.random() * this.#options.spread;
|
|
78
|
+
}
|
|
79
|
+
el.style.setProperty(
|
|
80
|
+
`--burst-x`,
|
|
81
|
+
`${Math.random() * Math.cos(angle) * distance}px`,
|
|
82
|
+
);
|
|
83
|
+
el.style.setProperty(
|
|
84
|
+
`--burst-y`,
|
|
85
|
+
`${Math.random() * Math.sin(angle) * distance}px`,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
el.style.setProperty(`--fall-x`, `0px`);
|
|
89
|
+
el.style.setProperty(`--rot`, `${Math.floor(Math.random() * 365)}deg`);
|
|
90
|
+
|
|
91
|
+
el.style.setProperty(
|
|
92
|
+
`--duration`,
|
|
93
|
+
`${(this.#options.duration / 1000).toFixed(2)}s`,
|
|
94
|
+
);
|
|
95
|
+
el.style.animationDelay = `${Math.random() * 0.1}s`;
|
|
96
|
+
|
|
97
|
+
this.#pieces.push(el);
|
|
98
|
+
document.body.append(el);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
Hooray.#isPainting = false;
|
|
103
|
+
this.cleanup();
|
|
104
|
+
}, this.#options.duration);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
cleanup() {
|
|
108
|
+
this.#pieces.forEach((piece) => {
|
|
109
|
+
piece.remove();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export const hooray = (target, options = {}) => {
|
|
115
|
+
const y = new Hooray(target, options);
|
|
116
|
+
y.paint();
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
document.querySelectorAll(".hooray-target").forEach((el) => {
|
|
120
|
+
el.addEventListener("click", (e) => {
|
|
121
|
+
hooray(e.target, {
|
|
122
|
+
count: 1000,
|
|
123
|
+
duration: 2500,
|
|
124
|
+
width: 10,
|
|
125
|
+
height: 10,
|
|
126
|
+
spread: 100,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
options: {
|
|
133
|
+
count: 200, // number of pieces; density
|
|
134
|
+
width: target.clientWidth / 10, // width in px
|
|
135
|
+
height: (target.clientWidth / 10) * 0.6, // height in px
|
|
136
|
+
duration: 3000, // duration of the animation from start to finish
|
|
137
|
+
}
|
|
138
|
+
*/
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
2
|
+
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
|
3
|
+
|
|
4
|
+
<svg
|
|
5
|
+
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
|
6
|
+
xmlns:cc="http://creativecommons.org/ns#"
|
|
7
|
+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
|
8
|
+
xmlns:svg="http://www.w3.org/2000/svg"
|
|
9
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
10
|
+
version="1.1"
|
|
11
|
+
width="130"
|
|
12
|
+
height="130"
|
|
13
|
+
id="svg2">
|
|
14
|
+
<metadata
|
|
15
|
+
id="metadata10">
|
|
16
|
+
<rdf:RDF>
|
|
17
|
+
<cc:Work
|
|
18
|
+
rdf:about="">
|
|
19
|
+
<dc:format>image/svg+xml</dc:format>
|
|
20
|
+
<dc:type
|
|
21
|
+
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
|
22
|
+
<dc:title></dc:title>
|
|
23
|
+
</cc:Work>
|
|
24
|
+
</rdf:RDF>
|
|
25
|
+
</metadata>
|
|
26
|
+
<defs
|
|
27
|
+
id="defs8" />
|
|
28
|
+
<path
|
|
29
|
+
d="M 65,29 C 59,19 49,12 37,12 20,12 7,25 7,42 7,75 25,80 65,118 105,80 123,75 123,42 123,25 110,12 93,12 81,12 71,19 65,29 z"
|
|
30
|
+
id="path4"
|
|
31
|
+
style="fill:#ff0707" />
|
|
32
|
+
</svg>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<svg width="800px" height="800px" viewBox="0 -0.5 33 33" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
3
|
+
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
|
4
|
+
<title>star</title>
|
|
5
|
+
<desc>Created with Sketch.</desc>
|
|
6
|
+
<defs>
|
|
7
|
+
|
|
8
|
+
<g id="Vivid.JS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
9
|
+
<g id="Vivid-Icons" transform="translate(-903.000000, -411.000000)" fill="#FF6E6E">
|
|
10
|
+
<g id="Icons" transform="translate(37.000000, 169.000000)">
|
|
11
|
+
<g id="star" transform="translate(858.000000, 234.000000)">
|
|
12
|
+
<g transform="translate(7.000000, 8.000000)" id="Shape">
|
|
13
|
+
<polygon points="27.865 31.83 17.615 26.209 7.462 32.009 9.553 20.362 0.99 12.335 12.532 10.758 17.394 0 22.436 10.672 34 12.047 25.574 20.22">
|
|
14
|
+
|
|
15
|
+
</g>
|
|
16
|
+
</g>
|
|
17
|
+
</g>
|
|
18
|
+
</g>
|
|
19
|
+
</g>
|
|
20
|
+
</svg>
|