CCActionPageTurn3D.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * <p>
  24. * This action simulates a page turn from the bottom right hand corner of the screen. <br/>
  25. * It's not much use by itself but is used by the PageTurnTransition. <br/>
  26. * <br/>
  27. * Based on an original paper by L Hong et al. <br/>
  28. * http://www.parc.com/publication/1638/turning-pages-of-3d-electronic-books.html
  29. * </p>
  30. * @class
  31. * @extends cc.Grid3DAction
  32. */
  33. cc.PageTurn3D = cc.Grid3DAction.extend(/** @lends cc.PageTurn3D# */{
  34. /**
  35. * Update each tick <br/>
  36. * Time is the percentage of the way through the duration
  37. */
  38. update:function (time) {
  39. var tt = Math.max(0, time - 0.25);
  40. var deltaAy = (tt * tt * 500);
  41. var ay = -100 - deltaAy;
  42. var deltaTheta = -Math.PI / 2 * Math.sqrt(time);
  43. var theta = /*0.01f */ +Math.PI / 2 + deltaTheta;
  44. var sinTheta = Math.sin(theta);
  45. var cosTheta = Math.cos(theta);
  46. var locGridSize = this._gridSize;
  47. var locVer = cc.p(0, 0);
  48. for (var i = 0; i <= locGridSize.width; ++i) {
  49. for (var j = 0; j <= locGridSize.height; ++j) {
  50. locVer.x = i;
  51. locVer.y = j;
  52. // Get original vertex
  53. var p = this.originalVertex(locVer);
  54. var R = Math.sqrt((p.x * p.x) + ((p.y - ay) * (p.y - ay)));
  55. var r = R * sinTheta;
  56. var alpha = Math.asin(p.x / R);
  57. var beta = alpha / sinTheta;
  58. var cosBeta = Math.cos(beta);
  59. // If beta > PI then we've wrapped around the cone
  60. // Reduce the radius to stop these points interfering with others
  61. if (beta <= Math.PI)
  62. p.x = ( r * Math.sin(beta));
  63. else
  64. p.x = 0; //Force X = 0 to stop wrapped points
  65. p.y = ( R + ay - ( r * (1 - cosBeta) * sinTheta));
  66. // We scale z here to avoid the animation being
  67. // too much bigger than the screen due to perspectve transform
  68. p.z = (r * ( 1 - cosBeta ) * cosTheta) / 7;// "100" didn't work for
  69. // Stop z coord from dropping beneath underlying page in a transition
  70. // issue #751
  71. if (p.z < 0.5)
  72. p.z = 0.5;
  73. // Set new coords
  74. this.setVertex(locVer, p);
  75. }
  76. }
  77. }
  78. });
  79. /**
  80. * create PageTurn3D action
  81. * @function
  82. * @param {Number} duration
  83. * @param {cc.Size} gridSize
  84. * @return {cc.PageTurn3D}
  85. */
  86. cc.pageTurn3D = function (duration, gridSize) {
  87. return new cc.PageTurn3D(duration, gridSize);
  88. };
  89. /**
  90. * Please use cc.pageTurn3D instead
  91. * create PageTurn3D action
  92. * @param {Number} duration
  93. * @param {cc.Size} gridSize
  94. * @return {cc.PageTurn3D}
  95. * @static
  96. * @deprecated since v3.0 please use cc.pageTurn3D instead.
  97. */
  98. cc.PageTurn3D.create = cc.pageTurn3D;