22 * A lightweight, dependency-free and responsive javascript plugin for particle backgrounds.
33 *
44 * @author Marc Bruederlin <[email protected] > 5- * @version 2.0.0
5+ * @version 2.0.1
66 * @license MIT
77 * @see https://github.com/marcbruederlin/particles.js
88 */
@@ -35,11 +35,11 @@ var Particles = (function(window, document) {
3535
3636 _ . element = null ;
3737 _ . context = null ;
38- _ . activeBreakpoint = null ;
38+ _ . ratio = null ;
3939 _ . breakpoints = [ ] ;
40+ _ . activeBreakpoint = null ;
4041 _ . breakpointSettings = [ ] ;
4142 _ . originalSettings = null ;
42- _ . windowWidth = 0 ;
4343 _ . storage = [ ] ;
4444 }
4545
@@ -61,10 +61,9 @@ var Particles = (function(window, document) {
6161
6262 _ . _initializeCanvas ( ) ;
6363 _ . _initializeEvents ( ) ;
64- _ . _initializeStorage ( ) ;
6564 _ . _registerBreakpoints ( ) ;
6665 _ . _checkResponsive ( ) ;
67-
66+ _ . _initializeStorage ( ) ;
6867 _ . _animate ( ) ;
6968 } ;
7069
@@ -74,7 +73,7 @@ var Particles = (function(window, document) {
7473 * @private
7574 */
7675 Plugin . prototype . _initializeCanvas = function ( ) {
77- var _ = this ;
76+ var _ = this , devicePixelRatio , backingStoreRatio ;
7877
7978 if ( ! _ . options . selector ) {
8079 console . warn ( 'particles.js: No selector specified! Check https://github.com/marcbruederlin/particles.js#options' ) ;
@@ -83,9 +82,19 @@ var Particles = (function(window, document) {
8382
8483 _ . element = document . querySelector ( _ . options . selector ) ;
8584 _ . context = _ . element . getContext ( '2d' ) ;
85+
86+ devicePixelRatio = window . devicePixelRatio || 1 ;
87+ backingStoreRatio = _ . context . webkitBackingStorePixelRatio || _ . context . mozBackingStorePixelRatio || _ . context . msBackingStorePixelRatio ||
88+ _ . context . oBackingStorePixelRatio || _ . context . backingStorePixelRatio || 1 ;
89+
90+ _ . ratio = devicePixelRatio / backingStoreRatio ;
91+
92+ _ . element . width = window . innerWidth * _ . ratio ;
93+ _ . element . height = window . innerHeight * _ . ratio ;
94+ _ . element . style . width = '100%' ;
95+ _ . element . style . height = '100%' ;
8696
87- _ . element . width = window . innerWidth ;
88- _ . element . height = window . innerHeight ;
97+ _ . context . scale ( _ . ratio , _ . ratio ) ;
8998 } ;
9099
91100 /**
@@ -110,7 +119,7 @@ var Particles = (function(window, document) {
110119 _ . storage = [ ] ;
111120
112121 for ( var i = _ . options . maxParticles ; i -- ; ) {
113- _ . storage . push ( new Particle ( _ . element , _ . context , _ . options ) ) ;
122+ _ . storage . push ( new Particle ( _ . context , _ . options ) ) ;
114123 }
115124 } ;
116125
@@ -120,8 +129,7 @@ var Particles = (function(window, document) {
120129 * @private
121130 */
122131 Plugin . prototype . _registerBreakpoints = function ( ) {
123- var _ = this , breakpoint , currentBreakpoint , l ,
124- responsiveSettings = _ . options . responsive || null ;
132+ var _ = this , breakpoint , currentBreakpoint , l , responsiveSettings = _ . options . responsive || null ;
125133
126134 if ( typeof responsiveSettings === 'object' && responsiveSettings !== null && responsiveSettings . length ) {
127135 for ( breakpoint in responsiveSettings ) {
@@ -158,9 +166,7 @@ var Particles = (function(window, document) {
158166 * @private
159167 */
160168 Plugin . prototype . _checkResponsive = function ( ) {
161- var _ = this ,
162- breakpoint , targetBreakpoint = false ,
163- windowWidth = window . innerWidth ;
169+ var _ = this , breakpoint , targetBreakpoint = false , windowWidth = window . innerWidth ;
164170
165171 if ( _ . options . responsive && _ . options . responsive . length && _ . options . responsive !== null ) {
166172 targetBreakpoint = null ;
@@ -207,18 +213,17 @@ var Particles = (function(window, document) {
207213 Plugin . prototype . _resize = function ( ) {
208214 var _ = this ;
209215
210- _ . element . width = window . innerWidth ;
211- _ . element . height = window . innerHeight ;
216+ _ . element . width = window . innerWidth * _ . ratio ;
217+ _ . element . height = window . innerHeight * _ . ratio ;
212218
213- if ( window . innerWidth !== _ . windowWidth ) {
214- clearTimeout ( _ . windowDelay ) ;
219+ _ . context . scale ( _ . ratio , _ . ratio ) ;
215220
216- _ . windowDelay = window . setTimeout ( function ( ) {
217- _ . windowWidth = window . innerWidth ;
218- _ . _checkResponsive ( ) ;
219- _ . _refresh ( ) ;
220- } , 50 ) ;
221- }
221+ clearTimeout ( _ . windowDelay ) ;
222+
223+ _ . windowDelay = window . setTimeout ( function ( ) {
224+ _ . _checkResponsive ( ) ;
225+ _ . _refresh ( ) ;
226+ } , 50 ) ;
222227 } ;
223228
224229 /**
@@ -265,16 +270,16 @@ var Particles = (function(window, document) {
265270 particle . x += particle . vx ;
266271 particle . y += particle . vy ;
267272
268- if ( particle . x + particle . radius > _ . element . width ) {
273+ if ( particle . x + particle . radius > window . innerWidth ) {
269274 particle . x = particle . radius ;
270275 } else if ( particle . x - particle . radius < 0 ) {
271- particle . x = _ . element . width - particle . radius ;
276+ particle . x = window . innerWidth - particle . radius ;
272277 }
273278
274- if ( particle . y + particle . radius > _ . element . height ) {
279+ if ( particle . y + particle . radius > window . innerHeight ) {
275280 particle . y = particle . radius ;
276281 } else if ( particle . y - particle . radius < 0 ) {
277- particle . y = _ . element . height - particle . radius ;
282+ particle . y = window . innerHeight - particle . radius ;
278283 }
279284
280285 if ( _ . options . connectParticles ) {
@@ -318,7 +323,9 @@ var Particles = (function(window, document) {
318323 * @param {object } obj
319324 */
320325 Plugin . prototype . _extend = function ( source , obj ) {
321- Object . keys ( obj ) . forEach ( function ( key ) { source [ key ] = obj [ key ] ; } ) ;
326+ Object . keys ( obj ) . forEach ( function ( key ) {
327+ source [ key ] = obj [ key ] ;
328+ } ) ;
322329
323330 return source ;
324331 } ;
@@ -344,20 +351,18 @@ var Particles = (function(window, document) {
344351 * Represents a single particle.
345352 *
346353 * @constructor
347- * @param {object } element
348354 * @param {object } context
349355 * @param {object } options
350356 *
351357 */
352- Particle = function ( element , context , options ) {
358+ Particle = function ( context , options ) {
353359 var _ = this ;
354360
355- _ . element = element ;
356361 _ . context = context ;
357362 _ . options = options ;
358363
359- _ . x = Math . random ( ) * _ . element . width ;
360- _ . y = Math . random ( ) * _ . element . height ;
364+ _ . x = Math . random ( ) * window . innerWidth ;
365+ _ . y = Math . random ( ) * window . innerHeight ;
361366 _ . vx = Math . random ( ) * _ . options . speed * 2 - _ . options . speed ;
362367 _ . vy = Math . random ( ) * _ . options . speed * 2 - _ . options . speed ;
363368 _ . radius = Math . random ( ) * Math . random ( ) * _ . options . sizeVariations ;
@@ -375,7 +380,7 @@ var Particles = (function(window, document) {
375380
376381 _ . context . fillStyle = 'rgb(' + _ . options . color . r + ', ' + _ . options . color . g + ', ' + _ . options . color . b + ')' ;
377382 _ . context . beginPath ( ) ;
378- _ . context . arc ( _ . x , this . y , _ . radius , 0 , Math . PI * 2 , false ) ;
383+ _ . context . arc ( _ . x , _ . y , _ . radius , 0 , Math . PI * 2 , false ) ;
379384 _ . context . fill ( ) ;
380385 } ;
381386
0 commit comments