CCScreen.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * The fullscreen API provides an easy way for web content to be presented using the user's entire screen.
  24. * It's invalid on safari, QQbrowser and android browser
  25. * @class
  26. * @name cc.screen
  27. */
  28. cc.screen = /** @lends cc.screen# */{
  29. _supportsFullScreen: false,
  30. // the pre fullscreenchange function
  31. _preOnFullScreenChange: null,
  32. _touchEvent: "",
  33. _fn: null,
  34. // Function mapping for cross browser support
  35. _fnMap: [
  36. [
  37. 'requestFullscreen',
  38. 'exitFullscreen',
  39. 'fullscreenchange',
  40. 'fullscreenEnabled',
  41. 'fullscreenElement'
  42. ],
  43. [
  44. 'requestFullScreen',
  45. 'exitFullScreen',
  46. 'fullScreenchange',
  47. 'fullScreenEnabled',
  48. 'fullScreenElement'
  49. ],
  50. [
  51. 'webkitRequestFullScreen',
  52. 'webkitCancelFullScreen',
  53. 'webkitfullscreenchange',
  54. 'webkitIsFullScreen',
  55. 'webkitCurrentFullScreenElement'
  56. ],
  57. [
  58. 'mozRequestFullScreen',
  59. 'mozCancelFullScreen',
  60. 'mozfullscreenchange',
  61. 'mozFullScreen',
  62. 'mozFullScreenElement'
  63. ],
  64. [
  65. 'msRequestFullscreen',
  66. 'msExitFullscreen',
  67. 'MSFullscreenChange',
  68. 'msFullscreenEnabled',
  69. 'msFullscreenElement'
  70. ]
  71. ],
  72. /**
  73. * initialize
  74. * @function
  75. */
  76. init: function () {
  77. this._fn = {};
  78. var i, val, map = this._fnMap, valL;
  79. for (i = 0, l = map.length; i < l; i++ ) {
  80. val = map[ i ];
  81. if ( val && val[1] in document ) {
  82. for ( i = 0, valL = val.length; i < valL; i++ ) {
  83. this._fn[ map[0][ i ] ] = val[ i ];
  84. }
  85. break;
  86. }
  87. }
  88. this._supportsFullScreen = (this._fn.requestFullscreen != undefined);
  89. this._touchEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown';
  90. },
  91. /**
  92. * return true if it's full now.
  93. * @returns {Boolean}
  94. */
  95. fullScreen: function() {
  96. return this._supportsFullScreen && document[ this._fn.fullscreenEnabled ];
  97. },
  98. /**
  99. * change the screen to full mode.
  100. * @param {Element} element
  101. * @param {Function} onFullScreenChange
  102. */
  103. requestFullScreen: function (element, onFullScreenChange) {
  104. if (!this._supportsFullScreen) return;
  105. element = element || document.documentElement;
  106. element[ this._fn.requestFullscreen ]();
  107. if (onFullScreenChange) {
  108. var eventName = this._fn.fullscreenchange;
  109. if (this._preOnFullScreenChange)
  110. document.removeEventListener(eventName, this._preOnFullScreenChange);
  111. this._preOnFullScreenChange = onFullScreenChange;
  112. cc._addEventListener(document, eventName, onFullScreenChange, false);
  113. }
  114. return element[ this._fn.requestFullscreen ]();
  115. },
  116. /**
  117. * exit the full mode.
  118. * @return {Boolean}
  119. */
  120. exitFullScreen: function () {
  121. return this._supportsFullScreen ? document[ this._fn.exitFullscreen ]() : true;
  122. },
  123. /**
  124. * Automatically request full screen with a touch/click event
  125. * @param {Element} element
  126. * @param {Function} onFullScreenChange
  127. */
  128. autoFullScreen: function (element, onFullScreenChange) {
  129. element = element || document.body;
  130. var touchTarget = cc._canvas || element;
  131. var theScreen = this;
  132. // Function bind will be too complicated here because we need the callback function's reference to remove the listener
  133. function callback() {
  134. theScreen.requestFullScreen(element, onFullScreenChange);
  135. touchTarget.removeEventListener(theScreen._touchEvent, callback);
  136. }
  137. this.requestFullScreen(element, onFullScreenChange);
  138. cc._addEventListener(touchTarget, this._touchEvent, callback);
  139. }
  140. };
  141. cc.screen.init();