123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- (function initJParticle( $ ){
- "use strict";
- var createParticlesSandbox, Utils;
- Utils = {};
- /*
- * Create jParticle animation.
- * @param {Object} options Few jParticle options.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.jParticle = function jParticle( options ){
-
- this.each(function( _, el ){
- if ( typeof el.sandbox === 'object' ) {
- $( el ).removeJParticle();
- }
- el.sandbox = createParticlesSandbox( el, options );
- });
-
- return this;
- };
- /*
- * Remove jParticle canvas.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.removeJParticle = function removeJParticle(){
- this.each(function( _, el ){
- if ( el.sandbox ) {
- el.sandbox.remove();
- delete el.sandbox;
- }
- });
- return this;
- };
- /*
- * Freeze jParticle animation.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.freezeJParticle = function freezeJParticle(){
- this.each(function( _, el ){
- if ( el.sandbox ) {
- el.sandbox.freeze();
- }
- });
- return this;
- };
- /*
- * Unfreeze jParticle animation.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.unfreezeJParticle = function unfreezeJParticle(){
- this.each(function( _, el ){
- if ( el.sandbox ) {
- el.sandbox.unfreeze();
- }
- });
- return this;
- };
- /*
- * Create a particles sandbox instance.
- * @param {Object} element Element for the sandbox.
- * @param {Object} params Few sandbox's params.
- * @return {Object} Particles sandbox object.
- */
- createParticlesSandbox = function createParticlesSandbox( element, params ){
- var ParticlesSandbox, createParticle;
- ParticlesSandbox = {};
-
- ParticlesSandbox.canvas = {};
- ParticlesSandbox.mouse = {};
- ParticlesSandbox.particles = [];
- ParticlesSandbox.isAnimated = false;
- /*
- * Initialize the sandbox
- * @param {Object} element Element for the sandbox.
- * @param {Object} params Few sandbox's params.
- */
- ParticlesSandbox.initialize = function initialize( element, params ){
- ParticlesSandbox.initParams( params );
- ParticlesSandbox.initHTML( element );
- ParticlesSandbox.initParticles();
- ParticlesSandbox.initEvents();
- ParticlesSandbox.initAnimation();
- };
- /*
- * Initialize sandbox's params.
- * @param {Object} params Few sandbox's params.
- */
- ParticlesSandbox.initParams = function initParams( params ){
- if ( params && params.color && (!params.particle || ( params.particle && !params.particle.color ) ) ) {
- if ( !params.particle ) {
- params.particle = {};
- }
- params.particle.color = params.color;
- }
- ParticlesSandbox.params = $.extend({
- particlesNumber: 100,
- linkDist: 50,
- createLinkDist: 150,
- disableLinks: false,
- disableMouse: false,
- background: 'black',
- color: 'white',
- width: null,
- height: null,
- linksWidth: 1
- }, params );
- };
- /*
- * Initialize the sandbox's html.
- * @param {Object} element Element for the sandbox.
- */
- ParticlesSandbox.initHTML = function initHTML( element ){
- var canvas;
- canvas = ParticlesSandbox.canvas;
- canvas.container = $( element );
- canvas.element = $('<canvas/>');
- canvas.context = canvas.element.get(0).getContext('2d');
- canvas.container.append( canvas.element );
- canvas.element.css( 'display', 'block' );
- canvas.element.get(0).width = ( ParticlesSandbox.params.width ) ? ParticlesSandbox.params.width : canvas.container.width();
- canvas.element.get(0).height = ( ParticlesSandbox.params.height ) ? ParticlesSandbox.params.height : canvas.container.height();
- canvas.element.css( 'background', ParticlesSandbox.params.background );
- };
- /*
- * Resize canvas.
- */
- ParticlesSandbox.resize = function resize( width, height ){
- if ( width ) {
- canvas.element.get(0).width = width;
- }
- if ( height ) {
- canvas.element.get(0).height = height;
- }
- };
- /*
- * Create all particles in the sandbox.
- */
- ParticlesSandbox.initParticles = function initParticles(){
- var i, count;
- i = 0;
- count = ParticlesSandbox.params.particlesNumber;
- for ( ; i < count; i += 1 ) {
- ParticlesSandbox.particles.push( createParticle(
- ParticlesSandbox.canvas.element.get(0),
- ParticlesSandbox.params.particle
- ) );
- }
- };
- /*
- * Initialize the sandbox's events.
- */
- ParticlesSandbox.initEvents = function initEvents(){
- ParticlesSandbox.canvas.element.mouseenter(function mouseEnterCallback(){
- ParticlesSandbox.mouse.hoverCanvas = true;
- if ( !ParticlesSandbox.isAnimated ) {
- ParticlesSandbox.draw();
- }
- });
- ParticlesSandbox.canvas.element.mouseleave(function mouseLeaveCallback(){
- ParticlesSandbox.mouse.hoverCanvas = false;
- });
- ParticlesSandbox.canvas.element.mousemove(function mouseMoveCallback(e){
- ParticlesSandbox.mouse = $.extend( ParticlesSandbox.mouse, Utils.getMousePosition( e, ParticlesSandbox.canvas.element[0] ) );
- });
- };
- /*
- * Initialize the sandbox's animation.
- */
- ParticlesSandbox.initAnimation = function initAnimation(){
- window.requestAnimFrame =
- window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.ORequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function requestAnimFrame( callback ){
- setTimeOut( callback, 1000/60 );
- };
- ParticlesSandbox.isAnimated = true;
- ParticlesSandbox.draw();
- };
- /*
- * Draw the sandbox canvas.
- */
- ParticlesSandbox.draw = function draw(){
- var i, j, count, canvas, particle, particle2;
- i = 0;
- count = ParticlesSandbox.particles.length;
- canvas = ParticlesSandbox.canvas;
- canvas.context.clearRect( 0, 0, canvas.element.get(0).width, canvas.element.get(0).height );
-
- for ( ; i < count; i += 1 ) {
- particle = ParticlesSandbox.particles[i];
- if ( ParticlesSandbox.isAnimated ) {
- particle.update();
- }
- particle.draw();
- if ( !ParticlesSandbox.params.disableMouse && ParticlesSandbox.mouse.hoverCanvas ) {
- ParticlesSandbox.drawLink(
- particle.getPosition('x'),
- particle.getPosition('y'),
- ParticlesSandbox.mouse.x,
- ParticlesSandbox.mouse.y
- );
- }
- if ( !ParticlesSandbox.params.disableLinks ) {
- for ( j = i+1; j < count; j += 1 ) {
- particle2 = ParticlesSandbox.particles[j];
- ParticlesSandbox.drawLink(
- particle.getPosition('x'),
- particle.getPosition('y'),
- particle2.getPosition('x'),
- particle2.getPosition('y')
- );
- }
- }
- }
- ParticlesSandbox.requestID = window.requestAnimFrame( ParticlesSandbox.draw );
- };
- /*
- * Draw a link between two particles.
- * @param {int} x First object abscissa coords.
- * @param {int} y First object ordered coords.
- * @param {int} x2 Second object abscissa coords.
- * @param {int} y2 Second object ordered coords.
- */
- ParticlesSandbox.drawLink = function drawLink( x, y, x2, y2 ){
- var context;
- if ( Utils.getDistance( x, y, x2, y2 ) <= ParticlesSandbox.params.createLinkDist ) {
- context = ParticlesSandbox.canvas.context;
- context.save();
- context.beginPath();
- context.lineWidth = ParticlesSandbox.params.linksWidth;
- context.moveTo( x, y );
- context.lineTo( x2, y2 );
- context.globalAlpha = ParticlesSandbox.getOpacityLink( x, y, x2, y2 );
- context.strokeStyle = ParticlesSandbox.params.color;
- context.lineCap = 'round';
- context.stroke();
- context.closePath();
- context.restore();
- }
- };
- /*
- * Get opacity for link two particles.
- * @param {int} x First object abscissa coords.
- * @param {int} y First object ordered coords.
- * @param {int} x2 Second object abscissa coords.
- * @param {int} y2 Second object ordered coords.
- * @return {int} 0 <= opacity <= 1
- */
- ParticlesSandbox.getOpacityLink = function getOpacityLink( x, y, x2, y2 ){
- var dist, opacity, linkDist, createLinkDist;
- dist = Utils.getDistance( x, y, x2, y2 );
- linkDist = ParticlesSandbox.params.linkDist;
- createLinkDist = ParticlesSandbox.params.createLinkDist;
- if ( dist <= linkDist ) {
- opacity = 1;
- } else if ( dist > createLinkDist ) {
- opacity = 0;
- } else {
- opacity = 1 - ( ( ( dist - linkDist ) * 100 ) / ( createLinkDist - linkDist ) ) / 100;
- }
- return opacity;
- };
- /*
- * Freeze the animation.
- */
- ParticlesSandbox.freeze = function freeze(){
- if ( ParticlesSandbox.isAnimated ) {
- ParticlesSandbox.isAnimated = false;
- }
- };
- /*
- * Unfreeze the animation.
- */
- ParticlesSandbox.unfreeze = function unfreeze(){
- if ( !ParticlesSandbox.isAnimated ) {
- ParticlesSandbox.isAnimated = true;
- }
- };
- /*
- * Remove the animation's canvas.
- */
- ParticlesSandbox.remove = function remove(){
- ParticlesSandbox.canvas.element.remove();
- };
- /*
- * Create a particle instance.
- * @param {Object} canvas DOM element.
- * @param {Object} params Few particle's params.
- * @return {Object} Particle object.
- */
- createParticle = function createParticle( canvas, params ){
- var Particle;
- Particle = {};
- Particle.canvas = {};
- Particle.vector = {};
- /*
- * Initialize the particle.
- * @param {Object} canvas DOM element.
- * @param {Object} params Few particle's params.
- */
- Particle.initialize = function initialize( canvas, params ){
- Particle.params = $.extend({
- color: 'white',
- minSize: 2,
- maxSize: 4,
- speed: 60
- }, params );
- Particle.setCanvasContext( canvas );
- Particle.initSize();
- Particle.initPosition();
- Particle.initVectors();
- };
- /*
- * Initialize particle's position.
- */
- Particle.initPosition = function initPosition(){
- Particle.x = Utils.getRandNumber( 0 + Particle.radius, Particle.canvas.element.width - Particle.radius );
- Particle.y = Utils.getRandNumber( 0 + Particle.radius, Particle.canvas.element.height - Particle.radius );
- };
- /*
- * Initialize particle's size.
- */
- Particle.initSize = function initSize(){
- Particle.size = Utils.getRandNumber( Particle.params.minSize, Particle.params.maxSize );
- Particle.radius = Particle.size / 2;
- };
- /*
- * Initialize particle's vectors for speed.
- */
- Particle.initVectors = function initVectors(){
- do {
- Particle.vector.x = Utils.getRandNumber( -Particle.params.speed / 60, Particle.params.speed / 60, false );
- Particle.vector.y = Utils.getRandNumber( -Particle.params.speed / 60, Particle.params.speed / 60, false );
- } while ( Particle.vector.x == 0 || Particle.vector.y == 0 )
- };
- /*
- * Set the context to draw particles.
- * @param {Object} canvas Canvas.
- */
- Particle.setCanvasContext = function setCanvasContext( canvas ){
- var context;
- Particle.canvas.element = canvas;
- context = canvas.getContext('2d');
- if ( typeof context === 'object' && typeof context.canvas === 'object' ) {
- Particle.canvas.context = context;
- } else {
- throw "Error: Can't set canvas context to Particle because context isn't a CanvasRenderingContext2D object.";
- }
- };
- /*
- * Draw particle.
- */
- Particle.draw = function draw(){
- var context = Particle.canvas.context;
- context.beginPath();
- context.arc( Particle.x, Particle.y, Particle.size /2, 0, Math.PI*2 );
- context.fillStyle = Particle.params.color;
- context.fill();
- context.closePath();
- };
- /*
- * Update the particle's position.
- */
- Particle.update = function update(){
- Particle.x += Particle.vector.x;
- Particle.y += Particle.vector.y;
- if ( 0 > ( Particle.x - Particle.radius ) || ( Particle.x + Particle.radius ) > Particle.canvas.element.width ) {
- Particle.vector.x = -Particle.vector.x;
- }
- if ( 0 > ( Particle.y - Particle.radius ) || ( Particle.y + Particle.radius ) > Particle.canvas.element.height ) {
- Particle.vector.y = -Particle.vector.y;
- }
- };
- /*
- * Return position of particle.
- * @param {string} axis Optionnal axis.
- * @return {int|Object} Return object if axis is not defined, else return int.
- */
- Particle.getPosition = function getPosition( axis ){
- if ( typeof axis === 'string' && ( axis != 'x' && axis != 'y' ) ) {
- axis = null;
- }
- return ( typeof( axis ) === 'string' ) ? Particle[ axis ] : { x: Particle.x, y: Particle.y };
- };
- Particle.initialize( canvas, params );
- return {
- getPosition: Particle.getPosition,
- update: Particle.update,
- draw: Particle.draw
- };
- };
- ParticlesSandbox.initialize( element, params );
- return {
- remove: ParticlesSandbox.remove,
- freeze: ParticlesSandbox.freeze,
- unfreeze: ParticlesSandbox.unfreeze,
- resize: ParticlesSandbox.resize
- };
- };
- /*
- * Get rand number between x and y.
- * @param {int} x Minimal number.
- * @param {int} y Maximal number.
- * @param {Boolean} round True is value shouldn't be round.
- * @return {int} Rand number.
- */
- Utils.getRandNumber = function getRandNumber( x, y, round ){
- var value;
- if( x == null ) {
- x = 0;
- }
- if( y == null ) {
- y = 10;
- }
- if( round == null ) {
- round = true;
- }
- value = Math.random() * ( y - x ) + x;
- return ( round ) ? Math.round( value ) : value;
- };
- /*
- * Get distance between to cartesian points.
- * @param {int} x First object abscissa coords.
- * @param {int} y First object ordered coords.
- * @param {int} x2 Second object abscissa coords.
- * @param {int} y2 Second object ordered coords.
- * @return {int} Distance.
- */
- Utils.getDistance = function getDistance( x, y, x2, y2 ){
- return Math.sqrt( Math.pow( x2 - x, 2 ) + Math.pow( y2 - y, 2 ) );
- };
- /*
- * Get mouse position.
- * @param {Object} event The HTML DOM events.
- * @param {Object} element The DOM element.
- * @return {Object} x/y position.
- */
- Utils.getMousePosition = function getMousePosition( event, element ){
- var rect;
- if ( typeof element === 'undefined' ) {
- element = $('body')[0];
- }
- rect = element.getBoundingClientRect();
- return {
- x: event.clientX - rect.left,
- y: event.clientY - rect.top
- };
- };
- })( jQuery )
|