jquery.particleground.min.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Particleground
  3. *
  4. * @author Jonathan Nicol - @mrjnicol
  5. * @version 1.0.1
  6. * @description Creates a canvas based particle system background
  7. *
  8. * Inspired by:
  9. * http://requestlab.fr/
  10. * http://disruptivebydesign.com/
  11. *
  12. * @license The MIT License (MIT)
  13. *
  14. * Copyright (c) 2014 Jonathan Nicol - @mrjnicol
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. * THE SOFTWARE.
  33. */
  34. !function (a) {
  35. function b(b, d) {
  36. function e() {
  37. if (w) {
  38. $canvas = a('<canvas class="pg-canvas"></canvas>'), v.prepend($canvas), p = $canvas[0], q = p.getContext("2d"), f();
  39. for (var b = Math.round(p.width * p.height / d.density), c = 0; b > c; c++) {
  40. var e = new l;
  41. e.setStackPos(c), x.push(e)
  42. }
  43. a(window).on("resize", function () {
  44. h()
  45. }), a(document).on("mousemove", function (a) {
  46. y = a.pageX, z = a.pageY
  47. }), B && !A && window.addEventListener("deviceorientation", function () {
  48. D = Math.min(Math.max(-event.beta, -30), 30), C = Math.min(Math.max(-event.gamma, -30), 30)
  49. }, !0), g(), o("onInit")
  50. }
  51. }
  52. function f() {
  53. p.width = v.width(), p.height = v.height(), q.fillStyle = d.dotColor, q.strokeStyle = d.lineColor, q.lineWidth = d.lineWidth
  54. }
  55. function g() {
  56. if (w) {
  57. s = a(window).width(), t = a(window).height(), q.clearRect(0, 0, p.width, p.height);
  58. for (var b = 0; b < x.length; b++) x[b].updatePosition();
  59. for (var b = 0; b < x.length; b++) x[b].draw();
  60. E || (r = requestAnimationFrame(g))
  61. }
  62. }
  63. function h() {
  64. for (f(), i = x.length - 1; i >= 0; i--) (x[i].position.x > v.width() || x[i].position.y > v.height()) && x.splice(i, 1);
  65. var a = Math.round(p.width * p.height / d.density);
  66. if (a > x.length) for (; a > x.length;) {
  67. var b = new l;
  68. x.push(b)
  69. } else a < x.length && x.splice(a);
  70. for (i = x.length - 1; i >= 0; i--) x[i].setStackPos(i)
  71. }
  72. function j() {
  73. E = !0
  74. }
  75. function k() {
  76. E = !1, g()
  77. }
  78. function l() {
  79. switch (this.stackPos, this.active = !0, this.layer = Math.ceil(3 * Math.random()), this.parallaxOffsetX = 0, this.parallaxOffsetY = 0, this.position = {x: Math.ceil(Math.random() * p.width), y: Math.ceil(Math.random() * p.height)}, this.speed = {}, d.directionX) {
  80. case"left":
  81. this.speed.x = +(-d.maxSpeedX + Math.random() * d.maxSpeedX - d.minSpeedX).toFixed(2);
  82. break;
  83. case"right":
  84. this.speed.x = +(Math.random() * d.maxSpeedX + d.minSpeedX).toFixed(2);
  85. break;
  86. default:
  87. this.speed.x = +(-d.maxSpeedX / 2 + Math.random() * d.maxSpeedX).toFixed(2), this.speed.x += this.speed.x > 0 ? d.minSpeedX : -d.minSpeedX
  88. }
  89. switch (d.directionY) {
  90. case"up":
  91. this.speed.y = +(-d.maxSpeedY + Math.random() * d.maxSpeedY - d.minSpeedY).toFixed(2);
  92. break;
  93. case"down":
  94. this.speed.y = +(Math.random() * d.maxSpeedY + d.minSpeedY).toFixed(2);
  95. break;
  96. default:
  97. this.speed.y = +(-d.maxSpeedY / 2 + Math.random() * d.maxSpeedY).toFixed(2), this.speed.x += this.speed.y > 0 ? d.minSpeedY : -d.minSpeedY
  98. }
  99. }
  100. function m(a, b) {
  101. return b ? void(d[a] = b) : d[a]
  102. }
  103. function n() {
  104. v.find(".pg-canvas").remove(), o("onDestroy"), v.removeData("plugin_" + c)
  105. }
  106. function o(a) {
  107. void 0 !== d[a] && d[a].call(u)
  108. }
  109. var p, q, r, s, t, u = b, v = a(b), w = !!document.createElement("canvas").getContext, x = [], y = 0, z = 0, A = !navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|BB10|mobi|tablet|opera mini|nexus 7)/i), B = !!window.DeviceOrientationEvent, C = 0, D = 0, E = !1;
  110. return d = a.extend({}, a.fn[c].defaults, d), l.prototype.draw = function () {
  111. q.beginPath(), q.arc(this.position.x + this.parallaxOffsetX, this.position.y + this.parallaxOffsetY, d.particleRadius / 2, 0, 2 * Math.PI, !0), q.closePath(), q.fill(), q.beginPath();
  112. for (var a = x.length - 1; a > this.stackPos; a--) {
  113. var b = x[a], c = this.position.x - b.position.x, e = this.position.y - b.position.y, f = Math.sqrt(c * c + e * e).toFixed(2);
  114. f < d.proximity && (q.moveTo(this.position.x + this.parallaxOffsetX, this.position.y + this.parallaxOffsetY), d.curvedLines ? q.quadraticCurveTo(Math.max(b.position.x, b.position.x), Math.min(b.position.y, b.position.y), b.position.x + b.parallaxOffsetX, b.position.y + b.parallaxOffsetY) : q.lineTo(b.position.x + b.parallaxOffsetX, b.position.y + b.parallaxOffsetY))
  115. }
  116. q.stroke(), q.closePath()
  117. }, l.prototype.updatePosition = function () {
  118. if (d.parallax) {
  119. if (B && !A) {
  120. var a = (s - 0) / 60;
  121. pointerX = (C - -30) * a + 0;
  122. var b = (t - 0) / 60;
  123. pointerY = (D - -30) * b + 0
  124. } else pointerX = y, pointerY = z;
  125. this.parallaxTargX = (pointerX - s / 2) / (d.parallaxMultiplier * this.layer), this.parallaxOffsetX += (this.parallaxTargX - this.parallaxOffsetX) / 10, this.parallaxTargY = (pointerY - t / 2) / (d.parallaxMultiplier * this.layer), this.parallaxOffsetY += (this.parallaxTargY - this.parallaxOffsetY) / 10
  126. }
  127. switch (d.directionX) {
  128. case"left":
  129. this.position.x + this.speed.x + this.parallaxOffsetX < 0 && (this.position.x = v.width() - this.parallaxOffsetX);
  130. break;
  131. case"right":
  132. this.position.x + this.speed.x + this.parallaxOffsetX > v.width() && (this.position.x = 0 - this.parallaxOffsetX);
  133. break;
  134. default:
  135. (this.position.x + this.speed.x + this.parallaxOffsetX > v.width() || this.position.x + this.speed.x + this.parallaxOffsetX < 0) && (this.speed.x = -this.speed.x)
  136. }
  137. switch (d.directionY) {
  138. case"up":
  139. this.position.y + this.speed.y + this.parallaxOffsetY < 0 && (this.position.y = v.height() - this.parallaxOffsetY);
  140. break;
  141. case"down":
  142. this.position.y + this.speed.y + this.parallaxOffsetY > v.height() && (this.position.y = 0 - this.parallaxOffsetY);
  143. break;
  144. default:
  145. (this.position.y + this.speed.y + this.parallaxOffsetY > v.height() || this.position.y + this.speed.y + this.parallaxOffsetY < 0) && (this.speed.y = -this.speed.y)
  146. }
  147. this.position.x += this.speed.x, this.position.y += this.speed.y
  148. }, l.prototype.setStackPos = function (a) {
  149. this.stackPos = a
  150. }, e(), {option: m, destroy: n, start: k, pause: j}
  151. }
  152. var c = "particleground";
  153. a.fn[c] = function (d) {
  154. if ("string" == typeof arguments[0]) {
  155. var e, f = arguments[0], g = Array.prototype.slice.call(arguments, 1);
  156. return this.each(function () {
  157. a.data(this, "plugin_" + c) && "function" == typeof a.data(this, "plugin_" + c)[f] && (e = a.data(this, "plugin_" + c)[f].apply(this, g))
  158. }), void 0 !== e ? e : this
  159. }
  160. return "object" != typeof d && d ? void 0 : this.each(function () {
  161. a.data(this, "plugin_" + c) || a.data(this, "plugin_" + c, new b(this, d))
  162. })
  163. }, a.fn[c].defaults = {
  164. minSpeedX: .1, maxSpeedX: .7, minSpeedY: .1, maxSpeedY: .7, directionX: "center", directionY: "center", density: 1e4, dotColor: "#666666", lineColor: "#666666", particleRadius: 7, lineWidth: 1, curvedLines: !1, proximity: 100, parallax: !0, parallaxMultiplier: 5, onInit: function () {
  165. }, onDestroy: function () {
  166. }
  167. }
  168. }(jQuery), /**
  169. * requestAnimationFrame polyfill by Erik M枚ller. fixes from Paul Irish and Tino Zijdel
  170. * @see: http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  171. * @see: http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  172. * @license: MIT license
  173. */
  174. function () {
  175. for (var a = 0, b = ["ms", "moz", "webkit", "o"], c = 0; c < b.length && !window.requestAnimationFrame; ++c) window.requestAnimationFrame = window[b[c] + "RequestAnimationFrame"], window.cancelAnimationFrame = window[b[c] + "CancelAnimationFrame"] || window[b[c] + "CancelRequestAnimationFrame"];
  176. window.requestAnimationFrame || (window.requestAnimationFrame = function (b) {
  177. var c = (new Date).getTime(), d = Math.max(0, 16 - (c - a)), e = window.setTimeout(function () {
  178. b(c + d)
  179. }, d);
  180. return a = c + d, e
  181. }), window.cancelAnimationFrame || (window.cancelAnimationFrame = function (a) {
  182. clearTimeout(a)
  183. })
  184. }();