Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. const opacity = x => ((confetti.Opacity) ? Math.random() + 0.5 :1);
  2. const rotate = x => ((confetti.Rotation) ? Math.floor(Math.random() * 70):0);
  3. const size = x =>((confetti.Size) ? 1 + (Math.random() - 1.3) : 1);
  4. const defaultopts = {
  5. rows: 10,
  6. columns: 10,
  7. Opacity: false,
  8. Size: false,
  9. Rotation: true,
  10. Icons: [
  11. './confetti/Ellipse.svg',
  12. './confetti/Vector.svg',
  13. './confetti/Line.svg',
  14. './confetti/Polygon.svg',
  15. './confetti/Text.svg',
  16. './confetti/Star.svg'
  17. ],
  18. Color : ['#4DA1DA', '#A4D77C']
  19. };
  20. const generateConfetti = (element,opts) => {
  21. opts=Object.assign({},defaultopts, opts);
  22. var style=document.createElement('style');
  23. style.innerHTML=""+element+"-parent{overflow:hidden;position:relative;left:0;z-index:-1;width:100%;height:50vh}"+element+"{width:100%;height:50vh;padding:0;position:absolute}.confetti-cell{position:absolute;top:0;left:0;width:20%;height:20%;opacity:0;animation:350ms fadeIn cubic-bezier(0.22,0.61,0.36,1);animation-fill-mode:forwards}.confetti-cell .icon{position:absolute;top:0;left:0;background-position:50% 50%;background-repeat:no-repeat;background-size:contain;width:50px;height:50px;display:block;-webkit-mask-size:contain;-webkit-mask-repeat:no-repeat}@keyframes fadeIn{100%{opacity:1;transform:scale(1) translate3d(0,0,0)}}.confetti-cell{width:' + (100 / confetti.columns) + '%;height:' + (100 / confetti.rows) + '%;}";
  24. document.body.appendChild(style);
  25. var parent = document.createElement('div');
  26. parent.setAttribute('class',element.split('.')[1]+'-parent');
  27. Array.prototype.forEach.call(document.querySelectorAll(element), function(element) {
  28. element.parentNode.insertBefore(parent, element);
  29. parent.appendChild(element);
  30. parent=parent.cloneNode(true);
  31.  
  32. });
  33. for(var i = 0;i < opts.rows; i++) {
  34. for(var j = 0;j < opts.columns; j++) {
  35. let icon = Math.floor(Math.random() * opts.Icons.length);
  36. let cell = document.createElement('div');
  37. cell.setAttribute("style",'top:' + (100 / opts.rows) * i + '%;left:' + (100 / opts.columns) * j +'%;animation-delay:' + (j * 100) + 'ms;');
  38. cell.setAttribute("class","confetti-cell");
  39. cell.innerHTML='<i class="icon i'+ icon +'" style="background-color:'+opts.Color[Math.floor(Math.random() * 4)]+';top:'+Math.floor(Math.random() * 100)+'%;left:'+Math.floor(Math.random() * 100)+'%;opacity:' + opacity()+ ';transform:rotate(' + rotate() + 'deg) scale(' + size() + ');-webkit-mask-image: url('+opts.Icons[icon]+');"></i>';
  40. Array.prototype.forEach.call(document.querySelectorAll(element), function(element) {
  41. element.appendChild(cell);
  42. cell = cell.cloneNode(true);
  43.  
  44. });
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement