@@ -239,7 +239,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
239239 */
240240 setTargetDensityDPI : function ( densityDPI ) {
241241 this . _targetDensityDPI = densityDPI ;
242- this . _setViewPortMeta ( ) ;
242+ this . _adjustViewportMeta ( ) ;
243243 } ,
244244
245245 /**
@@ -300,41 +300,45 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
300300 this . setDesignResolutionSize ( designWidth , designHeight , this . _resolutionPolicy ) ;
301301 } ,
302302
303- _setViewPortMeta : function ( ) {
304- if ( this . _isAdjustViewPort ) {
305- var vp = document . getElementById ( "cocosMetaElement" ) ;
306- if ( vp ) {
307- document . head . removeChild ( vp ) ;
308- }
309-
310- var viewportMetas ,
311- elems = document . getElementsByName ( "viewport" ) ,
312- currentVP = elems ? elems [ 0 ] : null ,
313- content ;
303+ _setViewportMeta : function ( metas , overwrite ) {
304+ var vp = document . getElementById ( "cocosMetaElement" ) ;
305+ if ( vp ) {
306+ document . head . removeChild ( vp ) ;
307+ }
314308
315- vp = cc . newElement ( "meta" ) ;
316- vp . id = "cocosMetaElement" ;
317- vp . name = "viewport" ;
318- vp . content = "" ;
309+ var elems = document . getElementsByName ( "viewport" ) ,
310+ currentVP = elems ? elems [ 0 ] : null ,
311+ content , key , pattern ;
319312
320- viewportMetas = cc . __BrowserGetter . meta ;
313+ vp = cc . newElement ( "meta" ) ;
314+ vp . id = "cocosMetaElement" ;
315+ vp . name = "viewport" ;
316+ vp . content = "" ;
321317
322- content = currentVP ? currentVP . content : "" ;
323- for ( var key in viewportMetas ) {
324- var pattern = new RegExp ( key ) ;
325- if ( ! pattern . test ( content ) ) {
326- content += "," + key + "=" + viewportMetas [ key ] ;
327- }
318+ content = currentVP ? currentVP . content : "" ;
319+ for ( key in metas ) {
320+ if ( content . indexOf ( key ) == - 1 ) {
321+ content += "," + key + "=" + metas [ key ] ;
328322 }
329- if ( / ^ , / . test ( content ) )
330- content = content . substr ( 1 ) ;
323+ else if ( overwrite ) {
324+ pattern = new RegExp ( key + "\s*=\s*[^,]+" ) ;
325+ content . replace ( pattern , key + "=" + metas [ key ] ) ;
326+ }
327+ }
328+ if ( / ^ , / . test ( content ) )
329+ content = content . substr ( 1 ) ;
331330
332- vp . content = content ;
333- // For adopting certain android devices which don't support second viewport
334- if ( currentVP )
335- currentVP . content = content ;
331+ vp . content = content ;
332+ // For adopting certain android devices which don't support second viewport
333+ if ( currentVP )
334+ currentVP . content = content ;
335+
336+ document . head . appendChild ( vp ) ;
337+ } ,
336338
337- document . head . appendChild ( vp ) ;
339+ _adjustViewportMeta : function ( ) {
340+ if ( this . _isAdjustViewPort ) {
341+ this . _setViewportMeta ( cc . __BrowserGetter . meta , false ) ;
338342 }
339343 } ,
340344
@@ -585,7 +589,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
585589
586590 // Reinit frame size
587591 if ( cc . sys . isMobile )
588- this . _setViewPortMeta ( ) ;
592+ this . _adjustViewportMeta ( ) ;
589593
590594 this . _initFrameSize ( ) ;
591595
@@ -647,6 +651,34 @@ cc.EGLView = cc.Class.extend(/** @lends cc.view# */{
647651 return cc . size ( this . _designResolutionSize . width , this . _designResolutionSize . height ) ;
648652 } ,
649653
654+ /**
655+ * Sets the document body to desired pixel resolution and fit the game content to it.
656+ * This function is very useful for adaptation in mobile browsers.
657+ * In some HD android devices, the resolution is very high, but its browser performance may not be very good.
658+ * In this case, enabling retina display is very costy and not suggested, and if retina is disabled, the image may be blurry.
659+ * But this API can be helpful to set a desired pixel resolution which is in between.
660+ * This API will do the following:
661+ * 1. Set viewport's width to the desired width in pixel
662+ * 2. Set body width to the exact pixel resolution
663+ * 3. The resolution policy will be reset with designed view size in points.
664+ * @param {Number } width Design resolution width.
665+ * @param {Number } height Design resolution height.
666+ * @param {cc.ResolutionPolicy|Number } resolutionPolicy The resolution policy desired
667+ */
668+ setRealPixelResolution : function ( width , height , resolutionPolicy ) {
669+ // Set viewport's width
670+ this . _setViewportMeta ( { "width" : width , "user-scalable" : "no" } , true ) ;
671+
672+ // Set body width to the exact pixel resolution
673+ document . body . style . width = width + "px" ;
674+ document . body . style . height = "100%" ;
675+ document . body . style . left = "0px" ;
676+ document . body . style . top = "0px" ;
677+
678+ // Reset the resolution size and policy
679+ this . setDesignResolutionSize ( width , height , resolutionPolicy ) ;
680+ } ,
681+
650682 /**
651683 * Sets view port rectangle with points.
652684 * @param {Number } x
0 commit comments