forked from catdad/canvas-confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
645 lines (562 loc) Β· 17.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Component for drawing confetti on a canvas">
<meta name="author" content="Kiril Vatev">
<meta name="keywords" content="canvas,confetti,component,module,animation,javascript,catdad">
<meta name="theme-color" content="#393939">
<title>π</title>
<link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #212121;
color: #eee;
font-size: 1em;
font-family: 'Noto Sans', sans-serif;
}
* {
box-sizing: border-box;
}
.sprite {
display: none;
}
a.github-corner {
position: absolute;
top: 0;
right: 0;
width: 80px;
height: 80px;
}
h1, h2, .center {
text-align: center;
}
h2 {
padding: 0;
margin: 0.25em;
}
p {
margin: 0.5em;
}
.container {
position: relative;
max-width: 1000px;
width: 100%;
margin: 0 auto;
}
.group {
position: relative;
width: 100%;
margin: 40px 0;
padding-top: 16px;
border-top: 1px solid #3e3e3e;
border-radius: 20px;
}
.run {
padding: 10px 6px;
margin: 0.75em 0;
background: rgba(255,255,255,0.1);
border: none;
outline: none;
color: #eee;
font-weight: bold;
cursor: pointer;
user-select: none;
opacity: 0.6;
transition: opacity 100ms ease;
}
.group .run:hover {
opacity: 1;
}
.editor {
position: relative;
min-height: 100px;
width: 100%;
}
.editor.ace_dark.ace_editor {
background-color: #363636;
}
.editor.ace_dark .ace_gutter {
color: #8b8b8b;
background: #272727;
}
.flex-rows {
position: relative;
display: block;
width: 100%;
padding-bottom: 1em;
}
.description {
width: 94%;
margin: 10px auto;
padding: 0;
align-items: center;
line-height: 1.5;
}
.left {
flex-grow: 1;
display: flex;
flex-direction: column;
}
a.anchor {
position: relative;
color: currentColor;
text-decoration: none;
}
a.anchor:hover::before {
content: "π";
color: currentColor;
position: absolute;
left: -2rem;
top: 0;
transform: scale(0.75, 0.75);
}
footer {
font-size: 0.9rem;
text-align: center;
line-height: 2;
background: rgba(255,255,255,0.1);
}
footer span {
vertical-align: middle;
}
span.icon {
position: relative;
display: inline-block;
height: 1em;
width: 1em;
}
svg.icon {
position: absolute;
pointer-events: none;
left: 0;
width: 100%;
height: 100%;
fill: #e5e5e5;
}
footer a {
text-decoration: none;
color: white;
opacity: 0.85;
will-change: opacity;
}
footer a:hover {
opacity: 1;
}
.custom-canvas {
margin-top: 30px;
width: 100%;
max-width: 1000px;
height: 380px;
background: #363636;
}
@media (min-width: 44em) {
.container {
width: 95%;
}
.flex-rows {
display: flex;
flex-direction: row;
}
.description {
width: 66%;
padding: 0 0 0 1em;
}
}
</style>
<script>
// this page is a demo that is not built, so fudge the module.exports support
// define a global `module` so that the actual source file can use it
window.module = {};
</script>
<script src="src/confetti.js"></script>
<script>
// define the `module.exports` as the `confetti` global, the way that the
// cdn distributed file would
window.confetti = module.exports;
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.0/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify.min.js"></script>
</head>
<body>
<svg class="sprite" xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="code" viewBox="0 0 14 16">
<!-- Icon from GitHub Octicons - https://github.com/primer/octicons/ -->
<path d="M9.5 3L8 4.5 11.5 8 8 11.5 9.5 13 14 8 9.5 3 9.5 3ZM4.5 3L0 8 4.5 13 6 11.5 2.5 8 6 4.5 4.5 3 4.5 3Z"/>
</symbol>
<symbol id="heart" viewBox="0 0 12 16">
<!-- Icon from GitHub Octicons - https://github.com/primer/octicons/ -->
<path d="M11.2 3C10.68 2.37 9.95 2.05 9 2 8.03 2 7.31 2.42 6.8 3 6.29 3.58 6.02 3.92 6 4 5.98 3.92 5.72 3.58 5.2 3 4.68 2.42 4.03 2 3 2 2.05 2.05 1.31 2.38 0.8 3 0.28 3.61 0.02 4.28 0 5 0 5.52 0.09 6.52 0.67 7.67 1.25 8.82 3.01 10.61 6 13 8.98 10.61 10.77 8.83 11.34 7.67 11.91 6.51 12 5.5 12 5 11.98 4.28 11.72 3.61 11.2 2.98L11.2 3Z"/>
</symbol>
<symbol id="run" viewBox="0 0 10 10">
<path d="M2 2L8 6L2 10" />
</symbol>
<symbol id="octo" viewBox="0 0 80 80">
<!-- Adapted from Tim Holman's GitHub Corners - https://github.com/tholman/github-corners -->
<path d="M0 0L36.8 36.8 45.3 45.3 80 80 80 0 0 0ZM40.6 34.6C36.3 31.7 38.1 28.8 38.1 28.8 39 26.6 38.7 25.3 38.7 25.3 38.4 23 39.7 24.6 39.7 24.6 41 26.2 40.3 28.2 40.3 28.2 39.6 30.7 41 32.2 42.3 32.9L42.9 32.3C43.8 31.7 44.8 31.4 45.4 31.4 42.9 27.8 40.6 23.7 46.1 18.2 47.7 16.6 49.3 16 51.2 16 51.5 15.4 52.2 13.8 55 12.5 55 12.5 56.6 13.4 57.3 17.6 58.6 18.2 59.8 19.2 61.1 20.5 62.4 21.8 63.4 23 64 24.3 68.5 25.3 69.4 26.6 69.4 26.6 68.2 29.1 66.6 30.1 65.9 30.1 65.9 32 65.3 33.6 63.7 35.2 58.6 40.3 54.1 38.6 50.6 36 50.6 37 50.2 38 49 39.4Q45.9 42.8 45.2 43.6C44.7 44.2 45.3 45.3 45.3 45.3L36.8 36.8C36.8 36.8 37.8 37.4 38.4 36.8L40.6 34.6Z"/>
</symbol>
</defs>
</svg>
<a class="github-corner" href="https://github.com/catdad/canvas-confetti" aria-label="View source on Github">
<svg class="icon"><use xlink:href="#octo"></use></svg>
</a>
<h1>Canvas Confetti</h1>
<div class="container">
<div class="group" data-name="cannon">
<div class="flex-rows">
<div class="left">
<h2><a href="#basic" id="basic" class="anchor">Basic Cannon</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
The default mode... just your regular basic average blast of confetti. But it's still
a little cool, right?
</p>
</div>
</div>
<div class="editor"></div>
</div>
</div>
<div class="container">
<div class="group" data-name="random">
<div class="flex-rows">
<div class="left">
<h2><a href="#random" id="random" class="anchor">Random Direction</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
Go crazy with some randomness. Shoot a random amount of confetti in random directions.
(Go ahead... you know you want to click that button more than once.)
</p>
</div>
</div>
<div class="editor"></div>
</div>
</div>
<div class="container">
<div class="group" data-name="realistic">
<div class="flex-rows">
<div class="left">
<h2><a href="#realistic" id="realistic" class="anchor">Realistic Look</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
If you happened to get curious and changed the particle count to 400 or so, you saw
something disappointing. An even "flattened cone" look to the confetti, making it look
way too perfect and ruining the illusion. We can fix that by mixing a few effects together.
</p>
</div>
</div>
<div class="editor"></div>
</div>
</div>
<div class="container">
<div class="group" data-name="fireworks">
<div class="flex-rows">
<div class="left">
<h2><a href="#fireworks" id="fireworks" class="anchor">Fireworks</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
Why click a button repeatedly when you can have code do it for you? Shoot some firework
of confetti from the sides of page so you can still read the content in the center.
</p>
</div>
</div>
<div class="editor"></div>
</div>
</div>
<div class="container">
<div class="group" data-name="snow">
<div class="flex-rows">
<div class="left">
<h2><a href="#snow" id="snow" class="anchor">Snow</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
The effect is not limited to crazy rapid fire of confetti though. You can create a
wintery mood with gently falling particles across the entire page.
</p>
</div>
</div>
<div class="editor"></div>
</div>
</div>
<div class="container">
<div class="group" data-name="continuous">
<div class="flex-rows">
<div class="left">
<h2><a href="#continuous" id="continuous" class="anchor">School Pride</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
But if you are into crazy rapid fire of confetti, what could be a better use than
to show everyone what you are all about? Tell people where you are from with two
confetti cannons from either side of the page.
</p>
<p class="center">π° Go Buckeyes! π°</p>
</div>
</div>
<div class="editor"></div>
</div>
</div>
<div class="container">
<div class="group" data-name="custom">
<div class="flex-rows">
<div class="left">
<h2><a href="#custom-canvas" id="custom-canvas" class="anchor">Custom Canvas</a></h2>
<button class="run">
Run
<span class="icon">
<svg class="icon"><use xlink:href="#run"></use></svg>
</span>
</button>
</div>
<div class="description">
<p>
But if you just hate confetti all over the place, there's something here for you
as well. You can limit where the confetti appear by providing your own canvas element.
</p>
</div>
</div>
<div class="editor"></div>
<div class="flex-rows">
<canvas id="my-canvas" class="custom-canvas"></canvas>
</div>
</div>
</div>
<footer>
<a href="https://github.com/catdad">
<span class="icon">
<svg class="icon"><use xlink:href="#code"></use></svg>
</span>
<span> with </span>
<span class="icon">
<svg class="icon"><use xlink:href="#heart"></use></svg>
</span>
<span> by <b>catdad</b></span>
</a>
</footer>
<script>
var modes = {
cannon: function cannon() {
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 }
});
},
random: function random() {
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}
confetti({
angle: randomInRange(55, 125),
spread: randomInRange(50, 70),
particleCount: randomInRange(50, 100),
origin: { y: 0.6 }
});
},
realistic: function realistic() {
var count = 200;
var defaults = {
origin: { y: 0.7 }
};
function fire(particleRatio, opts) {
confetti(Object.assign({}, defaults, opts, {
particleCount: Math.floor(count * particleRatio)
}));
}
fire(0.25, {
spread: 26,
startVelocity: 55,
});
fire(0.2, {
spread: 60,
});
fire(0.35, {
spread: 100,
decay: 0.91,
});
fire(0.1, {
spread: 120,
startVelocity: 25,
decay: 0.92,
});
fire(0.1, {
spread: 120,
startVelocity: 45,
});
},
fireworks: function fireworks() {
var duration = 15 * 1000;
var animationEnd = Date.now() + duration;
var defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 0 };
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}
var interval = setInterval(function () {
var timeLeft = animationEnd - Date.now();
if (timeLeft <= 0) {
return clearInterval(interval);
}
var particleCount = 50 * (timeLeft / duration);
// since particles fall down, start a bit higher than random
confetti(Object.assign({}, defaults, { particleCount, origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 } }));
confetti(Object.assign({}, defaults, { particleCount, origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 } }));
}, 250);
},
snow: function snow() {
var duration = 15 * 1000;
var animationEnd = Date.now() + duration;
var skew = 1;
(function frame() {
var timeLeft = animationEnd - Date.now();
var ticks = Math.max(200, 500 * (timeLeft / duration));
skew = Math.max(0.8, skew - 0.001);
confetti({
particleCount: 1,
startVelocity: 0,
ticks: ticks,
gravity: 0.5,
origin: {
x: Math.random(),
// since particles fall down, skew start toward the top
y: (Math.random() * skew) - 0.2
},
colors: ['#ffffff'],
shapes: ['circle']
});
if (timeLeft > 0) {
requestAnimationFrame(frame);
}
}());
},
continuous: function continuous() {
var end = Date.now() + (15 * 1000);
// go Buckeyes!
var colors = ['#bb0000', '#ffffff'];
(function frame() {
confetti({
particleCount: 2,
angle: 60,
spread: 55,
origin: { x: 0 },
colors: colors
});
confetti({
particleCount: 2,
angle: 120,
spread: 55,
origin: { x: 1 },
colors: colors
});
if (Date.now() < end) {
requestAnimationFrame(frame);
}
}());
},
custom: function() {
var canvas = document.getElementById('my-canvas');
// you should only initialize a canvas once, so save this function
// we'll save it to the canvas itself for the purpose of this demo
canvas.confetti = canvas.confetti || confetti.create(canvas, { resize: true });
canvas.confetti({
spread: 70,
origin: { y: 1.2 }
});
}
};
function auto(mode, count, time) {
count = count || 10;
var interval = setInterval(function () {
if (count--) {
return modes[mode]();
}
clearInterval(interval);
}, time || 200);
}
function pretty(val) {
return js_beautify(val, { indent_size: 2, brace_style: 'preserve-inline', });
}
function getCode(name) {
// pretty-print the code, since we will use minified code in production
var code = pretty(modes[name].toString());
// take out the function wrapper, trim all whitespace
code = code.split('\n').slice(1).slice(0, -1).map(function (s) {
return s.trim();
}).join('\n');
// pretty-print again
return pretty(code);
}
[].forEach.call(document.querySelectorAll('.group'), function (group) {
var name = group.getAttribute('data-name');
var button = group.querySelector('.run');
var codeElem = group.querySelector('.editor');
var editor = ace.edit(codeElem);
editor.setTheme('ace/theme/monokai');
editor.session.setMode('ace/mode/javascript');
editor.session.setUseSoftTabs(true);
editor.session.setTabSize(2);
editor.session.setValue(getCode(name));
var count = editor.session.getLength();
// set height so that all code is visible
codeElem.style.minHeight = (14 * count) + 1 + 'px';
codeElem.style.height = (count) + 'rem';
button.onclick = function (ev) {
// stop mobile browsers from zooming when clicking
// buttons repeatedly really fast
ev.preventDefault();
try {
eval(editor.getValue());
} catch (e) {
console.error(e);
}
};
});
</script>
<script src="/analytics.js"></script>
</body>
</html>