@@ -72,6 +72,13 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
7272 _cacheCanvas :null ,
7373 _cacheContext :null ,
7474 _cacheTexture :null ,
75+ // Sub caches for avoid Chrome big image draw issue
76+ _subCacheCanvas :[ ] ,
77+ _subCacheContext :[ ] ,
78+ _subCacheCount :0 ,
79+ _subCacheWidth :0 ,
80+ // Maximum pixel number by cache, a little more than 3072*3072, real limit is 4096*4096
81+ _maxCachePixel :10000000 ,
7582
7683 /**
7784 * Constructor
@@ -113,6 +120,28 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
113120 var locContentSize = this . _cacheTexture . _contentSize ;
114121 locContentSize . width = locCanvas . width ;
115122 locContentSize . height = locCanvas . height ;
123+
124+ // Init sub caches if needed
125+ var totalPixel = locCanvas . width * locCanvas . height ;
126+ if ( totalPixel > this . _maxCachePixel ) {
127+ this . _subCacheCount = Math . ceil ( totalPixel / this . _maxCachePixel ) ;
128+ for ( var i = 0 ; i < this . _subCacheCount ; i ++ ) {
129+ if ( ! this . _subCacheCanvas [ i ] ) {
130+ this . _subCacheCanvas [ i ] = document . createElement ( 'canvas' ) ;
131+ this . _subCacheContext [ i ] = this . _subCacheCanvas [ i ] . getContext ( '2d' ) ;
132+ }
133+ var tmpCanvas = this . _subCacheCanvas [ i ] ;
134+ tmpCanvas . width = this . _subCacheWidth = Math . round ( locCanvas . width / this . _subCacheCount ) ;
135+ tmpCanvas . height = locCanvas . height ;
136+ }
137+ // Clear wasted cache to release memory
138+ for ( var i = this . _subCacheCount ; i < this . _subCacheCanvas . length ; i ++ ) {
139+ tmpCanvas . width = 0 ;
140+ tmpCanvas . height = 0 ;
141+ }
142+ }
143+ // Otherwise use count as a flag to disable sub caches
144+ else this . _subCacheCount = 0 ;
116145 }
117146 } ,
118147
@@ -160,6 +189,13 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
160189 }
161190 }
162191 locCacheContext . restore ( ) ;
192+ // Update sub caches if needed
193+ if ( this . _subCacheCount > 0 ) {
194+ var subCacheW = this . _subCacheWidth , subCacheH = locCacheCanvas . height ;
195+ for ( var i = 0 ; i < this . _subCacheCount ; i ++ ) {
196+ this . _subCacheContext [ i ] . drawImage ( locCacheCanvas , i * subCacheW , 0 , subCacheW , subCacheH , 0 , 0 , subCacheW , subCacheH ) ;
197+ }
198+ }
163199 this . _cacheDirty = false ;
164200 }
165201 // draw RenderTexture
@@ -179,8 +215,14 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
179215 var posX = 0 | ( - this . _anchorPointInPoints . x ) , posY = 0 | ( - this . _anchorPointInPoints . y ) ;
180216 var locCacheCanvas = this . _cacheCanvas ;
181217 //direct draw image by canvas drawImage
182- if ( locCacheCanvas )
183- context . drawImage ( locCacheCanvas , posX , - ( posY + locCacheCanvas . height ) ) ;
218+ if ( locCacheCanvas ) {
219+ if ( this . _subCacheCount > 0 ) {
220+ for ( var i = 0 ; i < this . _subCacheCount ; i ++ ) {
221+ context . drawImage ( this . _subCacheCanvas [ i ] , posX + i * this . _subCacheWidth , - ( posY + locCacheCanvas . height ) ) ;
222+ }
223+ }
224+ else context . drawImage ( locCacheCanvas , posX , - ( posY + locCacheCanvas . height ) ) ;
225+ }
184226 } ,
185227
186228 /**
0 commit comments