@@ -638,7 +638,7 @@ cc.LayerColor = cc.Layer.extend(/** @lends cc.LayerColor# */{
638638
639639 var winSize = cc . Director . getInstance ( ) . getWinSize ( ) ;
640640
641- color = color || new cc . Color4B ( 0 , 0 , 0 , 255 ) ;
641+ color = color || new cc . Color4B ( 0 , 0 , 0 , 255 ) ;
642642 width = width || winSize . width ;
643643 height = height || winSize . height ;
644644
@@ -736,6 +736,9 @@ cc.LayerColor = cc.Layer.extend(/** @lends cc.LayerColor# */{
736736 context . fillStyle = "rgba(" + ( 0 | this . _color . r ) + "," + ( 0 | this . _color . g ) + "," + ( 0 | this . _color . b ) + "," + this . getOpacity ( ) / 255 + ")" ;
737737 context . fillRect ( - apip . x , apip . y , tWidth , - tHeight ) ;
738738
739+ if ( this . _rotation != 0 )
740+ context . rotate ( this . _rotationRadians ) ;
741+
739742 cc . INCREMENT_GL_DRAWS ( 1 ) ;
740743 } ,
741744
@@ -817,75 +820,22 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
817820 _alongVector :null ,
818821 _compressedInterpolation :false ,
819822
820- _drawGradientCanvas :null ,
821- _sourceGradientCanvas :null ,
822-
823- _buildGradientCanvas :function ( layerWidth , layerHeight ) {
824- //http://ricardocabello.com/blog/post/710
825-
826- layerWidth = layerWidth || this . getContentSize ( ) . width ;
827- layerHeight = layerHeight || this . getContentSize ( ) . height ;
828-
829- if ( ! this . _sourceGradientCanvas )
830- this . _sourceGradientCanvas = document . createElement ( 'canvas' ) ;
831- this . _sourceGradientCanvas . width = 2 ;
832- this . _sourceGradientCanvas . height = 2 ;
833-
834- var context_colors = this . _sourceGradientCanvas . getContext ( '2d' ) ;
835- context_colors . fillStyle = 'rgba(0,0,0,1)' ;
836- context_colors . fillRect ( 0 , 0 , 2 , 2 ) ;
837-
838- var image_colors = context_colors . getImageData ( 0 , 0 , 2 , 2 ) ;
839- var data = image_colors . data ;
840-
841- if ( ! this . _drawGradientCanvas )
842- this . _drawGradientCanvas = document . createElement ( 'canvas' ) ;
843- this . _drawGradientCanvas . width = layerWidth ;
844- this . _drawGradientCanvas . height = layerHeight ;
845-
846- var context_render = this . _drawGradientCanvas . getContext ( '2d' ) ;
847- context_render . translate ( - layerWidth / 2 , - layerHeight / 2 ) ;
848- context_render . scale ( layerWidth , layerHeight ) ;
849-
850- // Top-left,
851- data [ 0 ] = 0 | ( this . _squareColors [ 2 ] . r * 255 ) ;
852- data [ 1 ] = 0 | ( this . _squareColors [ 2 ] . g * 255 ) ;
853- data [ 2 ] = 0 | ( this . _squareColors [ 2 ] . b * 255 ) ;
854- data [ 3 ] = 0 | ( this . _squareColors [ 2 ] . a * 255 ) ;
855-
856- // Top-right,
857- data [ 4 ] = 0 | ( this . _squareColors [ 3 ] . r * 255 ) ;
858- data [ 5 ] = 0 | ( this . _squareColors [ 3 ] . g * 255 ) ;
859- data [ 6 ] = 0 | ( this . _squareColors [ 3 ] . b * 255 ) ;
860- data [ 7 ] = 0 | ( this . _squareColors [ 3 ] . a * 255 ) ;
861-
862- // Bottom-left,
863- data [ 8 ] = 0 | ( this . _squareColors [ 0 ] . r * 255 ) ;
864- data [ 9 ] = 0 | ( this . _squareColors [ 0 ] . g * 255 ) ;
865- data [ 10 ] = 0 | ( this . _squareColors [ 0 ] . b * 255 ) ;
866- data [ 11 ] = 0 | ( this . _squareColors [ 0 ] . a * 255 ) ;
867-
868- // Bottom-right,
869- data [ 12 ] = 0 | ( this . _squareColors [ 1 ] . r * 255 ) ;
870- data [ 13 ] = 0 | ( this . _squareColors [ 1 ] . g * 255 ) ;
871- data [ 14 ] = 0 | ( this . _squareColors [ 1 ] . b * 255 ) ;
872- data [ 15 ] = 0 | ( this . _squareColors [ 1 ] . a * 255 ) ;
873-
874- context_colors . putImageData ( image_colors , 0 , 0 ) ;
875- context_render . drawImage ( this . _sourceGradientCanvas , 0 , 0 ) ;
876- } ,
823+ _gradientStartPoint :null ,
824+ _gradientEndPoint :null ,
877825
878826 /**
879827 * Constructor
880828 * @function
881829 */
882830 ctor :function ( ) {
831+ this . _super ( ) ;
832+
883833 this . _startColor = new cc . Color3B ( 0 , 0 , 0 ) ;
884834 this . _endColor = new cc . Color3B ( 0 , 0 , 0 ) ;
885835 this . _alongVector = cc . p ( 0 , - 1 ) ;
886- this . _super ( ) ;
887836
888- this . _buildGradientCanvas ( ) ;
837+ this . _gradientStartPoint = cc . p ( 0 , 0 ) ;
838+ this . _gradientEndPoint = cc . p ( 0 , 0 ) ;
889839 } ,
890840
891841 /**
@@ -1004,7 +954,7 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
1004954
1005955 var argnum = arguments . length ;
1006956
1007- if ( argnum == 0 )
957+ if ( argnum == 0 )
1008958 return this . _super ( ) ;
1009959
1010960 if ( argnum == 2 ) {
@@ -1028,55 +978,63 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
1028978 this . _compressedInterpolation = true ;
1029979
1030980 this . _super ( cc . c4b ( start . r , start . g , start . b , 255 ) ) ;
1031- this . _buildGradientCanvas ( ) ;
1032981 return true ;
1033982 } ,
1034983
1035984 _updateColor :function ( ) {
1036- //todo need fixed for webGL
1037- this . _super ( ) ;
1038-
1039- var h = cc . pLength ( this . _alongVector ) ;
1040- if ( h == 0 )
1041- return ;
1042-
1043- var c = Math . sqrt ( 2.0 ) ;
1044- var u = cc . p ( this . _alongVector . x / h , this . _alongVector . y / h ) ;
1045-
1046- // Compressed Interpolation mode
1047- if ( this . _compressedInterpolation ) {
1048- var h2 = 1 / ( Math . abs ( u . x ) + Math . abs ( u . y ) ) ;
1049- u = cc . pMult ( u , h2 * c ) ;
1050- }
1051-
1052- var opacityf = this . _opacity / 255.0 ;
985+ if ( cc . renderContextType === cc . CANVAS ) {
986+ var tWidth = this . getContentSize ( ) . width / 2 ;
987+ var tHeight = this . getContentSize ( ) . height / 2 ;
988+ var apip = this . getAnchorPointInPoints ( ) ;
989+ var offWidth = tWidth - apip . x ;
990+ var offHeight = tHeight - apip . y ;
991+
992+ this . _gradientStartPoint = cc . p ( tWidth * - this . _alongVector . x + offWidth , tHeight * this . _alongVector . y - offHeight ) ;
993+ this . _gradientEndPoint = cc . p ( tWidth * this . _alongVector . x + offWidth , tHeight * - this . _alongVector . y - offHeight ) ;
994+ } else {
995+ //todo need fixed for webGL
996+ this . _super ( ) ;
1053997
1054- var S = new cc . Color4F ( this . _startColor . r / 255 , this . _startColor . g / 255 , this . _startColor . b / 255 , ( this . _startOpacity * opacityf ) / 255 ) ;
998+ var h = cc . pLength ( this . _alongVector ) ;
999+ if ( h == 0 )
1000+ return ;
10551001
1056- var E = new cc . Color4F ( this . _endColor . r / 255 , this . _endColor . g / 255 , this . _endColor . b / 255 , ( this . _endOpacity * opacityf ) / 255 ) ;
1002+ var c = Math . sqrt ( 2.0 ) ;
1003+ var u = cc . p ( this . _alongVector . x / h , this . _alongVector . y / h ) ;
10571004
1058- // (-1, -1)
1059- this . _squareColors [ 0 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1060- this . _squareColors [ 0 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1061- this . _squareColors [ 0 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1062- this . _squareColors [ 0 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1063- // (1, -1)
1064- this . _squareColors [ 1 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1065- this . _squareColors [ 1 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1066- this . _squareColors [ 1 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1067- this . _squareColors [ 1 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1068- // (-1, 1)
1069- this . _squareColors [ 2 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1070- this . _squareColors [ 2 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1071- this . _squareColors [ 2 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1072- this . _squareColors [ 2 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1073- // (1, 1)
1074- this . _squareColors [ 3 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1075- this . _squareColors [ 3 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1076- this . _squareColors [ 3 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1077- this . _squareColors [ 3 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1005+ // Compressed Interpolation mode
1006+ if ( this . _compressedInterpolation ) {
1007+ var h2 = 1 / ( Math . abs ( u . x ) + Math . abs ( u . y ) ) ;
1008+ u = cc . pMult ( u , h2 * c ) ;
1009+ }
10781010
1079- this . _buildGradientCanvas ( ) ;
1011+ var opacityf = this . _opacity / 255.0 ;
1012+
1013+ var S = new cc . Color4F ( this . _startColor . r / 255 , this . _startColor . g / 255 , this . _startColor . b / 255 , ( this . _startOpacity * opacityf ) / 255 ) ;
1014+
1015+ var E = new cc . Color4F ( this . _endColor . r / 255 , this . _endColor . g / 255 , this . _endColor . b / 255 , ( this . _endOpacity * opacityf ) / 255 ) ;
1016+
1017+ // (-1, -1)
1018+ this . _squareColors [ 0 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1019+ this . _squareColors [ 0 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1020+ this . _squareColors [ 0 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1021+ this . _squareColors [ 0 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c + u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1022+ // (1, -1)
1023+ this . _squareColors [ 1 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1024+ this . _squareColors [ 1 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1025+ this . _squareColors [ 1 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1026+ this . _squareColors [ 1 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c - u . x + u . y ) / ( 2.0 * c ) ) ) ) ;
1027+ // (-1, 1)
1028+ this . _squareColors [ 2 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1029+ this . _squareColors [ 2 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1030+ this . _squareColors [ 2 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1031+ this . _squareColors [ 2 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c + u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1032+ // (1, 1)
1033+ this . _squareColors [ 3 ] . r = ( ( E . r + ( S . r - E . r ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1034+ this . _squareColors [ 3 ] . g = ( ( E . g + ( S . g - E . g ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1035+ this . _squareColors [ 3 ] . b = ( ( E . b + ( S . b - E . b ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1036+ this . _squareColors [ 3 ] . a = ( ( E . a + ( S . a - E . a ) * ( ( c - u . x - u . y ) / ( 2.0 * c ) ) ) ) ;
1037+ }
10801038 } ,
10811039
10821040 draw :function ( ctx ) {
@@ -1085,31 +1043,22 @@ cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
10851043 if ( this . _isLighterMode )
10861044 context . globalCompositeOperation = 'lighter' ;
10871045
1088- if ( this . _drawGradientCanvas == null ) {
1089- var tWidth = this . getContentSize ( ) . width ;
1090- var tHeight = this . getContentSize ( ) . height ;
1091- var apip = this . getAnchorPointInPoints ( ) ;
1092- var tGradient = context . createLinearGradient ( - apip . x , apip . y ,
1093- - apip . x + tWidth , - ( apip . y + tHeight ) ) ;
1094-
1095- tGradient . addColorStop ( 0 , "rgba(" + Math . round ( this . _startColor . r ) + "," + Math . round ( this . _startColor . g ) + ","
1096- + Math . round ( this . _startColor . b ) + "," + Math . round ( this . _startOpacity / 255 ) . toFixed ( 4 ) + ")" ) ;
1097- tGradient . addColorStop ( 1 , "rgba(" + Math . round ( this . _endColor . r ) + "," + Math . round ( this . _endColor . g ) + ","
1098- + Math . round ( this . _endColor . b ) + "," + Math . round ( this . _endOpacity / 255 ) . toFixed ( 4 ) + ")" ) ;
1099-
1100- context . fillStyle = tGradient ;
1101- context . fillRect ( - apip . x , apip . y , tWidth , - tHeight ) ;
1102- } else {
1103- context . globalAlpha = this . _opacity / 255 ;
1104- var posX = 0 | ( - this . _anchorPointInPoints . x ) ;
1105- var posY = 0 | ( - this . _anchorPointInPoints . y ) ;
1106-
1107- context . drawImage ( this . _drawGradientCanvas ,
1108- 0 , 0 ,
1109- this . _drawGradientCanvas . width , this . _drawGradientCanvas . height ,
1110- posX , - ( posY + this . _contentSize . height ) ,
1111- this . _contentSize . width , this . _contentSize . height ) ;
1112- }
1046+ var tWidth = this . getContentSize ( ) . width ;
1047+ var tHeight = this . getContentSize ( ) . height ;
1048+ var apip = this . getAnchorPointInPoints ( ) ;
1049+ var tGradient = context . createLinearGradient ( this . _gradientStartPoint . x , this . _gradientStartPoint . y ,
1050+ this . _gradientEndPoint . x , this . _gradientEndPoint . y ) ;
1051+
1052+ tGradient . addColorStop ( 0 , "rgba(" + Math . round ( this . _startColor . r ) + "," + Math . round ( this . _startColor . g ) + ","
1053+ + Math . round ( this . _startColor . b ) + "," + ( this . _startOpacity / 255 ) . toFixed ( 4 ) + ")" ) ;
1054+ tGradient . addColorStop ( 1 , "rgba(" + Math . round ( this . _endColor . r ) + "," + Math . round ( this . _endColor . g ) + ","
1055+ + Math . round ( this . _endColor . b ) + "," + ( this . _endOpacity / 255 ) . toFixed ( 4 ) + ")" ) ;
1056+
1057+ context . fillStyle = tGradient ;
1058+ context . fillRect ( - apip . x , apip . y , tWidth , - tHeight ) ;
1059+
1060+ if ( this . _rotation != 0 )
1061+ context . rotate ( this . _rotationRadians ) ;
11131062 }
11141063 }
11151064} ) ;
0 commit comments