@@ -34,6 +34,7 @@ function Granim(options) {
34
34
this . isCanvasInWindowView = false ;
35
35
this . firstScrollInit = true ;
36
36
this . animating = false ;
37
+ this . gradientLength = this . states [ this . activeState ] . gradients [ 0 ] . length ;
37
38
if ( options . image && options . image . source ) {
38
39
this . image = {
39
40
source : options . image . source ,
@@ -42,7 +43,6 @@ function Granim(options) {
42
43
blendingMode : options . image . blendingMode || false
43
44
} ;
44
45
}
45
-
46
46
this . events = {
47
47
start : new CustomEvent ( 'granim:start' ) ,
48
48
end : new CustomEvent ( 'granim:end' ) ,
@@ -56,7 +56,7 @@ function Granim(options) {
56
56
} ,
57
57
bubbles : false ,
58
58
cancelable : false
59
- } )
59
+ } ) ;
60
60
}
61
61
} ;
62
62
this . callbacks = {
@@ -82,6 +82,7 @@ function Granim(options) {
82
82
83
83
if ( this . isPausedWhenNotInView ) {
84
84
this . onScroll ( ) ;
85
+
85
86
} else {
86
87
if ( ! this . image ) {
87
88
this . refreshColorsAndPos ( ) ;
@@ -95,65 +96,36 @@ function Granim(options) {
95
96
this . canvas . dispatchEvent ( this . events . start ) ;
96
97
}
97
98
98
- Granim . prototype . onResize = require ( './onResize.js' ) ;
99
-
100
- Granim . prototype . onScroll = require ( './onScroll.js' ) ;
101
-
102
- Granim . prototype . validateInput = require ( './validateInput.js' ) ;
103
-
104
- Granim . prototype . triggerError = require ( './triggerError.js' ) ;
105
-
99
+ Granim . prototype . animateColors = require ( './animateColors.js' ) ;
100
+ Granim . prototype . changeBlendingMode = require ( './changeBlendingMode.js' ) ;
101
+ Granim . prototype . changeDirection = require ( './changeDirection.js' ) ;
102
+ Granim . prototype . changeState = require ( './changeState.js' ) ;
103
+ Granim . prototype . clear = require ( './clear.js' ) ;
106
104
Granim . prototype . convertColorToRgba = require ( './convertColorToRgba.js' ) ;
107
-
108
- Granim . prototype . prepareImage = require ( './prepareImage.js' ) ;
109
-
105
+ Granim . prototype . destroy = require ( './destroy.js' ) ;
110
106
Granim . prototype . eventPolyfill = require ( './eventPolyfill.js' ) ;
111
-
112
107
Granim . prototype . getColor = require ( './getColor.js' ) ;
113
-
114
- Granim . prototype . getColorPos = require ( './getColorPos.js' ) ;
115
-
116
108
Granim . prototype . getColorDiff = require ( './getColorDiff.js' ) ;
117
-
109
+ Granim . prototype . getColorPos = require ( './getColorPos.js' ) ;
118
110
Granim . prototype . getColorPosDiff = require ( './getColorPosDiff.js' ) ;
119
-
120
- Granim . prototype . setDirection = require ( './setDirection.js' ) ;
121
-
122
- Granim . prototype . setColors = require ( './setColors.js' ) ;
123
-
124
- Granim . prototype . getElement = require ( './getElement.js' ) ;
125
-
126
- Granim . prototype . getDimensions = require ( './getDimensions.js' ) ;
127
-
128
- Granim . prototype . getLightness = require ( './getLightness.js' ) ;
129
-
130
111
Granim . prototype . getCurrentColors = require ( './getCurrentColors.js' ) ;
131
-
132
112
Granim . prototype . getCurrentColorsPos = require ( './getCurrentColorsPos.js' ) ;
133
-
134
- Granim . prototype . animateColors = require ( './animateColors.js' ) ;
135
-
136
- Granim . prototype . refreshColorsAndPos = require ( './refreshColorsAndPos.js' ) ;
137
-
113
+ Granim . prototype . getDimensions = require ( './getDimensions.js' ) ;
114
+ Granim . prototype . getElement = require ( './getElement.js' ) ;
115
+ Granim . prototype . getLightness = require ( './getLightness.js' ) ;
138
116
Granim . prototype . makeGradient = require ( './makeGradient.js' ) ;
139
-
117
+ Granim . prototype . onResize = require ( './onResize.js' ) ;
118
+ Granim . prototype . onScroll = require ( './onScroll.js' ) ;
140
119
Granim . prototype . pause = require ( './pause.js' ) ;
141
-
142
- Granim . prototype . play = require ( './play.js' ) ;
143
-
144
- Granim . prototype . clear = require ( './clear.js' ) ;
145
-
146
- Granim . prototype . destroy = require ( './destroy.js' ) ;
147
-
148
120
Granim . prototype . pauseWhenNotInView = require ( './pauseWhenNotInView.js' ) ;
149
-
121
+ Granim . prototype . play = require ( './play.js' ) ;
122
+ Granim . prototype . prepareImage = require ( './prepareImage.js' ) ;
123
+ Granim . prototype . refreshColorsAndPos = require ( './refreshColorsAndPos.js' ) ;
124
+ Granim . prototype . setColors = require ( './setColors.js' ) ;
125
+ Granim . prototype . setDirection = require ( './setDirection.js' ) ;
150
126
Granim . prototype . setSizeAttributes = require ( './setSizeAttributes.js' ) ;
151
-
152
- Granim . prototype . changeDirection = require ( './changeDirection.js' ) ;
153
-
154
- Granim . prototype . changeBlendingMode = require ( './changeBlendingMode.js' ) ;
155
-
156
- Granim . prototype . changeState = require ( './changeState.js' ) ;
127
+ Granim . prototype . triggerError = require ( './triggerError.js' ) ;
128
+ Granim . prototype . validateInput = require ( './validateInput.js' ) ;
157
129
158
130
module . exports = Granim ;
159
131
@@ -217,20 +189,21 @@ module.exports = function(timestamp) {
217
189
this . animation = requestAnimationFrame ( this . animateColors . bind ( this ) ) ;
218
190
219
191
// Callback and Event
220
- if ( this . callbacks . onGradientChange ) this . callbacks . onGradientChange ( {
221
- isLooping : isLooping ,
222
- colorsFrom : this . states [ this . activeState ] . gradients [ this . channelsIndex ] ,
223
- colorsTo : nextGradient ,
224
- activeState : this . activeState
225
- } ) ;
226
-
227
- this . canvas . dispatchEvent ( this . events . gradientChange ( {
192
+ if ( this . callbacks . onGradientChange ) {
193
+ this . callbacks . onGradientChange ( {
228
194
isLooping : isLooping ,
229
195
colorsFrom : this . states [ this . activeState ] . gradients [ this . channelsIndex ] ,
230
196
colorsTo : nextGradient ,
231
197
activeState : this . activeState
232
- } )
233
- ) ;
198
+ } ) ;
199
+ }
200
+
201
+ this . canvas . dispatchEvent ( this . events . gradientChange ( {
202
+ isLooping : isLooping ,
203
+ colorsFrom : this . states [ this . activeState ] . gradients [ this . channelsIndex ] ,
204
+ colorsTo : nextGradient ,
205
+ activeState : this . activeState
206
+ } ) ) ;
234
207
235
208
// Else if it was the last gradient on the list and the loop mode is off
236
209
} else {
@@ -322,7 +295,7 @@ module.exports = function() {
322
295
} ;
323
296
324
297
} , { } ] , 7 :[ function ( require , module , exports ) {
325
- 'use strict'
298
+ 'use strict' ;
326
299
327
300
var regex = {
328
301
hexa : / ^ # (?: [ 0 - 9 a - f A - F ] { 3 } ) { 1 , 2 } $ / ,
@@ -382,7 +355,7 @@ function identifyColorType(color) {
382
355
if ( match ) return colorTypes [ i ] ;
383
356
}
384
357
return false ;
385
- } ;
358
+ }
386
359
387
360
function hexToRgba ( hex ) {
388
361
// Expand shorthand form (e.g. '03F') to full form (e.g. '0033FF')
@@ -397,7 +370,7 @@ function hexToRgba(hex) {
397
370
parseInt ( result [ 3 ] , 16 ) ,
398
371
1
399
372
] : null ;
400
- } ;
373
+ }
401
374
402
375
function hue2rgb ( p , q , t ) {
403
376
if ( t < 0 ) t += 1 ;
@@ -406,7 +379,7 @@ function hue2rgb(p, q, t) {
406
379
if ( t < 1 / 2 ) return q ;
407
380
if ( t < 2 / 3 ) return p + ( q - p ) * ( 2 / 3 - t ) * 6 ;
408
381
return p ;
409
- } ;
382
+ }
410
383
411
384
function hslaToRgb ( h , s , l , a ) {
412
385
var r , g , b , q , p ;
@@ -420,7 +393,7 @@ function hslaToRgb(h, s, l, a) {
420
393
b = hue2rgb ( p , q , h - 1 / 3 ) ;
421
394
}
422
395
return [ Math . round ( r * 255 ) , Math . round ( g * 255 ) , Math . round ( b * 255 ) , a ] ;
423
- } ;
396
+ }
424
397
425
398
} , { } ] , 8 :[ function ( require , module , exports ) {
426
399
'use strict' ;
@@ -435,7 +408,7 @@ module.exports = function() {
435
408
'use strict' ;
436
409
437
410
module . exports = function ( ) {
438
- if ( typeof window . CustomEvent === " function" ) return ;
411
+ if ( typeof window . CustomEvent === ' function' ) return ;
439
412
440
413
function CustomEvent ( event , params ) {
441
414
params = params || { bubbles : false , cancelable : false , detail : undefined } ;
@@ -468,11 +441,11 @@ module.exports = function(gradientColor) {
468
441
'use strict' ;
469
442
470
443
module . exports = function ( colorA , colorB ) {
471
- var i ;
444
+ var i = 0 ;
472
445
var colorDiff = [ ] ;
473
446
474
- for ( i = 0 ; i < 4 ; i ++ ) {
475
- colorDiff . push ( colorB [ i ] - colorA [ i ] )
447
+ for ( i ; i < 4 ; i ++ ) {
448
+ colorDiff . push ( colorB [ i ] - colorA [ i ] ) ;
476
449
}
477
450
478
451
return colorDiff ;
@@ -487,9 +460,7 @@ module.exports = function(gradientColor, i) {
487
460
488
461
} else {
489
462
// Ensure first and last position to be 0 and 100
490
- return parseFloat (
491
- ! i ? 0 : ( ( 1 / ( this . states [ this . activeState ] . gradients [ 0 ] . length - 1 ) ) * i
492
- ) . toFixed ( 2 ) ) ;
463
+ return parseFloat ( ! i ? 0 : ( ( 1 / ( this . gradientLength - 1 ) ) * i ) . toFixed ( 2 ) ) ;
493
464
}
494
465
} ;
495
466
@@ -511,7 +482,7 @@ module.exports = function() {
511
482
currentColors . push ( [ ] ) ;
512
483
513
484
for ( j = 0 ; j < 4 ; j ++ ) {
514
- currentColors [ i ] . push ( this . currentColors [ i ] [ j ] )
485
+ currentColors [ i ] . push ( this . currentColors [ i ] [ j ] ) ;
515
486
}
516
487
}
517
488
@@ -548,7 +519,7 @@ module.exports = function(element) {
548
519
if ( element instanceof HTMLCanvasElement ) {
549
520
this . canvas = element ;
550
521
551
- } else if ( typeof element === " string" ) {
522
+ } else if ( typeof element === ' string' ) {
552
523
this . canvas = document . querySelector ( element ) ;
553
524
554
525
} else {
@@ -593,9 +564,9 @@ module.exports = function() {
593
564
'use strict' ;
594
565
595
566
module . exports = function ( ) {
596
- var gradient = this . setDirection ( ) ,
597
- elToSetClassOnClass = document . querySelector ( this . elToSetClassOn ) . classList ,
598
- i = 0 ;
567
+ var gradient = this . setDirection ( ) ;
568
+ var elToSetClassOnClass = document . querySelector ( this . elToSetClassOn ) . classList ;
569
+ var i = 0 ;
599
570
this . context . clearRect ( 0 , 0 , this . x1 , this . y1 ) ;
600
571
601
572
if ( this . image ) {
@@ -682,7 +653,7 @@ module.exports = function() {
682
653
683
654
if ( _this . isCanvasInWindowView ) {
684
655
if ( ! _this . isPaused || _this . firstScrollInit ) {
685
- if ( _this . image && ! _this . isImgLoaded ) { return }
656
+ if ( _this . image && ! _this . isImgLoaded ) { return ; }
686
657
_this . isPausedBecauseNotInView = false ;
687
658
_this . play ( 'isPlayedBecauseInView' ) ;
688
659
_this . firstScrollInit = false ;
@@ -765,9 +736,9 @@ module.exports = function() {
765
736
var imageAxisPosition ;
766
737
switch ( imageAlignIndex ) {
767
738
case 'center' :
768
- imageAxisPosition = imgOriginalWidthOrHeight > canvasWidthOrHeight ?
769
- - ( imgOriginalWidthOrHeight - canvasWidthOrHeight ) / 2 :
770
- ( canvasWidthOrHeight - imgOriginalWidthOrHeight ) / 2 ;
739
+ imageAxisPosition = imgOriginalWidthOrHeight > canvasWidthOrHeight
740
+ ? - ( imgOriginalWidthOrHeight - canvasWidthOrHeight ) / 2
741
+ : ( canvasWidthOrHeight - imgOriginalWidthOrHeight ) / 2 ;
771
742
_this . imagePosition [ axis ] = imageAxisPosition ;
772
743
_this . imagePosition [ axis === 'x' ? 'width' : 'height' ] = imgOriginalWidthOrHeight ;
773
744
break ;
@@ -848,7 +819,7 @@ module.exports = function(progressPercent) {
848
819
// Generate gradient color position
849
820
activeChannelPos = parseFloat ( ( _this . activeColorsPos [ i ] +
850
821
( _this . activeColorsPosDiff [ i ] / 100 * progressPercent )
851
- ) . toFixed ( 4 ) )
822
+ ) . toFixed ( 4 ) ) ;
852
823
853
824
if ( activeChannelPos <= 1 && activeChannelPos >= 0 ) {
854
825
_this . currentColorsPos [ i ] = activeChannelPos ;
@@ -877,7 +848,7 @@ module.exports = function() {
877
848
}
878
849
879
850
// Set blank properties
880
- this . channels [ this . activeState ] . push ( [ { } ] ) ;
851
+ this . channels [ this . activeState ] . push ( [ { } ] ) ;
881
852
this . channels [ this . activeState ] [ this . channelsIndex ] . colors = [ ] ;
882
853
this . channels [ this . activeState ] [ this . channelsIndex ] . colorsDiff = [ ] ;
883
854
this . channels [ this . activeState ] [ this . channelsIndex ] . colorsPos = [ ] ;
@@ -953,6 +924,7 @@ module.exports = function() {
953
924
954
925
case 'radial' :
955
926
return ctx . createRadialGradient ( this . x1 / 2 , this . y1 / 2 , this . x1 / 2 , this . x1 / 2 , this . y1 / 2 , 0 ) ;
927
+
956
928
case 'custom' :
957
929
return ctx . createLinearGradient (
958
930
getCustomCoordinateInPixels ( this . customDirection . x0 , this . x1 ) ,
@@ -967,7 +939,7 @@ function getCustomCoordinateInPixels(coordinate, size) {
967
939
return coordinate . indexOf ( '%' ) > - 1
968
940
? size / 100 * parseInt ( coordinate . split ( '%' ) [ 0 ] , 10 )
969
941
: parseInt ( coordinate . split ( 'px' ) [ 0 ] , 10 ) ;
970
- } ;
942
+ }
971
943
972
944
} , { } ] , 29 :[ function ( require , module , exports ) {
973
945
'use strict' ;
@@ -1000,27 +972,29 @@ module.exports = function(inputType) {
1000
972
'difference' , 'exclusion' , 'hue' , 'saturation' , 'color' , 'luminosity' ] ;
1001
973
var directionValues = [ 'diagonal' , 'left-right' , 'top-bottom' , 'radial' , 'custom' ] ;
1002
974
1003
- switch ( inputType ) {
975
+ switch ( inputType ) {
1004
976
case 'image' :
1005
977
// Validate image.position
1006
978
if ( ( ! Array . isArray ( this . image . position ) || this . image . position . length !== 2 ) ||
1007
979
xPositionValues . indexOf ( this . image . position [ 0 ] ) === - 1 ||
1008
980
yPositionValues . indexOf ( this . image . position [ 1 ] ) === - 1
1009
- ) { this . triggerError ( 'image.position' ) }
981
+ ) { this . triggerError ( 'image.position' ) ; }
1010
982
// Validate image.stretchMode
1011
983
if ( this . image . stretchMode ) {
1012
984
if ( ( ! Array . isArray ( this . image . stretchMode ) || this . image . stretchMode . length !== 2 ) ||
1013
985
stretchModeValues . indexOf ( this . image . stretchMode [ 0 ] ) === - 1 ||
1014
986
stretchModeValues . indexOf ( this . image . stretchMode [ 1 ] ) === - 1
1015
- ) { this . triggerError ( 'image.stretchMode' ) }
987
+ ) { this . triggerError ( 'image.stretchMode' ) ; }
1016
988
}
1017
989
break ;
990
+
1018
991
case 'blendingMode' :
1019
992
if ( blendingModeValues . indexOf ( this . image . blendingMode ) === - 1 ) {
1020
993
this . clear ( ) ;
1021
994
this . triggerError ( 'blendingMode' ) ;
1022
995
}
1023
996
break ;
997
+
1024
998
case 'direction' :
1025
999
if ( directionValues . indexOf ( this . direction ) === - 1 ) {
1026
1000
this . triggerError ( 'direction' ) ;
@@ -1071,7 +1045,7 @@ function areDefinedInPixelsOrPercentage(array) {
1071
1045
i ++ ;
1072
1046
}
1073
1047
return definedInPixelsOrPercentage ;
1074
- } ;
1048
+ }
1075
1049
1076
1050
} , { } ] , 32 :[ function ( require , module , exports ) {
1077
1051
window . Granim = require ( './lib/Granim.js' ) ;
0 commit comments