particles.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /* -----------------------------------------------
  2. /* Author : Vincent Garreau - vincentgarreau.com
  3. /* MIT license: http://opensource.org/licenses/MIT
  4. /* Demo / Generator : vincentgarreau.com/particles.js
  5. /* GitHub : github.com/VincentGarreau/particles.js
  6. /* How to use? : Check the GitHub README
  7. /* v2.0.0
  8. /* ----------------------------------------------- */
  9. var pJS = function(tag_id, params){
  10. var canvas_el = document.querySelector('#'+tag_id+' > .particles-js-canvas-el');
  11. /* particles.js variables with default values */
  12. this.pJS = {
  13. canvas: {
  14. el: canvas_el,
  15. w: canvas_el.offsetWidth,
  16. h: canvas_el.offsetHeight
  17. },
  18. particles: {
  19. number: {
  20. value: 400,
  21. density: {
  22. enable: true,
  23. value_area: 800
  24. }
  25. },
  26. color: {
  27. value: '#fff'
  28. },
  29. shape: {
  30. type: 'circle',
  31. stroke: {
  32. width: 0,
  33. color: '#ff0000'
  34. },
  35. polygon: {
  36. nb_sides: 5
  37. },
  38. image: {
  39. src: '',
  40. width: 100,
  41. height: 100
  42. }
  43. },
  44. opacity: {
  45. value: 1,
  46. random: false,
  47. anim: {
  48. enable: false,
  49. speed: 2,
  50. opacity_min: 0,
  51. sync: false
  52. }
  53. },
  54. size: {
  55. value: 20,
  56. random: false,
  57. anim: {
  58. enable: false,
  59. speed: 20,
  60. size_min: 0,
  61. sync: false
  62. }
  63. },
  64. line_linked: {
  65. enable: true,
  66. distance: 100,
  67. color: '#fff',
  68. opacity: 1,
  69. width: 1
  70. },
  71. move: {
  72. enable: true,
  73. speed: 2,
  74. direction: 'none',
  75. random: false,
  76. straight: false,
  77. out_mode: 'out',
  78. bounce: false,
  79. attract: {
  80. enable: false,
  81. rotateX: 3000,
  82. rotateY: 3000
  83. }
  84. },
  85. array: []
  86. },
  87. interactivity: {
  88. detect_on: 'canvas',
  89. events: {
  90. onhover: {
  91. enable: true,
  92. mode: 'grab'
  93. },
  94. onclick: {
  95. enable: true,
  96. mode: 'push'
  97. },
  98. resize: true
  99. },
  100. modes: {
  101. grab:{
  102. distance: 100,
  103. line_linked:{
  104. opacity: 1
  105. }
  106. },
  107. bubble:{
  108. distance: 200,
  109. size: 80,
  110. duration: 0.4
  111. },
  112. repulse:{
  113. distance: 200,
  114. duration: 0.4
  115. },
  116. push:{
  117. particles_nb: 4
  118. },
  119. remove:{
  120. particles_nb: 2
  121. }
  122. },
  123. mouse:{}
  124. },
  125. retina_detect: false,
  126. fn: {
  127. interact: {},
  128. modes: {},
  129. vendors:{}
  130. },
  131. tmp: {}
  132. };
  133. var pJS = this.pJS;
  134. /* params settings */
  135. if(params){
  136. Object.deepExtend(pJS, params);
  137. }
  138. pJS.tmp.obj = {
  139. size_value: pJS.particles.size.value,
  140. size_anim_speed: pJS.particles.size.anim.speed,
  141. move_speed: pJS.particles.move.speed,
  142. line_linked_distance: pJS.particles.line_linked.distance,
  143. line_linked_width: pJS.particles.line_linked.width,
  144. mode_grab_distance: pJS.interactivity.modes.grab.distance,
  145. mode_bubble_distance: pJS.interactivity.modes.bubble.distance,
  146. mode_bubble_size: pJS.interactivity.modes.bubble.size,
  147. mode_repulse_distance: pJS.interactivity.modes.repulse.distance
  148. };
  149. pJS.fn.retinaInit = function(){
  150. if(pJS.retina_detect && window.devicePixelRatio > 1){
  151. pJS.canvas.pxratio = window.devicePixelRatio;
  152. pJS.tmp.retina = true;
  153. }
  154. else{
  155. pJS.canvas.pxratio = 1;
  156. pJS.tmp.retina = false;
  157. }
  158. pJS.canvas.w = pJS.canvas.el.offsetWidth * pJS.canvas.pxratio;
  159. pJS.canvas.h = pJS.canvas.el.offsetHeight * pJS.canvas.pxratio;
  160. pJS.particles.size.value = pJS.tmp.obj.size_value * pJS.canvas.pxratio;
  161. pJS.particles.size.anim.speed = pJS.tmp.obj.size_anim_speed * pJS.canvas.pxratio;
  162. pJS.particles.move.speed = pJS.tmp.obj.move_speed * pJS.canvas.pxratio;
  163. pJS.particles.line_linked.distance = pJS.tmp.obj.line_linked_distance * pJS.canvas.pxratio;
  164. pJS.interactivity.modes.grab.distance = pJS.tmp.obj.mode_grab_distance * pJS.canvas.pxratio;
  165. pJS.interactivity.modes.bubble.distance = pJS.tmp.obj.mode_bubble_distance * pJS.canvas.pxratio;
  166. pJS.particles.line_linked.width = pJS.tmp.obj.line_linked_width * pJS.canvas.pxratio;
  167. pJS.interactivity.modes.bubble.size = pJS.tmp.obj.mode_bubble_size * pJS.canvas.pxratio;
  168. pJS.interactivity.modes.repulse.distance = pJS.tmp.obj.mode_repulse_distance * pJS.canvas.pxratio;
  169. };
  170. /* ---------- pJS functions - canvas ------------ */
  171. pJS.fn.canvasInit = function(){
  172. pJS.canvas.ctx = pJS.canvas.el.getContext('2d');
  173. };
  174. pJS.fn.canvasSize = function(){
  175. pJS.canvas.el.width = pJS.canvas.w;
  176. pJS.canvas.el.height = pJS.canvas.h;
  177. if(pJS && pJS.interactivity.events.resize){
  178. window.addEventListener('resize', function(){
  179. pJS.canvas.w = pJS.canvas.el.offsetWidth;
  180. pJS.canvas.h = pJS.canvas.el.offsetHeight;
  181. /* resize canvas */
  182. if(pJS.tmp.retina){
  183. pJS.canvas.w *= pJS.canvas.pxratio;
  184. pJS.canvas.h *= pJS.canvas.pxratio;
  185. }
  186. pJS.canvas.el.width = pJS.canvas.w;
  187. pJS.canvas.el.height = pJS.canvas.h;
  188. /* repaint canvas on anim disabled */
  189. if(!pJS.particles.move.enable){
  190. pJS.fn.particlesEmpty();
  191. pJS.fn.particlesCreate();
  192. pJS.fn.particlesDraw();
  193. pJS.fn.vendors.densityAutoParticles();
  194. }
  195. /* density particles enabled */
  196. pJS.fn.vendors.densityAutoParticles();
  197. });
  198. }
  199. };
  200. pJS.fn.canvasPaint = function(){
  201. pJS.canvas.ctx.fillRect(0, 0, pJS.canvas.w, pJS.canvas.h);
  202. };
  203. pJS.fn.canvasClear = function(){
  204. pJS.canvas.ctx.clearRect(0, 0, pJS.canvas.w, pJS.canvas.h);
  205. };
  206. /* --------- pJS functions - particles ----------- */
  207. pJS.fn.particle = function(color, opacity, position){
  208. /* size */
  209. this.radius = (pJS.particles.size.random ? Math.random() : 1) * pJS.particles.size.value;
  210. if(pJS.particles.size.anim.enable){
  211. this.size_status = false;
  212. this.vs = pJS.particles.size.anim.speed / 100;
  213. if(!pJS.particles.size.anim.sync){
  214. this.vs = this.vs * Math.random();
  215. }
  216. }
  217. /* position */
  218. this.x = position ? position.x : Math.random() * pJS.canvas.w;
  219. this.y = position ? position.y : Math.random() * pJS.canvas.h;
  220. /* check position - into the canvas */
  221. if(this.x > pJS.canvas.w - this.radius*2) this.x = this.x - this.radius;
  222. else if(this.x < this.radius*2) this.x = this.x + this.radius;
  223. if(this.y > pJS.canvas.h - this.radius*2) this.y = this.y - this.radius;
  224. else if(this.y < this.radius*2) this.y = this.y + this.radius;
  225. /* check position - avoid overlap */
  226. if(pJS.particles.move.bounce){
  227. pJS.fn.vendors.checkOverlap(this, position);
  228. }
  229. /* color */
  230. this.color = {};
  231. if(typeof(color.value) == 'object'){
  232. if(color.value instanceof Array){
  233. var color_selected = color.value[Math.floor(Math.random() * pJS.particles.color.value.length)];
  234. this.color.rgb = hexToRgb(color_selected);
  235. }else{
  236. if(color.value.r != undefined && color.value.g != undefined && color.value.b != undefined){
  237. this.color.rgb = {
  238. r: color.value.r,
  239. g: color.value.g,
  240. b: color.value.b
  241. }
  242. }
  243. if(color.value.h != undefined && color.value.s != undefined && color.value.l != undefined){
  244. this.color.hsl = {
  245. h: color.value.h,
  246. s: color.value.s,
  247. l: color.value.l
  248. }
  249. }
  250. }
  251. }
  252. else if(color.value == 'random'){
  253. this.color.rgb = {
  254. r: (Math.floor(Math.random() * (255 - 0 + 1)) + 0),
  255. g: (Math.floor(Math.random() * (255 - 0 + 1)) + 0),
  256. b: (Math.floor(Math.random() * (255 - 0 + 1)) + 0)
  257. }
  258. }
  259. else if(typeof(color.value) == 'string'){
  260. this.color = color;
  261. this.color.rgb = hexToRgb(this.color.value);
  262. }
  263. /* opacity */
  264. this.opacity = (pJS.particles.opacity.random ? Math.random() : 1) * pJS.particles.opacity.value;
  265. if(pJS.particles.opacity.anim.enable){
  266. this.opacity_status = false;
  267. this.vo = pJS.particles.opacity.anim.speed / 100;
  268. if(!pJS.particles.opacity.anim.sync){
  269. this.vo = this.vo * Math.random();
  270. }
  271. }
  272. /* animation - velocity for speed */
  273. var velbase = {}
  274. switch(pJS.particles.move.direction){
  275. case 'top':
  276. velbase = { x:0, y:-1 };
  277. break;
  278. case 'top-right':
  279. velbase = { x:0.5, y:-0.5 };
  280. break;
  281. case 'right':
  282. velbase = { x:1, y:-0 };
  283. break;
  284. case 'bottom-right':
  285. velbase = { x:0.5, y:0.5 };
  286. break;
  287. case 'bottom':
  288. velbase = { x:0, y:1 };
  289. break;
  290. case 'bottom-left':
  291. velbase = { x:-0.5, y:1 };
  292. break;
  293. case 'left':
  294. velbase = { x:-1, y:0 };
  295. break;
  296. case 'top-left':
  297. velbase = { x:-0.5, y:-0.5 };
  298. break;
  299. default:
  300. velbase = { x:0, y:0 };
  301. break;
  302. }
  303. if(pJS.particles.move.straight){
  304. this.vx = velbase.x;
  305. this.vy = velbase.y;
  306. if(pJS.particles.move.random){
  307. this.vx = this.vx * (Math.random());
  308. this.vy = this.vy * (Math.random());
  309. }
  310. }else{
  311. this.vx = velbase.x + Math.random()-0.5;
  312. this.vy = velbase.y + Math.random()-0.5;
  313. }
  314. // var theta = 2.0 * Math.PI * Math.random();
  315. // this.vx = Math.cos(theta);
  316. // this.vy = Math.sin(theta);
  317. this.vx_i = this.vx;
  318. this.vy_i = this.vy;
  319. /* if shape is image */
  320. var shape_type = pJS.particles.shape.type;
  321. if(typeof(shape_type) == 'object'){
  322. if(shape_type instanceof Array){
  323. var shape_selected = shape_type[Math.floor(Math.random() * shape_type.length)];
  324. this.shape = shape_selected;
  325. }
  326. }else{
  327. this.shape = shape_type;
  328. }
  329. if(this.shape == 'image'){
  330. var sh = pJS.particles.shape;
  331. this.img = {
  332. src: sh.image.src,
  333. ratio: sh.image.width / sh.image.height
  334. }
  335. if(!this.img.ratio) this.img.ratio = 1;
  336. if(pJS.tmp.img_type == 'svg' && pJS.tmp.source_svg != undefined){
  337. pJS.fn.vendors.createSvgImg(this);
  338. if(pJS.tmp.pushing){
  339. this.img.loaded = false;
  340. }
  341. }
  342. }
  343. };
  344. pJS.fn.particle.prototype.draw = function() {
  345. var p = this;
  346. if(p.radius_bubble != undefined){
  347. var radius = p.radius_bubble;
  348. }else{
  349. var radius = p.radius;
  350. }
  351. if(p.opacity_bubble != undefined){
  352. var opacity = p.opacity_bubble;
  353. }else{
  354. var opacity = p.opacity;
  355. }
  356. if(p.color.rgb){
  357. var color_value = 'rgba('+p.color.rgb.r+','+p.color.rgb.g+','+p.color.rgb.b+','+opacity+')';
  358. }else{
  359. var color_value = 'hsla('+p.color.hsl.h+','+p.color.hsl.s+'%,'+p.color.hsl.l+'%,'+opacity+')';
  360. }
  361. pJS.canvas.ctx.fillStyle = color_value;
  362. pJS.canvas.ctx.beginPath();
  363. switch(p.shape){
  364. case 'circle':
  365. pJS.canvas.ctx.arc(p.x, p.y, radius, 0, Math.PI * 2, false);
  366. break;
  367. case 'edge':
  368. pJS.canvas.ctx.rect(p.x-radius, p.y-radius, radius*2, radius*2);
  369. break;
  370. case 'triangle':
  371. pJS.fn.vendors.drawShape(pJS.canvas.ctx, p.x-radius, p.y+radius / 1.66, radius*2, 3, 2);
  372. break;
  373. case 'polygon':
  374. pJS.fn.vendors.drawShape(
  375. pJS.canvas.ctx,
  376. p.x - radius / (pJS.particles.shape.polygon.nb_sides/3.5), // startX
  377. p.y - radius / (2.66/3.5), // startY
  378. radius*2.66 / (pJS.particles.shape.polygon.nb_sides/3), // sideLength
  379. pJS.particles.shape.polygon.nb_sides, // sideCountNumerator
  380. 1 // sideCountDenominator
  381. );
  382. break;
  383. case 'star':
  384. pJS.fn.vendors.drawShape(
  385. pJS.canvas.ctx,
  386. p.x - radius*2 / (pJS.particles.shape.polygon.nb_sides/4), // startX
  387. p.y - radius / (2*2.66/3.5), // startY
  388. radius*2*2.66 / (pJS.particles.shape.polygon.nb_sides/3), // sideLength
  389. pJS.particles.shape.polygon.nb_sides, // sideCountNumerator
  390. 2 // sideCountDenominator
  391. );
  392. break;
  393. case 'image':
  394. function draw(){
  395. pJS.canvas.ctx.drawImage(
  396. img_obj,
  397. p.x-radius,
  398. p.y-radius,
  399. radius*2,
  400. radius*2 / p.img.ratio
  401. );
  402. }
  403. if(pJS.tmp.img_type == 'svg'){
  404. var img_obj = p.img.obj;
  405. }else{
  406. var img_obj = pJS.tmp.img_obj;
  407. }
  408. if(img_obj){
  409. draw();
  410. }
  411. break;
  412. }
  413. pJS.canvas.ctx.closePath();
  414. if(pJS.particles.shape.stroke.width > 0){
  415. pJS.canvas.ctx.strokeStyle = pJS.particles.shape.stroke.color;
  416. pJS.canvas.ctx.lineWidth = pJS.particles.shape.stroke.width;
  417. pJS.canvas.ctx.stroke();
  418. }
  419. pJS.canvas.ctx.fill();
  420. };
  421. pJS.fn.particlesCreate = function(){
  422. for(var i = 0; i < pJS.particles.number.value; i++) {
  423. pJS.particles.array.push(new pJS.fn.particle(pJS.particles.color, pJS.particles.opacity.value));
  424. }
  425. };
  426. pJS.fn.particlesUpdate = function(){
  427. for(var i = 0; i < pJS.particles.array.length; i++){
  428. /* the particle */
  429. var p = pJS.particles.array[i];
  430. // var d = ( dx = pJS.interactivity.mouse.click_pos_x - p.x ) * dx + ( dy = pJS.interactivity.mouse.click_pos_y - p.y ) * dy;
  431. // var f = -BANG_SIZE / d;
  432. // if ( d < BANG_SIZE ) {
  433. // var t = Math.atan2( dy, dx );
  434. // p.vx = f * Math.cos(t);
  435. // p.vy = f * Math.sin(t);
  436. // }
  437. /* move the particle */
  438. if(pJS.particles.move.enable){
  439. var ms = pJS.particles.move.speed/2;
  440. p.x += p.vx * ms;
  441. p.y += p.vy * ms;
  442. }
  443. /* change opacity status */
  444. if(pJS.particles.opacity.anim.enable) {
  445. if(p.opacity_status == true) {
  446. if(p.opacity >= pJS.particles.opacity.value) p.opacity_status = false;
  447. p.opacity += p.vo;
  448. }else {
  449. if(p.opacity <= pJS.particles.opacity.anim.opacity_min) p.opacity_status = true;
  450. p.opacity -= p.vo;
  451. }
  452. if(p.opacity < 0) p.opacity = 0;
  453. }
  454. /* change size */
  455. if(pJS.particles.size.anim.enable){
  456. if(p.size_status == true){
  457. if(p.radius >= pJS.particles.size.value) p.size_status = false;
  458. p.radius += p.vs;
  459. }else{
  460. if(p.radius <= pJS.particles.size.anim.size_min) p.size_status = true;
  461. p.radius -= p.vs;
  462. }
  463. if(p.radius < 0) p.radius = 0;
  464. }
  465. /* change particle position if it is out of canvas */
  466. if(pJS.particles.move.out_mode == 'bounce'){
  467. var new_pos = {
  468. x_left: p.radius,
  469. x_right: pJS.canvas.w,
  470. y_top: p.radius,
  471. y_bottom: pJS.canvas.h
  472. }
  473. }else{
  474. var new_pos = {
  475. x_left: -p.radius,
  476. x_right: pJS.canvas.w + p.radius,
  477. y_top: -p.radius,
  478. y_bottom: pJS.canvas.h + p.radius
  479. }
  480. }
  481. if(p.x - p.radius > pJS.canvas.w){
  482. p.x = new_pos.x_left;
  483. p.y = Math.random() * pJS.canvas.h;
  484. }
  485. else if(p.x + p.radius < 0){
  486. p.x = new_pos.x_right;
  487. p.y = Math.random() * pJS.canvas.h;
  488. }
  489. if(p.y - p.radius > pJS.canvas.h){
  490. p.y = new_pos.y_top;
  491. p.x = Math.random() * pJS.canvas.w;
  492. }
  493. else if(p.y + p.radius < 0){
  494. p.y = new_pos.y_bottom;
  495. p.x = Math.random() * pJS.canvas.w;
  496. }
  497. /* out of canvas modes */
  498. switch(pJS.particles.move.out_mode){
  499. case 'bounce':
  500. if (p.x + p.radius > pJS.canvas.w) p.vx = -p.vx;
  501. else if (p.x - p.radius < 0) p.vx = -p.vx;
  502. if (p.y + p.radius > pJS.canvas.h) p.vy = -p.vy;
  503. else if (p.y - p.radius < 0) p.vy = -p.vy;
  504. break;
  505. }
  506. /* events */
  507. if(isInArray('grab', pJS.interactivity.events.onhover.mode)){
  508. pJS.fn.modes.grabParticle(p);
  509. }
  510. if(isInArray('bubble', pJS.interactivity.events.onhover.mode) || isInArray('bubble', pJS.interactivity.events.onclick.mode)){
  511. pJS.fn.modes.bubbleParticle(p);
  512. }
  513. if(isInArray('repulse', pJS.interactivity.events.onhover.mode) || isInArray('repulse', pJS.interactivity.events.onclick.mode)){
  514. pJS.fn.modes.repulseParticle(p);
  515. }
  516. /* interaction auto between particles */
  517. if(pJS.particles.line_linked.enable || pJS.particles.move.attract.enable){
  518. for(var j = i + 1; j < pJS.particles.array.length; j++){
  519. var p2 = pJS.particles.array[j];
  520. /* link particles */
  521. if(pJS.particles.line_linked.enable){
  522. pJS.fn.interact.linkParticles(p,p2);
  523. }
  524. /* attract particles */
  525. if(pJS.particles.move.attract.enable){
  526. pJS.fn.interact.attractParticles(p,p2);
  527. }
  528. /* bounce particles */
  529. if(pJS.particles.move.bounce){
  530. pJS.fn.interact.bounceParticles(p,p2);
  531. }
  532. }
  533. }
  534. }
  535. };
  536. pJS.fn.particlesDraw = function(){
  537. /* clear canvas */
  538. pJS.canvas.ctx.clearRect(0, 0, pJS.canvas.w, pJS.canvas.h);
  539. /* update each particles param */
  540. pJS.fn.particlesUpdate();
  541. /* draw each particle */
  542. for(var i = 0; i < pJS.particles.array.length; i++){
  543. var p = pJS.particles.array[i];
  544. p.draw();
  545. }
  546. };
  547. pJS.fn.particlesEmpty = function(){
  548. pJS.particles.array = [];
  549. };
  550. pJS.fn.particlesRefresh = function(){
  551. /* init all */
  552. cancelRequestAnimFrame(pJS.fn.checkAnimFrame);
  553. cancelRequestAnimFrame(pJS.fn.drawAnimFrame);
  554. pJS.tmp.source_svg = undefined;
  555. pJS.tmp.img_obj = undefined;
  556. pJS.tmp.count_svg = 0;
  557. pJS.fn.particlesEmpty();
  558. pJS.fn.canvasClear();
  559. /* restart */
  560. pJS.fn.vendors.start();
  561. };
  562. /* ---------- pJS functions - particles interaction ------------ */
  563. pJS.fn.interact.linkParticles = function(p1, p2){
  564. var dx = p1.x - p2.x,
  565. dy = p1.y - p2.y,
  566. dist = Math.sqrt(dx*dx + dy*dy);
  567. /* draw a line between p1 and p2 if the distance between them is under the config distance */
  568. if(dist <= pJS.particles.line_linked.distance){
  569. var opacity_line = pJS.particles.line_linked.opacity - (dist / (1/pJS.particles.line_linked.opacity)) / pJS.particles.line_linked.distance;
  570. if(opacity_line > 0){
  571. /* style */
  572. var color_line = pJS.particles.line_linked.color_rgb_line;
  573. pJS.canvas.ctx.strokeStyle = 'rgba('+color_line.r+','+color_line.g+','+color_line.b+','+opacity_line+')';
  574. pJS.canvas.ctx.lineWidth = pJS.particles.line_linked.width;
  575. //pJS.canvas.ctx.lineCap = 'round'; /* performance issue */
  576. /* path */
  577. pJS.canvas.ctx.beginPath();
  578. pJS.canvas.ctx.moveTo(p1.x, p1.y);
  579. pJS.canvas.ctx.lineTo(p2.x, p2.y);
  580. pJS.canvas.ctx.stroke();
  581. pJS.canvas.ctx.closePath();
  582. }
  583. }
  584. };
  585. pJS.fn.interact.attractParticles = function(p1, p2){
  586. /* condensed particles */
  587. var dx = p1.x - p2.x,
  588. dy = p1.y - p2.y,
  589. dist = Math.sqrt(dx*dx + dy*dy);
  590. if(dist <= pJS.particles.line_linked.distance){
  591. var ax = dx/(pJS.particles.move.attract.rotateX*1000),
  592. ay = dy/(pJS.particles.move.attract.rotateY*1000);
  593. p1.vx -= ax;
  594. p1.vy -= ay;
  595. p2.vx += ax;
  596. p2.vy += ay;
  597. }
  598. }
  599. pJS.fn.interact.bounceParticles = function(p1, p2){
  600. var dx = p1.x - p2.x,
  601. dy = p1.y - p2.y,
  602. dist = Math.sqrt(dx*dx + dy*dy),
  603. dist_p = p1.radius+p2.radius;
  604. if(dist <= dist_p){
  605. p1.vx = -p1.vx;
  606. p1.vy = -p1.vy;
  607. p2.vx = -p2.vx;
  608. p2.vy = -p2.vy;
  609. }
  610. }
  611. /* ---------- pJS functions - modes events ------------ */
  612. pJS.fn.modes.pushParticles = function(nb, pos){
  613. pJS.tmp.pushing = true;
  614. for(var i = 0; i < nb; i++){
  615. pJS.particles.array.push(
  616. new pJS.fn.particle(
  617. pJS.particles.color,
  618. pJS.particles.opacity.value,
  619. {
  620. 'x': pos ? pos.pos_x : Math.random() * pJS.canvas.w,
  621. 'y': pos ? pos.pos_y : Math.random() * pJS.canvas.h
  622. }
  623. )
  624. )
  625. if(i == nb-1){
  626. if(!pJS.particles.move.enable){
  627. pJS.fn.particlesDraw();
  628. }
  629. pJS.tmp.pushing = false;
  630. }
  631. }
  632. };
  633. pJS.fn.modes.removeParticles = function(nb){
  634. pJS.particles.array.splice(0, nb);
  635. if(!pJS.particles.move.enable){
  636. pJS.fn.particlesDraw();
  637. }
  638. };
  639. pJS.fn.modes.bubbleParticle = function(p){
  640. /* on hover event */
  641. if(pJS.interactivity.events.onhover.enable && isInArray('bubble', pJS.interactivity.events.onhover.mode)){
  642. var dx_mouse = p.x - pJS.interactivity.mouse.pos_x,
  643. dy_mouse = p.y - pJS.interactivity.mouse.pos_y,
  644. dist_mouse = Math.sqrt(dx_mouse*dx_mouse + dy_mouse*dy_mouse),
  645. ratio = 1 - dist_mouse / pJS.interactivity.modes.bubble.distance;
  646. function init(){
  647. p.opacity_bubble = p.opacity;
  648. p.radius_bubble = p.radius;
  649. }
  650. /* mousemove - check ratio */
  651. if(dist_mouse <= pJS.interactivity.modes.bubble.distance){
  652. if(ratio >= 0 && pJS.interactivity.status == 'mousemove'){
  653. /* size */
  654. if(pJS.interactivity.modes.bubble.size != pJS.particles.size.value){
  655. if(pJS.interactivity.modes.bubble.size > pJS.particles.size.value){
  656. var size = p.radius + (pJS.interactivity.modes.bubble.size*ratio);
  657. if(size >= 0){
  658. p.radius_bubble = size;
  659. }
  660. }else{
  661. var dif = p.radius - pJS.interactivity.modes.bubble.size,
  662. size = p.radius - (dif*ratio);
  663. if(size > 0){
  664. p.radius_bubble = size;
  665. }else{
  666. p.radius_bubble = 0;
  667. }
  668. }
  669. }
  670. /* opacity */
  671. if(pJS.interactivity.modes.bubble.opacity != pJS.particles.opacity.value){
  672. if(pJS.interactivity.modes.bubble.opacity > pJS.particles.opacity.value){
  673. var opacity = pJS.interactivity.modes.bubble.opacity*ratio;
  674. if(opacity > p.opacity && opacity <= pJS.interactivity.modes.bubble.opacity){
  675. p.opacity_bubble = opacity;
  676. }
  677. }else{
  678. var opacity = p.opacity - (pJS.particles.opacity.value-pJS.interactivity.modes.bubble.opacity)*ratio;
  679. if(opacity < p.opacity && opacity >= pJS.interactivity.modes.bubble.opacity){
  680. p.opacity_bubble = opacity;
  681. }
  682. }
  683. }
  684. }
  685. }else{
  686. init();
  687. }
  688. /* mouseleave */
  689. if(pJS.interactivity.status == 'mouseleave'){
  690. init();
  691. }
  692. }
  693. /* on click event */
  694. else if(pJS.interactivity.events.onclick.enable && isInArray('bubble', pJS.interactivity.events.onclick.mode)){
  695. if(pJS.tmp.bubble_clicking){
  696. var dx_mouse = p.x - pJS.interactivity.mouse.click_pos_x,
  697. dy_mouse = p.y - pJS.interactivity.mouse.click_pos_y,
  698. dist_mouse = Math.sqrt(dx_mouse*dx_mouse + dy_mouse*dy_mouse),
  699. time_spent = (new Date().getTime() - pJS.interactivity.mouse.click_time)/1000;
  700. if(time_spent > pJS.interactivity.modes.bubble.duration){
  701. pJS.tmp.bubble_duration_end = true;
  702. }
  703. if(time_spent > pJS.interactivity.modes.bubble.duration*2){
  704. pJS.tmp.bubble_clicking = false;
  705. pJS.tmp.bubble_duration_end = false;
  706. }
  707. }
  708. function process(bubble_param, particles_param, p_obj_bubble, p_obj, id){
  709. if(bubble_param != particles_param){
  710. if(!pJS.tmp.bubble_duration_end){
  711. if(dist_mouse <= pJS.interactivity.modes.bubble.distance){
  712. if(p_obj_bubble != undefined) var obj = p_obj_bubble;
  713. else var obj = p_obj;
  714. if(obj != bubble_param){
  715. var value = p_obj - (time_spent * (p_obj - bubble_param) / pJS.interactivity.modes.bubble.duration);
  716. if(id == 'size') p.radius_bubble = value;
  717. if(id == 'opacity') p.opacity_bubble = value;
  718. }
  719. }else{
  720. if(id == 'size') p.radius_bubble = undefined;
  721. if(id == 'opacity') p.opacity_bubble = undefined;
  722. }
  723. }else{
  724. if(p_obj_bubble != undefined){
  725. var value_tmp = p_obj - (time_spent * (p_obj - bubble_param) / pJS.interactivity.modes.bubble.duration),
  726. dif = bubble_param - value_tmp;
  727. value = bubble_param + dif;
  728. if(id == 'size') p.radius_bubble = value;
  729. if(id == 'opacity') p.opacity_bubble = value;
  730. }
  731. }
  732. }
  733. }
  734. if(pJS.tmp.bubble_clicking){
  735. /* size */
  736. process(pJS.interactivity.modes.bubble.size, pJS.particles.size.value, p.radius_bubble, p.radius, 'size');
  737. /* opacity */
  738. process(pJS.interactivity.modes.bubble.opacity, pJS.particles.opacity.value, p.opacity_bubble, p.opacity, 'opacity');
  739. }
  740. }
  741. };
  742. pJS.fn.modes.repulseParticle = function(p){
  743. if(pJS.interactivity.events.onhover.enable && isInArray('repulse', pJS.interactivity.events.onhover.mode) && pJS.interactivity.status == 'mousemove') {
  744. var dx_mouse = p.x - pJS.interactivity.mouse.pos_x,
  745. dy_mouse = p.y - pJS.interactivity.mouse.pos_y,
  746. dist_mouse = Math.sqrt(dx_mouse*dx_mouse + dy_mouse*dy_mouse);
  747. var normVec = {x: dx_mouse/dist_mouse, y: dy_mouse/dist_mouse},
  748. repulseRadius = pJS.interactivity.modes.repulse.distance,
  749. velocity = 100,
  750. repulseFactor = clamp((1/repulseRadius)*(-1*Math.pow(dist_mouse/repulseRadius,2)+1)*repulseRadius*velocity, 0, 50);
  751. var pos = {
  752. x: p.x + normVec.x * repulseFactor,
  753. y: p.y + normVec.y * repulseFactor
  754. }
  755. if(pJS.particles.move.out_mode == 'bounce'){
  756. if(pos.x - p.radius > 0 && pos.x + p.radius < pJS.canvas.w) p.x = pos.x;
  757. if(pos.y - p.radius > 0 && pos.y + p.radius < pJS.canvas.h) p.y = pos.y;
  758. }else{
  759. p.x = pos.x;
  760. p.y = pos.y;
  761. }
  762. }
  763. else if(pJS.interactivity.events.onclick.enable && isInArray('repulse', pJS.interactivity.events.onclick.mode)) {
  764. if(!pJS.tmp.repulse_finish){
  765. pJS.tmp.repulse_count++;
  766. if(pJS.tmp.repulse_count == pJS.particles.array.length){
  767. pJS.tmp.repulse_finish = true;
  768. }
  769. }
  770. if(pJS.tmp.repulse_clicking){
  771. var repulseRadius = Math.pow(pJS.interactivity.modes.repulse.distance/6, 3);
  772. var dx = pJS.interactivity.mouse.click_pos_x - p.x,
  773. dy = pJS.interactivity.mouse.click_pos_y - p.y,
  774. d = dx*dx + dy*dy;
  775. var force = -repulseRadius / d * 1;
  776. function process(){
  777. var f = Math.atan2(dy,dx);
  778. p.vx = force * Math.cos(f);
  779. p.vy = force * Math.sin(f);
  780. if(pJS.particles.move.out_mode == 'bounce'){
  781. var pos = {
  782. x: p.x + p.vx,
  783. y: p.y + p.vy
  784. }
  785. if (pos.x + p.radius > pJS.canvas.w) p.vx = -p.vx;
  786. else if (pos.x - p.radius < 0) p.vx = -p.vx;
  787. if (pos.y + p.radius > pJS.canvas.h) p.vy = -p.vy;
  788. else if (pos.y - p.radius < 0) p.vy = -p.vy;
  789. }
  790. }
  791. // default
  792. if(d <= repulseRadius){
  793. process();
  794. }
  795. // bang - slow motion mode
  796. // if(!pJS.tmp.repulse_finish){
  797. // if(d <= repulseRadius){
  798. // process();
  799. // }
  800. // }else{
  801. // process();
  802. // }
  803. }else{
  804. if(pJS.tmp.repulse_clicking == false){
  805. p.vx = p.vx_i;
  806. p.vy = p.vy_i;
  807. }
  808. }
  809. }
  810. }
  811. pJS.fn.modes.grabParticle = function(p){
  812. if(pJS.interactivity.events.onhover.enable && pJS.interactivity.status == 'mousemove'){
  813. var dx_mouse = p.x - pJS.interactivity.mouse.pos_x,
  814. dy_mouse = p.y - pJS.interactivity.mouse.pos_y,
  815. dist_mouse = Math.sqrt(dx_mouse*dx_mouse + dy_mouse*dy_mouse);
  816. /* draw a line between the cursor and the particle if the distance between them is under the config distance */
  817. if(dist_mouse <= pJS.interactivity.modes.grab.distance){
  818. var opacity_line = pJS.interactivity.modes.grab.line_linked.opacity - (dist_mouse / (1/pJS.interactivity.modes.grab.line_linked.opacity)) / pJS.interactivity.modes.grab.distance;
  819. if(opacity_line > 0){
  820. /* style */
  821. var color_line = pJS.particles.line_linked.color_rgb_line;
  822. pJS.canvas.ctx.strokeStyle = 'rgba('+color_line.r+','+color_line.g+','+color_line.b+','+opacity_line+')';
  823. pJS.canvas.ctx.lineWidth = pJS.particles.line_linked.width;
  824. //pJS.canvas.ctx.lineCap = 'round'; /* performance issue */
  825. /* path */
  826. pJS.canvas.ctx.beginPath();
  827. pJS.canvas.ctx.moveTo(p.x, p.y);
  828. pJS.canvas.ctx.lineTo(pJS.interactivity.mouse.pos_x, pJS.interactivity.mouse.pos_y);
  829. pJS.canvas.ctx.stroke();
  830. pJS.canvas.ctx.closePath();
  831. }
  832. }
  833. }
  834. };
  835. /* ---------- pJS functions - vendors ------------ */
  836. pJS.fn.vendors.eventsListeners = function(){
  837. /* events target element */
  838. if(pJS.interactivity.detect_on == 'window'){
  839. pJS.interactivity.el = window;
  840. }else{
  841. pJS.interactivity.el = pJS.canvas.el;
  842. }
  843. /* detect mouse pos - on hover / click event */
  844. if(pJS.interactivity.events.onhover.enable || pJS.interactivity.events.onclick.enable){
  845. /* el on mousemove */
  846. pJS.interactivity.el.addEventListener('mousemove', function(e){
  847. if(pJS.interactivity.el == window){
  848. var pos_x = e.clientX,
  849. pos_y = e.clientY;
  850. }
  851. else{
  852. var pos_x = e.offsetX || e.clientX,
  853. pos_y = e.offsetY || e.clientY;
  854. }
  855. pJS.interactivity.mouse.pos_x = pos_x;
  856. pJS.interactivity.mouse.pos_y = pos_y;
  857. if(pJS.tmp.retina){
  858. pJS.interactivity.mouse.pos_x *= pJS.canvas.pxratio;
  859. pJS.interactivity.mouse.pos_y *= pJS.canvas.pxratio;
  860. }
  861. pJS.interactivity.status = 'mousemove';
  862. });
  863. /* el on onmouseleave */
  864. pJS.interactivity.el.addEventListener('mouseleave', function(e){
  865. pJS.interactivity.mouse.pos_x = null;
  866. pJS.interactivity.mouse.pos_y = null;
  867. pJS.interactivity.status = 'mouseleave';
  868. });
  869. }
  870. /* on click event */
  871. if(pJS.interactivity.events.onclick.enable){
  872. pJS.interactivity.el.addEventListener('click', function(){
  873. pJS.interactivity.mouse.click_pos_x = pJS.interactivity.mouse.pos_x;
  874. pJS.interactivity.mouse.click_pos_y = pJS.interactivity.mouse.pos_y;
  875. pJS.interactivity.mouse.click_time = new Date().getTime();
  876. if(pJS.interactivity.events.onclick.enable){
  877. switch(pJS.interactivity.events.onclick.mode){
  878. case 'push':
  879. if(pJS.particles.move.enable){
  880. pJS.fn.modes.pushParticles(pJS.interactivity.modes.push.particles_nb, pJS.interactivity.mouse);
  881. }else{
  882. if(pJS.interactivity.modes.push.particles_nb == 1){
  883. pJS.fn.modes.pushParticles(pJS.interactivity.modes.push.particles_nb, pJS.interactivity.mouse);
  884. }
  885. else if(pJS.interactivity.modes.push.particles_nb > 1){
  886. pJS.fn.modes.pushParticles(pJS.interactivity.modes.push.particles_nb);
  887. }
  888. }
  889. break;
  890. case 'remove':
  891. pJS.fn.modes.removeParticles(pJS.interactivity.modes.remove.particles_nb);
  892. break;
  893. case 'bubble':
  894. pJS.tmp.bubble_clicking = true;
  895. break;
  896. case 'repulse':
  897. pJS.tmp.repulse_clicking = true;
  898. pJS.tmp.repulse_count = 0;
  899. pJS.tmp.repulse_finish = false;
  900. setTimeout(function(){
  901. pJS.tmp.repulse_clicking = false;
  902. }, pJS.interactivity.modes.repulse.duration*1000)
  903. break;
  904. }
  905. }
  906. });
  907. }
  908. };
  909. pJS.fn.vendors.densityAutoParticles = function(){
  910. if(pJS.particles.number.density.enable){
  911. /* calc area */
  912. var area = pJS.canvas.el.width * pJS.canvas.el.height / 1000;
  913. if(pJS.tmp.retina){
  914. area = area/(pJS.canvas.pxratio*2);
  915. }
  916. /* calc number of particles based on density area */
  917. var nb_particles = area * pJS.particles.number.value / pJS.particles.number.density.value_area;
  918. /* add or remove X particles */
  919. var missing_particles = pJS.particles.array.length - nb_particles;
  920. if(missing_particles < 0) pJS.fn.modes.pushParticles(Math.abs(missing_particles));
  921. else pJS.fn.modes.removeParticles(missing_particles);
  922. }
  923. };
  924. pJS.fn.vendors.checkOverlap = function(p1, position){
  925. for(var i = 0; i < pJS.particles.array.length; i++){
  926. var p2 = pJS.particles.array[i];
  927. var dx = p1.x - p2.x,
  928. dy = p1.y - p2.y,
  929. dist = Math.sqrt(dx*dx + dy*dy);
  930. if(dist <= p1.radius + p2.radius){
  931. p1.x = position ? position.x : Math.random() * pJS.canvas.w;
  932. p1.y = position ? position.y : Math.random() * pJS.canvas.h;
  933. pJS.fn.vendors.checkOverlap(p1);
  934. }
  935. }
  936. };
  937. pJS.fn.vendors.createSvgImg = function(p){
  938. /* set color to svg element */
  939. var svgXml = pJS.tmp.source_svg,
  940. rgbHex = /#([0-9A-F]{3,6})/gi,
  941. coloredSvgXml = svgXml.replace(rgbHex, function (m, r, g, b) {
  942. if(p.color.rgb){
  943. var color_value = 'rgba('+p.color.rgb.r+','+p.color.rgb.g+','+p.color.rgb.b+','+p.opacity+')';
  944. }else{
  945. var color_value = 'hsla('+p.color.hsl.h+','+p.color.hsl.s+'%,'+p.color.hsl.l+'%,'+p.opacity+')';
  946. }
  947. return color_value;
  948. });
  949. /* prepare to create img with colored svg */
  950. var svg = new Blob([coloredSvgXml], {type: 'image/svg+xml;charset=utf-8'}),
  951. DOMURL = window.URL || window.webkitURL || window,
  952. url = DOMURL.createObjectURL(svg);
  953. /* create particle img obj */
  954. var img = new Image();
  955. img.addEventListener('load', function(){
  956. p.img.obj = img;
  957. p.img.loaded = true;
  958. DOMURL.revokeObjectURL(url);
  959. pJS.tmp.count_svg++;
  960. });
  961. img.src = url;
  962. };
  963. pJS.fn.vendors.destroypJS = function(){
  964. cancelAnimationFrame(pJS.fn.drawAnimFrame);
  965. canvas_el.remove();
  966. pJSDom = null;
  967. };
  968. pJS.fn.vendors.drawShape = function(c, startX, startY, sideLength, sideCountNumerator, sideCountDenominator){
  969. // By Programming Thomas - https://programmingthomas.wordpress.com/2013/04/03/n-sided-shapes/
  970. var sideCount = sideCountNumerator * sideCountDenominator;
  971. var decimalSides = sideCountNumerator / sideCountDenominator;
  972. var interiorAngleDegrees = (180 * (decimalSides - 2)) / decimalSides;
  973. var interiorAngle = Math.PI - Math.PI * interiorAngleDegrees / 180; // convert to radians
  974. c.save();
  975. c.beginPath();
  976. c.translate(startX, startY);
  977. c.moveTo(0,0);
  978. for (var i = 0; i < sideCount; i++) {
  979. c.lineTo(sideLength,0);
  980. c.translate(sideLength,0);
  981. c.rotate(interiorAngle);
  982. }
  983. //c.stroke();
  984. c.fill();
  985. c.restore();
  986. };
  987. pJS.fn.vendors.exportImg = function(){
  988. window.open(pJS.canvas.el.toDataURL('image/png'), '_blank');
  989. };
  990. pJS.fn.vendors.loadImg = function(type){
  991. pJS.tmp.img_error = undefined;
  992. if(pJS.particles.shape.image.src != ''){
  993. if(type == 'svg'){
  994. var xhr = new XMLHttpRequest();
  995. xhr.open('GET', pJS.particles.shape.image.src);
  996. xhr.onreadystatechange = function (data) {
  997. if(xhr.readyState == 4){
  998. if(xhr.status == 200){
  999. pJS.tmp.source_svg = data.currentTarget.response;
  1000. pJS.fn.vendors.checkBeforeDraw();
  1001. }else{
  1002. console.log('Error pJS - Image not found');
  1003. pJS.tmp.img_error = true;
  1004. }
  1005. }
  1006. }
  1007. xhr.send();
  1008. }else{
  1009. var img = new Image();
  1010. img.addEventListener('load', function(){
  1011. pJS.tmp.img_obj = img;
  1012. pJS.fn.vendors.checkBeforeDraw();
  1013. });
  1014. img.src = pJS.particles.shape.image.src;
  1015. }
  1016. }else{
  1017. console.log('Error pJS - No image.src');
  1018. pJS.tmp.img_error = true;
  1019. }
  1020. };
  1021. pJS.fn.vendors.draw = function(){
  1022. if(pJS.particles.shape.type == 'image'){
  1023. if(pJS.tmp.img_type == 'svg'){
  1024. if(pJS.tmp.count_svg >= pJS.particles.number.value){
  1025. pJS.fn.particlesDraw();
  1026. if(!pJS.particles.move.enable) cancelRequestAnimFrame(pJS.fn.drawAnimFrame);
  1027. else pJS.fn.drawAnimFrame = requestAnimFrame(pJS.fn.vendors.draw);
  1028. }else{
  1029. //console.log('still loading...');
  1030. if(!pJS.tmp.img_error) pJS.fn.drawAnimFrame = requestAnimFrame(pJS.fn.vendors.draw);
  1031. }
  1032. }else{
  1033. if(pJS.tmp.img_obj != undefined){
  1034. pJS.fn.particlesDraw();
  1035. if(!pJS.particles.move.enable) cancelRequestAnimFrame(pJS.fn.drawAnimFrame);
  1036. else pJS.fn.drawAnimFrame = requestAnimFrame(pJS.fn.vendors.draw);
  1037. }else{
  1038. if(!pJS.tmp.img_error) pJS.fn.drawAnimFrame = requestAnimFrame(pJS.fn.vendors.draw);
  1039. }
  1040. }
  1041. }else{
  1042. pJS.fn.particlesDraw();
  1043. if(!pJS.particles.move.enable) cancelRequestAnimFrame(pJS.fn.drawAnimFrame);
  1044. else pJS.fn.drawAnimFrame = requestAnimFrame(pJS.fn.vendors.draw);
  1045. }
  1046. };
  1047. pJS.fn.vendors.checkBeforeDraw = function(){
  1048. // if shape is image
  1049. if(pJS.particles.shape.type == 'image'){
  1050. if(pJS.tmp.img_type == 'svg' && pJS.tmp.source_svg == undefined){
  1051. pJS.tmp.checkAnimFrame = requestAnimFrame(check);
  1052. }else{
  1053. //console.log('images loaded! cancel check');
  1054. cancelRequestAnimFrame(pJS.tmp.checkAnimFrame);
  1055. if(!pJS.tmp.img_error){
  1056. pJS.fn.vendors.init();
  1057. pJS.fn.vendors.draw();
  1058. }
  1059. }
  1060. }else{
  1061. pJS.fn.vendors.init();
  1062. pJS.fn.vendors.draw();
  1063. }
  1064. };
  1065. pJS.fn.vendors.init = function(){
  1066. /* init canvas + particles */
  1067. pJS.fn.retinaInit();
  1068. pJS.fn.canvasInit();
  1069. pJS.fn.canvasSize();
  1070. pJS.fn.canvasPaint();
  1071. pJS.fn.particlesCreate();
  1072. pJS.fn.vendors.densityAutoParticles();
  1073. /* particles.line_linked - convert hex colors to rgb */
  1074. pJS.particles.line_linked.color_rgb_line = hexToRgb(pJS.particles.line_linked.color);
  1075. };
  1076. pJS.fn.vendors.start = function(){
  1077. if(isInArray('image', pJS.particles.shape.type)){
  1078. pJS.tmp.img_type = pJS.particles.shape.image.src.substr(pJS.particles.shape.image.src.length - 3);
  1079. pJS.fn.vendors.loadImg(pJS.tmp.img_type);
  1080. }else{
  1081. pJS.fn.vendors.checkBeforeDraw();
  1082. }
  1083. };
  1084. /* ---------- pJS - start ------------ */
  1085. pJS.fn.vendors.eventsListeners();
  1086. pJS.fn.vendors.start();
  1087. };
  1088. /* ---------- global functions - vendors ------------ */
  1089. Object.deepExtend = function(destination, source) {
  1090. for (var property in source) {
  1091. if (source[property] && source[property].constructor &&
  1092. source[property].constructor === Object) {
  1093. destination[property] = destination[property] || {};
  1094. arguments.callee(destination[property], source[property]);
  1095. } else {
  1096. destination[property] = source[property];
  1097. }
  1098. }
  1099. return destination;
  1100. };
  1101. window.requestAnimFrame = (function(){
  1102. return window.requestAnimationFrame ||
  1103. window.webkitRequestAnimationFrame ||
  1104. window.mozRequestAnimationFrame ||
  1105. window.oRequestAnimationFrame ||
  1106. window.msRequestAnimationFrame ||
  1107. function(callback){
  1108. window.setTimeout(callback, 1000 / 60);
  1109. };
  1110. })();
  1111. window.cancelRequestAnimFrame = ( function() {
  1112. return window.cancelAnimationFrame ||
  1113. window.webkitCancelRequestAnimationFrame ||
  1114. window.mozCancelRequestAnimationFrame ||
  1115. window.oCancelRequestAnimationFrame ||
  1116. window.msCancelRequestAnimationFrame ||
  1117. clearTimeout
  1118. } )();
  1119. function hexToRgb(hex){
  1120. // By Tim Down - http://stackoverflow.com/a/5624139/3493650
  1121. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  1122. var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  1123. hex = hex.replace(shorthandRegex, function(m, r, g, b) {
  1124. return r + r + g + g + b + b;
  1125. });
  1126. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  1127. return result ? {
  1128. r: parseInt(result[1], 16),
  1129. g: parseInt(result[2], 16),
  1130. b: parseInt(result[3], 16)
  1131. } : null;
  1132. };
  1133. function clamp(number, min, max) {
  1134. return Math.min(Math.max(number, min), max);
  1135. };
  1136. function isInArray(value, array) {
  1137. return array.indexOf(value) > -1;
  1138. }
  1139. /* ---------- particles.js functions - start ------------ */
  1140. window.pJSDom = [];
  1141. window.particlesJS = function(tag_id, params){
  1142. //console.log(params);
  1143. /* no string id? so it's object params, and set the id with default id */
  1144. if(typeof(tag_id) != 'string'){
  1145. params = tag_id;
  1146. tag_id = 'particles-js';
  1147. }
  1148. /* no id? set the id to default id */
  1149. if(!tag_id){
  1150. tag_id = 'particles-js';
  1151. }
  1152. /* pJS elements */
  1153. var pJS_tag = document.getElementById(tag_id),
  1154. pJS_canvas_class = 'particles-js-canvas-el',
  1155. exist_canvas = pJS_tag.getElementsByClassName(pJS_canvas_class);
  1156. /* remove canvas if exists into the pJS target tag */
  1157. if(exist_canvas.length){
  1158. while(exist_canvas.length > 0){
  1159. pJS_tag.removeChild(exist_canvas[0]);
  1160. }
  1161. }
  1162. /* create canvas element */
  1163. var canvas_el = document.createElement('canvas');
  1164. canvas_el.className = pJS_canvas_class;
  1165. /* set size canvas */
  1166. canvas_el.style.width = "100%";
  1167. canvas_el.style.height = "100%";
  1168. /* append canvas */
  1169. var canvas = document.getElementById(tag_id).appendChild(canvas_el);
  1170. /* launch particle.js */
  1171. if(canvas != null){
  1172. pJSDom.push(new pJS(tag_id, params));
  1173. }
  1174. };
  1175. window.particlesJS.load = function(tag_id, path_config_json, callback){
  1176. /* load json config */
  1177. var xhr = new XMLHttpRequest();
  1178. xhr.open('GET', path_config_json);
  1179. xhr.onreadystatechange = function (data) {
  1180. if(xhr.readyState == 4){
  1181. if(xhr.status == 200){
  1182. var params = JSON.parse(data.currentTarget.response);
  1183. window.particlesJS(tag_id, params);
  1184. if(callback) callback();
  1185. }else{
  1186. console.log('Error pJS - XMLHttpRequest status: '+xhr.status);
  1187. console.log('Error pJS - File config not found');
  1188. }
  1189. }
  1190. };
  1191. xhr.send();
  1192. };