Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. //
  2. ! function(a) {
  3. var b = "object" == typeof self && self.self === self && self || "object" == typeof global && global.global === global && global;
  4. "function" == typeof define && define.amd ? define(["exports"], function(c) {
  5. b.ParticleNetwork = a(b, c)
  6. }) : "object" == typeof module && module.exports ? module.exports = a(b, {}) : b.ParticleNetwork = a(b, {})
  7. }(function(a, b) {
  8. var c = function(a) {
  9. this.canvas = a.canvas, this.g = a.g, this.particleColor = a.options.particleColor, this.x = Math.random() * this.canvas.width, this.y = Math.random() * this.canvas.height, this.velocity = {
  10. x: (Math.random() - .5) * a.options.velocity,
  11. y: (Math.random() - .5) * a.options.velocity
  12. }
  13. };
  14. return c.prototype.update = function() {
  15. (this.x > this.canvas.width + 20 || this.x < -20) && (this.velocity.x = -this.velocity.x), (this.y > this.canvas.height + 20 || this.y < -20) && (this.velocity.y = -this.velocity.y), this.x += this.velocity.x, this.y += this.velocity.y
  16. }, c.prototype.h = function() {
  17. this.g.beginPath(), this.g.fillStyle = this.particleColor, this.g.globalAlpha = .7, this.g.arc(this.x, this.y, 1.5, 0, 2 * Math.PI), this.g.fill()
  18. }, b = function(a, b) {
  19. this.i = a, this.i.size = {
  20. width: this.i.offsetWidth,
  21. height: this.i.offsetHeight
  22. }, b = void 0 !== b ? b : {}, this.options = {
  23. particleColor: void 0 !== b.particleColor ? b.particleColor : "#fff",
  24. background: void 0 !== b.background ? b.background : "#1a252f",
  25. interactive: void 0 !== b.interactive ? b.interactive : !0,
  26. velocity: this.setVelocity(b.speed),
  27. density: this.j(b.density)
  28. }, this.init()
  29. }, b.prototype.init = function() {
  30. if (this.k = document.createElement("div"), this.i.appendChild(this.k), this.l(this.k, {
  31. position: "absolute",
  32. top: 0,
  33. left: 0,
  34. bottom: 0,
  35. right: 0,
  36. "z-index": 0
  37. }), /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(this.options.background)) this.l(this.k, {
  38. background: this.options.background
  39. });
  40. else {
  41. if (!/\.(gif|jpg|jpeg|tiff|png)$/i.test(this.options.background)) return console.error("#333"), !1;
  42. this.l(this.k, {
  43. background: 'url("' + this.options.background + '") no-repeat center',
  44. "background-size": "cover"
  45. })
  46. }
  47. if (!/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(this.options.particleColor)) return console.error("Please specify a valid particleColor hexadecimal color"), !1;
  48. this.canvas = document.createElement("canvas"), this.i.appendChild(this.canvas), this.g = this.canvas.getContext("2d"), this.canvas.width = this.i.size.width, this.canvas.height = this.i.size.height, this.l(this.i, {
  49. position: "absolute"
  50. }), this.l(this.canvas, {
  51. "z-index": "1",
  52. position: "absolute"
  53. }), window.addEventListener("resize", function() {
  54. return this.i.offsetWidth === this.i.size.width && this.i.offsetHeight === this.i.size.height ? !1 : (this.canvas.width = this.i.size.width = this.i.offsetWidth, this.canvas.height = this.i.size.height = this.i.offsetHeight, clearTimeout(this.m), void(this.m = setTimeout(function() {
  55. this.o = [];
  56. for (var a = 0; a < this.canvas.width * this.canvas.height / this.options.density; a++) this.o.push(new c(this));
  57. this.options.interactive && this.o.push(this.p), requestAnimationFrame(this.update.bind(this))
  58. }.bind(this), 500)))
  59. }.bind(this)), this.o = [];
  60. for (var a = 0; a < this.canvas.width * this.canvas.height / this.options.density; a++) this.o.push(new c(this));
  61. this.options.interactive && (this.p = new c(this), this.p.velocity = {
  62. x: 0,
  63. y: 0
  64. }, this.o.push(this.p), this.canvas.addEventListener("mousemove", function(a) {
  65. this.p.x = a.clientX - this.canvas.offsetLeft, this.p.y = a.clientY - this.canvas.offsetTop
  66. }.bind(this)), this.canvas.addEventListener("mouseup", function(a) {
  67. this.p.velocity = {
  68. x: (Math.random() - .5) * this.options.velocity,
  69. y: (Math.random() - .5) * this.options.velocity
  70. }, this.p = new c(this), this.p.velocity = {
  71. x: 0,
  72. y: 0
  73. }, this.o.push(this.p)
  74. }.bind(this))), requestAnimationFrame(this.update.bind(this))
  75. }, b.prototype.update = function() {
  76. this.g.clearRect(0, 0, this.canvas.width, this.canvas.height), this.g.globalAlpha = 1;
  77. for (var a = 0; a < this.o.length; a++) {
  78. this.o[a].update(), this.o[a].h();
  79. for (var b = this.o.length - 1; b > a; b--) {
  80. var c = Math.sqrt(Math.pow(this.o[a].x - this.o[b].x, 2) + Math.pow(this.o[a].y - this.o[b].y, 2));
  81. c > 120 || (this.g.beginPath(), this.g.strokeStyle = this.options.particleColor, this.g.globalAlpha = (120 - c) / 120, this.g.lineWidth = .7, this.g.moveTo(this.o[a].x, this.o[a].y), this.g.lineTo(this.o[b].x, this.o[b].y), this.g.stroke())
  82. }
  83. }
  84. 0 !== this.options.velocity && requestAnimationFrame(this.update.bind(this))
  85. }, b.prototype.setVelocity = function(a) {
  86. return "fast" === a ? 1 : "slow" === a ? .33 : "none" === a ? 0 : .66
  87. }, b.prototype.j = function(a) {
  88. return "high" === a ? 5e3 : "low" === a ? 2e4 : isNaN(parseInt(a, 10)) ? 1e4 : a
  89. }, b.prototype.l = function(a, b) {
  90. for (var c in b) a.style[c] = b[c]
  91. }, b
  92. });
  93.  
  94. // Initialisation
  95.  
  96. var canvasDiv = document.getElementById('particle-canvas');
  97. var options = {
  98. particleColor: '#fff',
  99. background: '/css/img/black.jpg',
  100. interactive: true,
  101. speed: 'slow',
  102. density: 'high'
  103. };
  104. var particleCanvas = new ParticleNetwork(canvasDiv, options);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement