jparticle.jquery.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. (function initJParticle( $ ){
  2. "use strict";
  3. var createParticlesSandbox, Utils;
  4. Utils = {};
  5. /*
  6. * Create jParticle animation.
  7. * @param {Object} options Few jParticle options.
  8. * @return {Object} jQuery object for chaining.
  9. */
  10. $.fn.jParticle = function jParticle( options ){
  11. this.each(function( _, el ){
  12. if ( typeof el.sandbox === 'object' ) {
  13. $( el ).removeJParticle();
  14. }
  15. el.sandbox = createParticlesSandbox( el, options );
  16. });
  17. return this;
  18. };
  19. /*
  20. * Remove jParticle canvas.
  21. * @return {Object} jQuery object for chaining.
  22. */
  23. $.fn.removeJParticle = function removeJParticle(){
  24. this.each(function( _, el ){
  25. if ( el.sandbox ) {
  26. el.sandbox.remove();
  27. delete el.sandbox;
  28. }
  29. });
  30. return this;
  31. };
  32. /*
  33. * Freeze jParticle animation.
  34. * @return {Object} jQuery object for chaining.
  35. */
  36. $.fn.freezeJParticle = function freezeJParticle(){
  37. this.each(function( _, el ){
  38. if ( el.sandbox ) {
  39. el.sandbox.freeze();
  40. }
  41. });
  42. return this;
  43. };
  44. /*
  45. * Unfreeze jParticle animation.
  46. * @return {Object} jQuery object for chaining.
  47. */
  48. $.fn.unfreezeJParticle = function unfreezeJParticle(){
  49. this.each(function( _, el ){
  50. if ( el.sandbox ) {
  51. el.sandbox.unfreeze();
  52. }
  53. });
  54. return this;
  55. };
  56. /*
  57. * Create a particles sandbox instance.
  58. * @param {Object} element Element for the sandbox.
  59. * @param {Object} params Few sandbox's params.
  60. * @return {Object} Particles sandbox object.
  61. */
  62. createParticlesSandbox = function createParticlesSandbox( element, params ){
  63. var ParticlesSandbox, createParticle;
  64. ParticlesSandbox = {};
  65. ParticlesSandbox.canvas = {};
  66. ParticlesSandbox.mouse = {};
  67. ParticlesSandbox.particles = [];
  68. ParticlesSandbox.isAnimated = false;
  69. /*
  70. * Initialize the sandbox
  71. * @param {Object} element Element for the sandbox.
  72. * @param {Object} params Few sandbox's params.
  73. */
  74. ParticlesSandbox.initialize = function initialize( element, params ){
  75. ParticlesSandbox.initParams( params );
  76. ParticlesSandbox.initHTML( element );
  77. ParticlesSandbox.initParticles();
  78. ParticlesSandbox.initEvents();
  79. ParticlesSandbox.initAnimation();
  80. };
  81. /*
  82. * Initialize sandbox's params.
  83. * @param {Object} params Few sandbox's params.
  84. */
  85. ParticlesSandbox.initParams = function initParams( params ){
  86. if ( params && params.color && (!params.particle || ( params.particle && !params.particle.color ) ) ) {
  87. if ( !params.particle ) {
  88. params.particle = {};
  89. }
  90. params.particle.color = params.color;
  91. }
  92. ParticlesSandbox.params = $.extend({
  93. particlesNumber: 100,
  94. linkDist: 50,
  95. createLinkDist: 150,
  96. disableLinks: false,
  97. disableMouse: false,
  98. background: 'black',
  99. color: 'white',
  100. width: null,
  101. height: null,
  102. linksWidth: 1
  103. }, params );
  104. };
  105. /*
  106. * Initialize the sandbox's html.
  107. * @param {Object} element Element for the sandbox.
  108. */
  109. ParticlesSandbox.initHTML = function initHTML( element ){
  110. var canvas;
  111. canvas = ParticlesSandbox.canvas;
  112. canvas.container = $( element );
  113. canvas.element = $('<canvas/>');
  114. canvas.context = canvas.element.get(0).getContext('2d');
  115. canvas.container.append( canvas.element );
  116. canvas.element.css( 'display', 'block' );
  117. canvas.element.get(0).width = ( ParticlesSandbox.params.width ) ? ParticlesSandbox.params.width : canvas.container.width();
  118. canvas.element.get(0).height = ( ParticlesSandbox.params.height ) ? ParticlesSandbox.params.height : canvas.container.height();
  119. canvas.element.css( 'background', ParticlesSandbox.params.background );
  120. };
  121. /*
  122. * Resize canvas.
  123. */
  124. ParticlesSandbox.resize = function resize( width, height ){
  125. if ( width ) {
  126. canvas.element.get(0).width = width;
  127. }
  128. if ( height ) {
  129. canvas.element.get(0).height = height;
  130. }
  131. };
  132. /*
  133. * Create all particles in the sandbox.
  134. */
  135. ParticlesSandbox.initParticles = function initParticles(){
  136. var i, count;
  137. i = 0;
  138. count = ParticlesSandbox.params.particlesNumber;
  139. for ( ; i < count; i += 1 ) {
  140. ParticlesSandbox.particles.push( createParticle(
  141. ParticlesSandbox.canvas.element.get(0),
  142. ParticlesSandbox.params.particle
  143. ) );
  144. }
  145. };
  146. /*
  147. * Initialize the sandbox's events.
  148. */
  149. ParticlesSandbox.initEvents = function initEvents(){
  150. ParticlesSandbox.canvas.element.mouseenter(function mouseEnterCallback(){
  151. ParticlesSandbox.mouse.hoverCanvas = true;
  152. if ( !ParticlesSandbox.isAnimated ) {
  153. ParticlesSandbox.draw();
  154. }
  155. });
  156. ParticlesSandbox.canvas.element.mouseleave(function mouseLeaveCallback(){
  157. ParticlesSandbox.mouse.hoverCanvas = false;
  158. });
  159. ParticlesSandbox.canvas.element.mousemove(function mouseMoveCallback(e){
  160. ParticlesSandbox.mouse = $.extend( ParticlesSandbox.mouse, Utils.getMousePosition( e, ParticlesSandbox.canvas.element[0] ) );
  161. });
  162. };
  163. /*
  164. * Initialize the sandbox's animation.
  165. */
  166. ParticlesSandbox.initAnimation = function initAnimation(){
  167. window.requestAnimFrame =
  168. window.requestAnimationFrame ||
  169. window.webkitRequestAnimationFrame ||
  170. window.mozRequestAnimationFrame ||
  171. window.ORequestAnimationFrame ||
  172. window.msRequestAnimationFrame ||
  173. function requestAnimFrame( callback ){
  174. setTimeOut( callback, 1000/60 );
  175. };
  176. ParticlesSandbox.isAnimated = true;
  177. ParticlesSandbox.draw();
  178. };
  179. /*
  180. * Draw the sandbox canvas.
  181. */
  182. ParticlesSandbox.draw = function draw(){
  183. var i, j, count, canvas, particle, particle2;
  184. i = 0;
  185. count = ParticlesSandbox.particles.length;
  186. canvas = ParticlesSandbox.canvas;
  187. canvas.context.clearRect( 0, 0, canvas.element.get(0).width, canvas.element.get(0).height );
  188. for ( ; i < count; i += 1 ) {
  189. particle = ParticlesSandbox.particles[i];
  190. if ( ParticlesSandbox.isAnimated ) {
  191. particle.update();
  192. }
  193. particle.draw();
  194. if ( !ParticlesSandbox.params.disableMouse && ParticlesSandbox.mouse.hoverCanvas ) {
  195. ParticlesSandbox.drawLink(
  196. particle.getPosition('x'),
  197. particle.getPosition('y'),
  198. ParticlesSandbox.mouse.x,
  199. ParticlesSandbox.mouse.y
  200. );
  201. }
  202. if ( !ParticlesSandbox.params.disableLinks ) {
  203. for ( j = i+1; j < count; j += 1 ) {
  204. particle2 = ParticlesSandbox.particles[j];
  205. ParticlesSandbox.drawLink(
  206. particle.getPosition('x'),
  207. particle.getPosition('y'),
  208. particle2.getPosition('x'),
  209. particle2.getPosition('y')
  210. );
  211. }
  212. }
  213. }
  214. ParticlesSandbox.requestID = window.requestAnimFrame( ParticlesSandbox.draw );
  215. };
  216. /*
  217. * Draw a link between two particles.
  218. * @param {int} x First object abscissa coords.
  219. * @param {int} y First object ordered coords.
  220. * @param {int} x2 Second object abscissa coords.
  221. * @param {int} y2 Second object ordered coords.
  222. */
  223. ParticlesSandbox.drawLink = function drawLink( x, y, x2, y2 ){
  224. var context;
  225. if ( Utils.getDistance( x, y, x2, y2 ) <= ParticlesSandbox.params.createLinkDist ) {
  226. context = ParticlesSandbox.canvas.context;
  227. context.save();
  228. context.beginPath();
  229. context.lineWidth = ParticlesSandbox.params.linksWidth;
  230. context.moveTo( x, y );
  231. context.lineTo( x2, y2 );
  232. context.globalAlpha = ParticlesSandbox.getOpacityLink( x, y, x2, y2 );
  233. context.strokeStyle = ParticlesSandbox.params.color;
  234. context.lineCap = 'round';
  235. context.stroke();
  236. context.closePath();
  237. context.restore();
  238. }
  239. };
  240. /*
  241. * Get opacity for link two particles.
  242. * @param {int} x First object abscissa coords.
  243. * @param {int} y First object ordered coords.
  244. * @param {int} x2 Second object abscissa coords.
  245. * @param {int} y2 Second object ordered coords.
  246. * @return {int} 0 <= opacity <= 1
  247. */
  248. ParticlesSandbox.getOpacityLink = function getOpacityLink( x, y, x2, y2 ){
  249. var dist, opacity, linkDist, createLinkDist;
  250. dist = Utils.getDistance( x, y, x2, y2 );
  251. linkDist = ParticlesSandbox.params.linkDist;
  252. createLinkDist = ParticlesSandbox.params.createLinkDist;
  253. if ( dist <= linkDist ) {
  254. opacity = 1;
  255. } else if ( dist > createLinkDist ) {
  256. opacity = 0;
  257. } else {
  258. opacity = 1 - ( ( ( dist - linkDist ) * 100 ) / ( createLinkDist - linkDist ) ) / 100;
  259. }
  260. return opacity;
  261. };
  262. /*
  263. * Freeze the animation.
  264. */
  265. ParticlesSandbox.freeze = function freeze(){
  266. if ( ParticlesSandbox.isAnimated ) {
  267. ParticlesSandbox.isAnimated = false;
  268. }
  269. };
  270. /*
  271. * Unfreeze the animation.
  272. */
  273. ParticlesSandbox.unfreeze = function unfreeze(){
  274. if ( !ParticlesSandbox.isAnimated ) {
  275. ParticlesSandbox.isAnimated = true;
  276. }
  277. };
  278. /*
  279. * Remove the animation's canvas.
  280. */
  281. ParticlesSandbox.remove = function remove(){
  282. ParticlesSandbox.canvas.element.remove();
  283. };
  284. /*
  285. * Create a particle instance.
  286. * @param {Object} canvas DOM element.
  287. * @param {Object} params Few particle's params.
  288. * @return {Object} Particle object.
  289. */
  290. createParticle = function createParticle( canvas, params ){
  291. var Particle;
  292. Particle = {};
  293. Particle.canvas = {};
  294. Particle.vector = {};
  295. /*
  296. * Initialize the particle.
  297. * @param {Object} canvas DOM element.
  298. * @param {Object} params Few particle's params.
  299. */
  300. Particle.initialize = function initialize( canvas, params ){
  301. Particle.params = $.extend({
  302. color: 'white',
  303. minSize: 2,
  304. maxSize: 4,
  305. speed: 60
  306. }, params );
  307. Particle.setCanvasContext( canvas );
  308. Particle.initSize();
  309. Particle.initPosition();
  310. Particle.initVectors();
  311. };
  312. /*
  313. * Initialize particle's position.
  314. */
  315. Particle.initPosition = function initPosition(){
  316. Particle.x = Utils.getRandNumber( 0 + Particle.radius, Particle.canvas.element.width - Particle.radius );
  317. Particle.y = Utils.getRandNumber( 0 + Particle.radius, Particle.canvas.element.height - Particle.radius );
  318. };
  319. /*
  320. * Initialize particle's size.
  321. */
  322. Particle.initSize = function initSize(){
  323. Particle.size = Utils.getRandNumber( Particle.params.minSize, Particle.params.maxSize );
  324. Particle.radius = Particle.size / 2;
  325. };
  326. /*
  327. * Initialize particle's vectors for speed.
  328. */
  329. Particle.initVectors = function initVectors(){
  330. do {
  331. Particle.vector.x = Utils.getRandNumber( -Particle.params.speed / 60, Particle.params.speed / 60, false );
  332. Particle.vector.y = Utils.getRandNumber( -Particle.params.speed / 60, Particle.params.speed / 60, false );
  333. } while ( Particle.vector.x == 0 || Particle.vector.y == 0 )
  334. };
  335. /*
  336. * Set the context to draw particles.
  337. * @param {Object} canvas Canvas.
  338. */
  339. Particle.setCanvasContext = function setCanvasContext( canvas ){
  340. var context;
  341. Particle.canvas.element = canvas;
  342. context = canvas.getContext('2d');
  343. if ( typeof context === 'object' && typeof context.canvas === 'object' ) {
  344. Particle.canvas.context = context;
  345. } else {
  346. throw "Error: Can't set canvas context to Particle because context isn't a CanvasRenderingContext2D object.";
  347. }
  348. };
  349. /*
  350. * Draw particle.
  351. */
  352. Particle.draw = function draw(){
  353. var context = Particle.canvas.context;
  354. context.beginPath();
  355. context.arc( Particle.x, Particle.y, Particle.size /2, 0, Math.PI*2 );
  356. context.fillStyle = Particle.params.color;
  357. context.fill();
  358. context.closePath();
  359. };
  360. /*
  361. * Update the particle's position.
  362. */
  363. Particle.update = function update(){
  364. Particle.x += Particle.vector.x;
  365. Particle.y += Particle.vector.y;
  366. if ( 0 > ( Particle.x - Particle.radius ) || ( Particle.x + Particle.radius ) > Particle.canvas.element.width ) {
  367. Particle.vector.x = -Particle.vector.x;
  368. }
  369. if ( 0 > ( Particle.y - Particle.radius ) || ( Particle.y + Particle.radius ) > Particle.canvas.element.height ) {
  370. Particle.vector.y = -Particle.vector.y;
  371. }
  372. };
  373. /*
  374. * Return position of particle.
  375. * @param {string} axis Optionnal axis.
  376. * @return {int|Object} Return object if axis is not defined, else return int.
  377. */
  378. Particle.getPosition = function getPosition( axis ){
  379. if ( typeof axis === 'string' && ( axis != 'x' && axis != 'y' ) ) {
  380. axis = null;
  381. }
  382. return ( typeof( axis ) === 'string' ) ? Particle[ axis ] : { x: Particle.x, y: Particle.y };
  383. };
  384. Particle.initialize( canvas, params );
  385. return {
  386. getPosition: Particle.getPosition,
  387. update: Particle.update,
  388. draw: Particle.draw
  389. };
  390. };
  391. ParticlesSandbox.initialize( element, params );
  392. return {
  393. remove: ParticlesSandbox.remove,
  394. freeze: ParticlesSandbox.freeze,
  395. unfreeze: ParticlesSandbox.unfreeze,
  396. resize: ParticlesSandbox.resize
  397. };
  398. };
  399. /*
  400. * Get rand number between x and y.
  401. * @param {int} x Minimal number.
  402. * @param {int} y Maximal number.
  403. * @param {Boolean} round True is value shouldn't be round.
  404. * @return {int} Rand number.
  405. */
  406. Utils.getRandNumber = function getRandNumber( x, y, round ){
  407. var value;
  408. if( x == null ) {
  409. x = 0;
  410. }
  411. if( y == null ) {
  412. y = 10;
  413. }
  414. if( round == null ) {
  415. round = true;
  416. }
  417. value = Math.random() * ( y - x ) + x;
  418. return ( round ) ? Math.round( value ) : value;
  419. };
  420. /*
  421. * Get distance between to cartesian points.
  422. * @param {int} x First object abscissa coords.
  423. * @param {int} y First object ordered coords.
  424. * @param {int} x2 Second object abscissa coords.
  425. * @param {int} y2 Second object ordered coords.
  426. * @return {int} Distance.
  427. */
  428. Utils.getDistance = function getDistance( x, y, x2, y2 ){
  429. return Math.sqrt( Math.pow( x2 - x, 2 ) + Math.pow( y2 - y, 2 ) );
  430. };
  431. /*
  432. * Get mouse position.
  433. * @param {Object} event The HTML DOM events.
  434. * @param {Object} element The DOM element.
  435. * @return {Object} x/y position.
  436. */
  437. Utils.getMousePosition = function getMousePosition( event, element ){
  438. var rect;
  439. if ( typeof element === 'undefined' ) {
  440. element = $('body')[0];
  441. }
  442. rect = element.getBoundingClientRect();
  443. return {
  444. x: event.clientX - rect.left,
  445. y: event.clientY - rect.top
  446. };
  447. };
  448. })( jQuery )