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

调用方法:

html:

  1. <div class="layui-canvs"></div>

js:

  1. $(".layui-canvs").jParticle({
  2. background: "#141414",
  3. color: "#E6E6E6"
  4. });

注意:需要先设置 canvas的宽高

js之:漂浮线的更多相关文章

  1. Js弹性漂浮广告代码

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  2. three.js 创建点 线 面

    <html> <head> <title>My first three.js app</title> <style> body { marg ...

  3. d3.js制作连线动画图和编辑器

    此文章为原创文章,原文地址:https://www.cnblogs.com/eagle1098/p/11431679.html 连线动画图 编辑器 效果如上图所示.本项目使用主要d3.jsv4制作,分 ...

  4. js 开源k线图开发库

    https://github.com/andredumas/techan.js/wiki http://techanjs.org/ A visual, stock charting (Candlest ...

  5. vue.js打包部署线上

    你完成了工程开发,需要部署到外网环境,要进行下面的步骤: 一.首先你要购买一个服务器或者有自己的服务器.我介绍给大家的一个免费的服务器:http://free.3v.do/index.html可以免费 ...

  6. JavaScript浮动广告代码,容纯DIV/CSS对联漂浮广告代码,兼容性非常好的js右下角与漂浮广告代码

    基于JavaScript代码实现随机漂浮图片广告,javascript图片广告 在网上有很多这样的代码,不过未必符合W3C标准,因为在头部加上<!DOCTYPE html>类似标签之后,漂 ...

  7. 一些js 插件的作用

    前言: 从一些开源网站上下载下来的 后台管理系统模板一般会有很多的js ,其js 的功能是什么呢?这里随手查询了一下,记录下来 正文: 1.zDialog.js 各种弹窗插件详细案例:http://w ...

  8. 怎么评价Facebook的Relay框架?Meteor.js 是什么?

    http://www.zhihu.com/question/34531232?rf=34500201 Meteor.js 是什么? 作者:陈天链接:http://www.zhihu.com/quest ...

  9. JS常用正则表达式备忘录

    摘要: 玩转正则表达式. 原文:JS常用正则表达式备忘录 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 正则表达式或"regex"用于匹配字符串的各个部分 下面是 ...

随机推荐

  1. .NET写入文件操作

    2018-01-16  22:44:35 许多程序需要记录运行日志,这就需要将程序运行记录写入本机,一般是.txt 文本或.csv 文件.具体操作如下: 一.C#   using System.IO; ...

  2. echart图表中legend不显示问题

    主要是legend中的name要和series中的name对应上.

  3. PDIUSBD12管脚简述

    PDIUSBD12管脚简述          PDIUSBD12管脚及简述 PDIUSBD12读写时序图 CS_N是片选信号,当片选信号位低电平时,下面的操作才有效.由于板子上将CS_N接地,所以它一 ...

  4. [译]ABP框架v2.3.0已经发布!

    在新冠病毒的日子里,我们发布了ABP框架v2.3, 这篇文章将说明本次发布新增内容和过去的两周我们做了什么. 关于新冠病毒和我们的团队 关于冠状病毒的状况我们很难过.在Volosoft的团队,我们有不 ...

  5. 在Windows中像Linux里一样使用CMake和make

    1. 安装GCC环境 1.1 安装MinGW(Minimalist GNU for Windows) 首先下载MinGW,并安装.安装完成之后运行MinGW Installer.界面如下.勾选自己需要 ...

  6. F版本SpringCloud1—大白话为啥要有微服务?啥是微服务?SpringCloud为什么有那么多组件?

    前言 为什么要有微服务呢? 什么是微服务? SpringCloud 中为什么会有那么多的组件? ...... 作为SpringCloud教程的第一篇,不讲解具体的技术使用,通过一个通俗易懂的小故事,来 ...

  7. 二维数组及Arrays工具类

    1.二维数组 概念: 数组中的每一个元素类型都是一维数组 二维数组初始化方式: 静态初始化: 格式: 元素类型[][] 数组名 = new 元素类型[][]{{一维数组1},{一维数组2},{一维数组 ...

  8. [UWP]抄抄《CSS 故障艺术》的动画

    1. 前言 什么是故障艺术(Glitch Art 风)?我们熟知的抖音的 LOGO 正是故障艺术其中一种表现形式.它有一种魔幻的感觉,看起来具有闪烁.震动的效果,很吸引人眼球.故障艺术它模拟了画面信号 ...

  9. Django redis的使用

    一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...

  10. MySQL数据库02

    MySQL数据库 前言: 前面我们了解了什么是数据库,什么是MySQL数据库以及如何运用,接下来我们接着深入学习MySQL. (提前声明,以下所提供的事例不标准,仅供参考) 数据库的备份与还原: 备份 ...