miniFramework.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. * create a webgl context
  24. * @param {HTMLCanvasElement} canvas
  25. * @param {Object} opt_attribs
  26. * @return {WebGLRenderingContext}
  27. */
  28. cc.create3DContext = function (canvas, opt_attribs) {
  29. var names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
  30. var context = null;
  31. for (var ii = 0; ii < names.length; ++ii) {
  32. try {
  33. context = canvas.getContext(names[ii], opt_attribs);
  34. } catch (e) {
  35. }
  36. if (context) {
  37. break;
  38. }
  39. }
  40. return context;
  41. };
  42. /**
  43. * Browser detection, based on mootools<br/>
  44. * platform will print out win32, mac, etc<br/>
  45. * type is the browser type, chrome, firefox etc
  46. * @type {Object}
  47. */
  48. cc.Browser = {};
  49. /**
  50. * Browsers that Cocos2d-HTML5 support well in WebGL render mode
  51. * @type {Array}
  52. */
  53. cc.Browser.webglWhiteList = ["baidubrowser", "opera", "firefox", "chrome", "safari"];
  54. /**
  55. * Browsers that multiple audio support well
  56. * @type {Array}
  57. */
  58. cc.Browser.multipleAudioWhiteList = ["baidubrowser", "opera", "firefox", "chrome", "safari", "ucbrowser", "qqbrowser", "mqqbrowser", "ie"];
  59. (function () {
  60. var ua = navigator.userAgent;
  61. cc.Browser.ua = ua.toLowerCase();
  62. cc.Browser.platform = navigator.platform.toLowerCase();
  63. cc.Browser.isMobile = (cc.Browser.ua.indexOf('mobile') != -1 || cc.Browser.ua.indexOf('android') != -1);
  64. cc.Browser.type = (function () {
  65. var browserTypes = cc.Browser.ua.match(/micromessenger|qqbrowser|mqqbrowser|ucbrowser|360browser|baiduboxapp|baidubrowser|maxthon|ie|opera|miuibrowser|firefox/)
  66. || cc.Browser.ua.match(/chrome|safari/);
  67. if (browserTypes && browserTypes.length > 0) {
  68. var el = browserTypes[0];
  69. if (el == 'micromessenger') {
  70. return 'wechat';
  71. }else if( el === "safari" && (cc.Browser.ua.match(/android.*applewebkit/) != null))
  72. return "androidbrowser";
  73. return el;
  74. }
  75. return "unknow";
  76. })();
  77. cc.Browser.mode = cc.Browser.type == 'ie' && document.documentMode;
  78. if (!document["ccConfig"])
  79. document["ccConfig"] = {};
  80. var c = document["ccConfig"];
  81. // check supportWebGL item
  82. cc._userRenderMode = parseInt(c["renderMode"]) || 0;
  83. var notInWhiteList = cc.Browser.webglWhiteList.indexOf(cc.Browser.type) == -1;
  84. if (cc._userRenderMode === 1 || (cc._userRenderMode === 0 && (cc.Browser.isMobile || notInWhiteList))) {
  85. //canvas only
  86. cc.Browser.supportWebGL = false;
  87. } else {
  88. // WebGL first
  89. cc.Browser.supportWebGL = !(window.WebGLRenderingContext == null);
  90. var tempCanvas = document.createElement("Canvas");
  91. var tempContext = cc.create3DContext(tempCanvas, {'stencil': true, 'preserveDrawingBuffer': true });
  92. cc.Browser.supportWebGL = !(tempContext == null)
  93. }
  94. if (cc._userRenderMode === 2 && !cc.Browser.supportWebGL) {
  95. // WebGL render only, but browser doesn't support WebGL.
  96. cc.__renderDoesnotSupport = true;
  97. }
  98. // check if browser supports Web Audio
  99. cc.Browser.supportWebAudio = (function () {
  100. // check Web Audio's context
  101. try {
  102. var ctx = new (window.AudioContext || window.webkitAudioContext || window.mozAudioContext)();
  103. return ctx ? true : false;
  104. } catch (e) {
  105. return false;
  106. }
  107. })();
  108. cc.Browser.openURL = function (url) {
  109. if (this.isMobile) {
  110. var size = cc.Director.getInstance().getWinSize();
  111. var w = size.width + "px";
  112. var h = size.height + "px";
  113. var div = cc.$new("div");
  114. div.style.backgroundColor = "#ffffff";
  115. div.style.width = w;
  116. div.style.height = h;
  117. div.style.zindex = 1000;
  118. div.style.position = 'absolute';
  119. div.style.top = 0 + 'px';
  120. div.style.left = 0 + 'px';
  121. div.id = "cocos2d-browser";
  122. var iframe = cc.$new("iframe");
  123. iframe.src = url;
  124. iframe.style.width = w;
  125. iframe.style.height = h;
  126. iframe.setAttribute("frameborder", "no");
  127. iframe.setAttribute("scrolling", "no");
  128. div.appendChild(iframe);
  129. iframe.onload = function () {
  130. var close = document.createElement('img');
  131. close.src = "data:image/png;base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2dk5rqmpeUrE6+vr06ADzpEIlI5kMjFwPhh5PB5DJBKdK7KDg4Oj2tpaVUdHxw/0eWxszIjyj8Jvy4N60FdVVX2Grnt4dkaowYJESAG3yaLR09Oz5uvrexwbGxuAR2erpKTkI6RqxW5DM6RnLT09PQQV5vDwsDYlJWUU+I4EIDMhEQLAA6q0DA4OrqMCg/c/qL6+XtXY2Kgn4sGJuavRaFbFYrFPeXn5FIj6ReFa64KnIpJOpaMK39vbM9XV1X13lF9kc3Nz+xMTEwZo89s03A4ycRE1N/RjF/WPKgyfDRU39Gu7w1qYyNYAtwDB1yhgGPDBfgzU4bMi7xoEjAI6iWZRdGMGH80Cr2goRlP5W8B7qwBHfw1YO6kEH4yC8EnJ5QKbnuDFh17nr4BPRP9P/BFgAHo7ZNgI9EbHAAAAAElFTkSuQmCC";
  132. div.appendChild(close);
  133. close.style.zindex = 1000;
  134. close.style.position = 'absolute';
  135. close.style.bottom = 10 + 'px';
  136. close.style.right = 10 + 'px';
  137. close.onclick = function () {
  138. div.remove();
  139. }
  140. };
  141. var tag = document['ccConfig'].tag;
  142. var parent = document.getElementById(tag).parentNode;
  143. if (parent) {
  144. parent.appendChild(div);
  145. }
  146. }
  147. else {
  148. window.open(url);
  149. }
  150. }
  151. })();
  152. cc.RenderDoesnotSupport = function () {
  153. if (cc.__renderDoesnotSupport === "undefined")
  154. return false;
  155. return cc.__renderDoesnotSupport;
  156. };
  157. /**
  158. * the dollar sign, classic like jquery, this selector add extra methods to HTMLElement without touching its prototype</br>
  159. * it is also chainable like jquery
  160. * @param {HTMLElement|String} x pass in a css selector in string or the whole HTMLElement
  161. * @class
  162. * @return {cc.$}
  163. */
  164. cc.$ = function (x) {
  165. /** @lends cc.$# */
  166. var parent = (this == cc) ? document : this;
  167. /**
  168. * @type {HTMLElement}
  169. */
  170. var el = (x instanceof HTMLElement) ? x : parent.querySelector(x);
  171. if (el) {
  172. /**
  173. * find and return the child wth css selector (same as jquery.find)
  174. * @param {HTMLElement|String} x pass in a css selector in string or the whole HTMLElement
  175. * @return {cc.$}
  176. */
  177. el.find = el.find || cc.$;
  178. /**
  179. * check if a DOMNode has a specific class
  180. * @param {String} cls
  181. * @return {Boolean}
  182. */
  183. el.hasClass = el.hasClass || function (cls) {
  184. return this.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
  185. };
  186. /**
  187. * add a class to a DOMNode, returns self to allow chaining
  188. * @param {String} cls
  189. * @return {cc.$}
  190. */
  191. el.addClass = el.addClass || function (cls) {
  192. if (!this.hasClass(cls)) {
  193. if (this.className) {
  194. this.className += " ";
  195. }
  196. this.className += cls;
  197. }
  198. return this;
  199. };
  200. /**
  201. * remove a specific class from a DOMNode, returns self to allow chaining
  202. * @param {String} cls
  203. * @return {cc.$}
  204. */
  205. el.removeClass = el.removeClass || function (cls) {
  206. if (this.hasClass(cls)) {
  207. this.className = this.className.replace(cls, '');
  208. }
  209. return this;
  210. };
  211. /**
  212. * detach it self from parent
  213. * @function
  214. */
  215. el.remove = el.remove || function () {
  216. if (this.parentNode)
  217. this.parentNode.removeChild(this);
  218. return this;
  219. };
  220. /**
  221. * add to another element as a child
  222. * @param {HTMLElement|cc.$} x
  223. * @return {cc.$}
  224. */
  225. el.appendTo = el.appendTo || function (x) {
  226. x.appendChild(this);
  227. return this;
  228. };
  229. /**
  230. * add to another element as a child and place on the top of the children list
  231. * @param {HTMLElement|cc.$} x
  232. * @return {cc.$}
  233. */
  234. el.prependTo = el.prependTo || function (x) {
  235. ( x.childNodes[0]) ? x.insertBefore(this, x.childNodes[0]) : x.appendChild(this);
  236. return this;
  237. };
  238. /**
  239. * helper function for updating the css transform
  240. * @return {cc.$}
  241. */
  242. el.transforms = el.transforms || function () {
  243. this.style[cc.$.trans] = cc.$.translate(this.position) + cc.$.rotate(this.rotation) + cc.$.scale(this.scale) + cc.$.skew(this.skew);
  244. return this;
  245. };
  246. el.position = el.position || {x: 0, y: 0};
  247. el.rotation = el.rotation || 0;
  248. el.scale = el.scale || {x: 1, y: 1};
  249. el.skew = el.skew || {x: 0, y: 0};
  250. /**
  251. * move the element
  252. * @param {Number} x in pixel
  253. * @param {Number} y in pixel
  254. * @return {cc.$}
  255. */
  256. el.translates = function (x, y) {
  257. this.position.x = x;
  258. this.position.y = y;
  259. this.transforms();
  260. return this
  261. };
  262. /**
  263. * rotate the element
  264. * @param {Number} x in degrees
  265. * @return {cc.$}
  266. */
  267. el.rotate = function (x) {
  268. this.rotation = x;
  269. this.transforms();
  270. return this
  271. };
  272. /**
  273. * resize the element
  274. * @param {Number} x
  275. * @param {Number} y
  276. * @return {cc.$}
  277. */
  278. el.resize = function (x, y) {
  279. this.scale.x = x;
  280. this.scale.y = y;
  281. this.transforms();
  282. return this
  283. };
  284. /**
  285. * skews the element
  286. * @param {Number} x in degrees
  287. * @param {Number} y
  288. * @return {cc.$}
  289. */
  290. el.setSkew = function (x, y) {
  291. this.skew.x = x;
  292. this.skew.y = y;
  293. this.transforms();
  294. return this
  295. };
  296. }
  297. return el;
  298. };
  299. //getting the prefix and css3 3d support
  300. switch (cc.Browser.type) {
  301. case "firefox":
  302. cc.$.pfx = "Moz";
  303. cc.$.hd = true;
  304. break;
  305. case "chrome":
  306. case "safari":
  307. cc.$.pfx = "webkit";
  308. cc.$.hd = true;
  309. break;
  310. case "opera":
  311. cc.$.pfx = "O";
  312. cc.$.hd = false;
  313. break;
  314. case "ie":
  315. cc.$.pfx = "ms";
  316. cc.$.hd = false;
  317. break;
  318. default:
  319. cc.$.pfx = "webkit";
  320. cc.$.hd = true;
  321. }
  322. //cache for prefixed transform
  323. cc.$.trans = cc.$.pfx + "Transform";
  324. //helper function for constructing transform strings
  325. cc.$.translate = (cc.$.hd) ? function (a) {
  326. return "translate3d(" + a.x + "px, " + a.y + "px, 0) "
  327. } : function (a) {
  328. return "translate(" + a.x + "px, " + a.y + "px) "
  329. };
  330. cc.$.rotate = (cc.$.hd) ? function (a) {
  331. return "rotateZ(" + a + "deg) ";
  332. } : function (a) {
  333. return "rotate(" + a + "deg) ";
  334. };
  335. cc.$.scale = function (a) {
  336. return "scale(" + a.x + ", " + a.y + ") "
  337. };
  338. cc.$.skew = function (a) {
  339. return "skewX(" + -a.x + "deg) skewY(" + a.y + "deg)";
  340. };
  341. /**
  342. * Creates a new element, and adds cc.$ methods
  343. * @param {String} x name of the element tag to create
  344. * @return {cc.$}
  345. */
  346. cc.$new = function (x) {
  347. return cc.$(document.createElement(x))
  348. };
  349. cc.$.findpos = function (obj) {
  350. var curleft = 0;
  351. var curtop = 0;
  352. do {
  353. curleft += obj.offsetLeft;
  354. curtop += obj.offsetTop;
  355. } while (obj = obj.offsetParent);
  356. return {x: curleft, y: curtop};
  357. };