particles.js 42 KB

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