Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.88 KB | None | 0 0
  1. function ParticleSlider(a) {
  2. var b = this;
  3. b.sliderId = "particle-slider", b.color = "#fff", b.hoverColor = "#88f", b.width = 0, b.height = 20, b.ptlGap = 0, b.ptlSize = 1, b.slideDelay = 10, b.arrowPadding = 10, b.showArrowControls = !0, b.onNextSlide = null, b.onWidthChange = null, b.onHeightChange = null, b.onSizeChange = null, b.monochrome = !1, b.mouseForce = 1e4, b.restless = !0, b.imgs = [];
  4. if (a) {
  5. var c = ["color", "hoverColor", "width", "height", "ptlGap", "ptlSize", "slideDelay", "arrowPadding", "sliderId", "showArrowControls", "onNextSlide", "monochrome", "mouseForce", "restless", "imgs", "onSizeChange", "onWidthChange", "onHeightChange"];
  6. for (var d = 0, e = c.length; d < e; d++) a[c[d]] && (b[c[d]] = a[c[d]])
  7. }
  8. b.$container = b.$("#" + b.sliderId), b.$$children = b.$container.childNodes, b.$controlsContainer = b.$(".controls"), b.$$slides = b.$(".slide", b.$(".slides").childNodes, !0), b.$controlLeft = null, b.$controlRight = null, b.$canv = b.$(".draw"), b.$srcCanv = document.createElement("canvas"), b.$srcCanv.style.display = "none", b.$container.appendChild(b.$srcCanv), b.$prevCanv = document.createElement("canvas"), b.$prevCanv.style.display = "none", b.$container.appendChild(b.$prevCanv), b.$nextCanv = document.createElement("canvas"), b.$nextCanv.style.display = "none", b.$container.appendChild(b.$nextCanv), b.$overlay = document.createElement("p"), b.$container.appendChild(b.$overlay), b.imgControlPrev = null, b.imgControlNext = null, b.$$slides.length <= 1 && (b.showArrowControls = !1), b.$controlsContainer && b.$controlsContainer.childNodes && b.showArrowControls == !0 ? (b.$controlLeft = b.$(".left", b.$controlsContainer.childNodes), b.$controlRight = b.$(".right", b.$controlsContainer.childNodes), b.imgControlPrev = new Image, b.imgControlNext = new Image, b.imgControlPrev.onload = function() {
  9. b.$prevCanv.height = this.height, b.$prevCanv.width = this.width, b.loadingStep()
  10. }, b.imgControlNext.onload = function() {
  11. b.$nextCanv.height = this.height, b.$nextCanv.width = this.width, b.loadingStep()
  12. }, b.imgControlPrev.src = b.$controlLeft.getAttribute("data-src"), b.imgControlNext.src = b.$controlRight.getAttribute("data-src")) : b.showArrowControls = !1, b.width <= 0 && (b.width = b.$container.clientWidth), b.height <= 0 && (b.height = b.$container.clientHeight), b.mouseDownRegion = 0, b.colorArr = b.parseColor(b.color), b.hoverColorArr = b.parseColor(b.hoverColor), b.mx = -1, b.my = -1, b.swipeOffset = 0, b.cw = b.getCw(), b.ch = b.getCh(), b.frame = 0, b.nextSlideTimer = !1, b.currImg = 0, b.lastImg = 0, b.imagesLoaded = 0, b.pxlBuffer = {
  13. first: null
  14. }, b.recycleBuffer = {
  15. first: null
  16. }, b.ctx = b.$canv.getContext("2d"), b.srcCtx = b.$srcCanv.getContext("2d"), b.prevCtx = b.$prevCanv.getContext("2d"), b.nextCtx = b.$nextCanv.getContext("2d"), b.$canv.width = b.cw, b.$canv.height = b.ch, b.shuffle = function() {
  17. var a, b;
  18. for (var c = 0, d = this.length; c < d; c++) b = Math.floor(Math.random() * d), a = this[c], this[c] = this[b], this[b] = a
  19. }, Array.prototype.shuffle = b.shuffle, b.$canv.onmouseout = function() {
  20. b.mx = -1, b.my = -1, b.mouseDownRegion = 0
  21. }, b.$canv.onmousemove = function(a) {
  22. function c(a) {
  23. var c = 0,
  24. d = 0,
  25. e = typeof a == "string" ? b.$(a) : a;
  26. if (e) {
  27. c = e.offsetLeft, d = e.offsetTop;
  28. var f = document.getElementsByTagName("body")[0];
  29. while (e.offsetParent && e != f) c += e.offsetParent.offsetLeft, d += e.offsetParent.offsetTop, e = e.offsetParent
  30. }
  31. this.x = c, this.y = d
  32. }
  33. var d = new c(b.$container);
  34. b.mx = a.clientX - d.x + document.body.scrollLeft + document.documentElement.scrollLeft, b.my = a.clientY - d.y + document.body.scrollTop + document.documentElement.scrollTop
  35. }, b.$canv.onmousedown = function() {
  36. if (b.imgs.length > 1) {
  37. var a = 0;
  38. b.mx >= 0 && b.mx < b.arrowPadding * 2 + b.$prevCanv.width ? a = -1 : b.mx > 0 && b.mx > b.cw - (b.arrowPadding * 2 + b.$nextCanv.width) && (a = 1), b.mouseDownRegion = a
  39. }
  40. }, b.$canv.onmouseup = function() {
  41. if (b.imgs.length > 1) {
  42. var a = "";
  43. b.mx >= 0 && b.mx < b.arrowPadding * 2 + b.$prevCanv.width ? a = -1 : b.mx > 0 && b.mx > b.cw - (b.arrowPadding * 2 + b.$nextCanv.width) && (a = 1), a != 0 && b.mouseDownRegion != 0 && (a != b.mouseDownRegion && (a *= -1), b.nextSlideTimer && clearTimeout(b.nextSlideTimer), b.nextSlide(a)), b.mouseDownRegion = 0
  44. }
  45. };
  46. if (b.imgs.length == 0)
  47. for (var d = 0, e = b.$$slides.length; d < e; d++) {
  48. var f = new Image;
  49. b.imgs.push(f), f.src = b.$$slides[d].getAttribute("data-src")
  50. }
  51. b.imgs.length > 0 && (b.imgs[0].onload = function() {
  52. b.loadingStep()
  53. }), b.requestAnimationFrame(function() {
  54. b.nextFrame()
  55. })
  56. }
  57. var psParticle = function(a) {
  58. this.ps = a, this.ttl = null, this.color = a.colorArr, this.next = null, this.prev = null, this.gravityX = 0, this.gravityY = 0, this.x = Math.random() * a.cw, this.y = Math.random() * a.ch, this.velocityX = Math.random() * 10 - 9, this.velocityY = Math.random() * 10 - 9
  59. };
  60. psParticle.prototype.move = function() {
  61. var a = this.ps,
  62. b = this;
  63. if (this.ttl != null && this.ttl-- <= 0) a.swapList(b, a.pxlBuffer, a.recycleBuffer), this.ttl = null;
  64. else {
  65. var c = this.gravityX + a.swipeOffset - this.x,
  66. d = this.gravityY - this.y,
  67. e = Math.sqrt(Math.pow(c, 2) + Math.pow(d, 2)),
  68. f = Math.atan2(d, c),
  69. g = e * .01;
  70. a.restless == !0 ? g += Math.random() * .1 - .05 : g < .01 && (this.x = this.gravityX + .25, this.y = this.gravityY + .25);
  71. var h = 0,
  72. i = 0;
  73. if (a.mx >= 0 && a.mouseForce) {
  74. var j = this.x - a.mx,
  75. k = this.y - a.my;
  76. h = Math.min(a.mouseForce / (Math.pow(j, 2) + Math.pow(k, 2)), a.mouseForce), i = Math.atan2(k, j), typeof this.color == "function" && (i += Math.PI, h *= .001 + Math.random() * .1 - .05)
  77. } else h = 0, i = 0;
  78. this.velocityX += g * Math.cos(f) + h * Math.cos(i), this.velocityY += g * Math.sin(f) + h * Math.sin(i), this.velocityX *= .92, this.velocityY *= .92, this.x += this.velocityX, this.y += this.velocityY
  79. }
  80. }, ParticleSlider.prototype.Particle = psParticle, ParticleSlider.prototype.swapList = function(a, b, c) {
  81. var d = this;
  82. a == null ? a = new d.Particle(d) : b.first == a ? a.next != null ? (a.next.prev = null, b.first = a.next) : b.first = null : a.next == null ? a.prev.next = null : (a.prev.next = a.next, a.next.prev = a.prev), c.first == null ? (c.first = a, a.prev = null, a.next = null) : (a.next = c.first, c.first.prev = a, c.first = a, a.prev = null)
  83. }, ParticleSlider.prototype.parseColor = function(a) {
  84. var b, a = a.replace(" ", "");
  85. if (b = /^#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/.exec(a)) b = [parseInt(b[1], 16), parseInt(b[2], 16), parseInt(b[3], 16)];
  86. else if (b = /^#([\da-fA-F])([\da-fA-F])([\da-fA-F])/.exec(a)) b = [parseInt(b[1], 16) * 17, parseInt(b[2], 16) * 17, parseInt(b[3], 16) * 17];
  87. else if (b = /^rgba\(([\d]+),([\d]+),([\d]+),([\d]+|[\d]*.[\d]+)\)/.exec(a)) b = [+b[1], +b[2], +b[3], +b[4]];
  88. else if (b = /^rgb\(([\d]+),([\d]+),([\d]+)\)/.exec(a)) b = [+b[1], +b[2], +b[3]];
  89. else return null;
  90. isNaN(b[3]) && (b[3] = 1), b[3] *= 255;
  91. return b
  92. }, ParticleSlider.prototype.loadingStep = function() {
  93. var a = this;
  94. a.imagesLoaded++;
  95. if (a.imagesLoaded >= 3 || a.showArrowControls == !1) a.resize(), a.slideDelay > 0 && (a.nextSlideTimer = setTimeout(function() {
  96. a.nextSlide()
  97. }, 1e3 * a.slideDelay))
  98. }, ParticleSlider.prototype.$ = function(a, b, c) {
  99. var d = this;
  100. if (a[0] == ".") {
  101. var e = a.substr(1);
  102. b || (b = d.$$children);
  103. var f = [];
  104. for (var g = 0, h = b.length; g < h; g++) b[g].className && b[g].className == e && f.push(b[g]);
  105. return f.length == 0 ? null : f.length == 1 && !c ? f[0] : f
  106. }
  107. return document.getElementById(a.substr(1))
  108. }, ParticleSlider.prototype.nextFrame = function() {
  109. var a = this;
  110. a.mouseDownRegion == 1 && a.mx < a.cw / 2 || a.mouseDownRegion == -1 && a.mx > a.cw / 2 ? a.swipeOffset = a.mx - a.cw / 2 : a.swipeOffset = 0;
  111. var b = a.pxlBuffer.first,
  112. c = null;
  113. while (b != null) c = b.next, b.move(), b = c;
  114. a.drawParticles();
  115. if (a.frame++ % 25 == 0 && (a.cw != a.getCw() || a.ch != a.getCh())) {
  116. var d = a.getCh(),
  117. e = a.getCw();
  118. a.ch != e && typeof a.onWidthChange == "function" && a.onWidthChange(a, e), a.ch != d && typeof a.onHeightChange == "function" && a.onHeightChange(a, d), typeof a.onSizeChange == "function" && a.onSizeChange(a, e, d), a.resize()
  119. }
  120. setTimeout(function() {
  121. a.requestAnimationFrame(function() {
  122. a.nextFrame()
  123. })
  124. }, 15)
  125. }, ParticleSlider.prototype.nextSlide = function(a) {
  126. var b = this;
  127. b.nextSlideTimer != null && b.imgs.length > 1 ? (b.currImg = (b.currImg + b.imgs.length + (a ? a : 1)) % b.imgs.length, b.resize(), b.slideDelay > 0 && (b.nextSlideTimer = setTimeout(function() {
  128. b.nextSlide()
  129. }, 1e3 * b.slideDelay))) : b.slideDelay > 0 && (b.nextSlideTimer = setTimeout(function() {
  130. b.nextSlide()
  131. }, 1e3 * b.slideDelay)), typeof b.onNextSlide == "function" && b.onNextSlide(b.currImg)
  132. }, ParticleSlider.prototype.drawParticles = function() {
  133. var a = this,
  134. b = a.ctx.createImageData(a.cw, a.ch),
  135. c = b.data,
  136. d, e, f, g, h, i, j = a.pxlBuffer.first;
  137. while (j != null) {
  138. e = ~~j.x, f = ~~j.y;
  139. for (g = e; g < e + a.ptlSize && g >= 0 && g < a.cw; g++)
  140. for (h = f; h < f + a.ptlSize && h >= 0 && h < a.ch; h++) d = (h * b.width + g) * 4, i = typeof j.color == "function" ? j.color() : j.color, c[d + 0] = i[0], c[d + 1] = i[1], c[d + 2] = i[2], c[d + 3] = i[3];
  141. j = j.next
  142. }
  143. b.data = c, a.ctx.putImageData(b, 0, 0)
  144. }, ParticleSlider.prototype.getPixelFromImageData = function(a, b, c) {
  145. var d = this,
  146. e = [];
  147. for (var f = 0; f < a.width; f += d.ptlGap + 1)
  148. for (var g = 0; g < a.height; g += d.ptlGap + 1) i = (g * a.width + f) * 4, a.data[i + 3] > 0 && e.push({
  149. x: b + f,
  150. y: c + g,
  151. color: d.monochrome == !0 ? [d.colorArr[0], d.colorArr[1], d.colorArr[2], d.colorArr[3]] : [a.data[i], a.data[i + 1], a.data[i + 2], a.data[i + 3]]
  152. });
  153. return e
  154. }, ParticleSlider.prototype.init = function(a) {
  155. var b = this;
  156. if (b.imgs.length > 0) {
  157. b.$srcCanv.width = b.imgs[b.currImg].width, b.$srcCanv.height = b.imgs[b.currImg].height, b.srcCtx.clearRect(0, 0, b.$srcCanv.width, b.$srcCanv.height), b.srcCtx.drawImage(b.imgs[b.currImg], 0, 0);
  158. var c = b.getPixelFromImageData(b.srcCtx.getImageData(0, 0, b.$srcCanv.width, b.$srcCanv.height), ~~(b.cw / 2 - b.$srcCanv.width / 2), ~~(b.ch / 2 - b.$srcCanv.height / 2));
  159. if (b.showArrowControls == !0) {
  160. b.prevCtx.clearRect(0, 0, b.$prevCanv.width, b.$prevCanv.height), b.prevCtx.drawImage(b.imgControlPrev, 0, 0);
  161. var d = b.getPixelFromImageData(b.prevCtx.getImageData(0, 0, b.$prevCanv.width, b.$prevCanv.height), b.arrowPadding, ~~(b.ch / 2 - b.$prevCanv.height / 2));
  162. for (var e = 0, f = d.length; e < f; e++) d[e].color = function() {
  163. return b.mx >= 0 && b.mx < b.arrowPadding * 2 + b.$prevCanv.width ? b.hoverColorArr : b.colorArr
  164. }, c.push(d[e]);
  165. b.nextCtx.clearRect(0, 0, b.$nextCanv.width, b.$nextCanv.height), b.nextCtx.drawImage(b.imgControlNext, 0, 0);
  166. var g = b.getPixelFromImageData(b.nextCtx.getImageData(0, 0, b.$nextCanv.width, b.$nextCanv.height), b.cw - b.arrowPadding - b.$nextCanv.width, ~~(b.ch / 2 - b.$nextCanv.height / 2));
  167. for (var e = 0, f = g.length; e < f; e++) g[e].color = function() {
  168. return b.mx > 0 && b.mx > b.cw - (b.arrowPadding * 2 + b.$nextCanv.width) ? b.hoverColorArr : b.colorArr
  169. }, c.push(g[e])
  170. }
  171. if (b.currImg != b.lastImg || a == !0) c.shuffle(), b.lastImg = b.currImg;
  172. var h = b.pxlBuffer.first;
  173. for (var e = 0, f = c.length; e < f; e++) {
  174. var i = null;
  175. h != null ? (i = h, h = h.next) : (b.swapList(b.recycleBuffer.first, b.recycleBuffer, b.pxlBuffer), i = b.pxlBuffer.first), i.gravityX = c[e].x, i.gravityY = c[e].y, i.color = c[e].color
  176. }
  177. while (h != null) h.ttl = ~~(Math.random() * 10), h.gravityY = ~~(b.ch * Math.random()), h.gravityX = ~~(b.cw * Math.random()), h = h.next;
  178. b.$overlay.innerHTML = b.$$slides[b.currImg].innerHTML
  179. }
  180. }, ParticleSlider.prototype.getCw = function() {
  181. var a = this;
  182. return Math.min(document.body.clientWidth, a.width, a.$container.clientWidth)
  183. }, ParticleSlider.prototype.getCh = function() {
  184. var a = this;
  185. return Math.min(document.body.clientHeight, a.height, a.$container.clientHeight)
  186. }, ParticleSlider.prototype.resize = function() {
  187. var a = this;
  188. a.cw = a.getCw(), a.ch = a.getCh(), a.$canv.width = a.cw, a.$canv.height = a.ch, a.init(!0)
  189. }, ParticleSlider.prototype.setColor = function(a) {
  190. var b = this;
  191. b.colorArr = b.parseColor(a)
  192. }, ParticleSlider.prototype.setHoverColor = function(a) {
  193. var b = this;
  194. b.hoverColorArr = b.parseColor(a)
  195. }, ParticleSlider.prototype.requestAnimationFrame = function(a) {
  196. var b = this,
  197. c = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(a) {
  198. window.setTimeout(a, 1e3 / 60)
  199. };
  200. c(a)
  201. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement