Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. (function() {
  2.  
  3. // Dimensions of the whole book
  4. var BOOK_WIDTH = 1391;
  5. var BOOK_HEIGHT = 950;
  6.  
  7. // Dimensions of one page in the book
  8. var PAGE_WIDTH = 651;
  9. var PAGE_HEIGHT = 890;
  10.  
  11. // Vertical spacing between the top edge of the book and the papers
  12. var PAGE_Y = ( BOOK_HEIGHT - PAGE_HEIGHT ) / 2;
  13.  
  14. // The canvas size equals to the book dimensions + this padding
  15. var CANVAS_PADDING = 20;
  16.  
  17. var page = 0;
  18.  
  19. var canvas = document.getElementById( "pageflip-canvas" );
  20. var context = canvas.getContext( "2d" );
  21.  
  22. var mouse = { x: 0, y: 0 };
  23.  
  24. var flips = [];
  25.  
  26. var book = document.getElementById( "book" );
  27.  
  28. // List of all the page elements in the DOM
  29. var pages = book.getElementsByTagName( "section" );
  30.  
  31. // Organize the depth of our pages and create the flip definitions
  32. for( var i = 0, len = pages.length; i < len; i++ ) {
  33. pages[i].style.zIndex = len - i;
  34.  
  35. flips.push( {
  36. // Current progress of the flip (left -1 to right +1)
  37. progress: 1,
  38. // The target value towards which progress is always moving
  39. target: 1,
  40. // The page DOM element related to this flip
  41. page: pages[i],
  42. // True while the page is being dragged
  43. dragging: false
  44. } );
  45. }
  46.  
  47. // Resize the canvas to match the book size
  48. canvas.width = BOOK_WIDTH + ( CANVAS_PADDING * 2 );
  49. canvas.height = BOOK_HEIGHT + ( CANVAS_PADDING * 2 );
  50.  
  51. // Offset the canvas so that it's padding is evenly spread around the book
  52. canvas.style.top = -CANVAS_PADDING + "px";
  53. canvas.style.left = -CANVAS_PADDING + "px";
  54.  
  55. // Render the page flip 60 times a second
  56. setInterval( render, 1000 / 60 );
  57.  
  58. document.addEventListener( "mousemove", mouseMoveHandler, false );
  59. document.addEventListener( "mousedown", mouseDownHandler, false );
  60. document.addEventListener( "mouseup", mouseUpHandler, false );
  61.  
  62. function mouseMoveHandler( event ) {
  63. // Offset mouse position so that the top of the book spine is 0,0
  64. mouse.x = event.clientX - book.offsetLeft - ( BOOK_WIDTH / 2 );
  65. mouse.y = event.clientY - book.offsetTop;
  66. }
  67.  
  68. function mouseDownHandler( event ) {
  69. // Make sure the mouse pointer is inside of the book
  70. if (Math.abs(mouse.x) < PAGE_WIDTH) {
  71. if (mouse.x < 0 && page - 1 >= 0) {
  72. // We are on the left side, drag the previous page
  73. flips[page - 1].dragging = true;
  74. }
  75. else if (mouse.x > 0 && page + 1 < flips.length) {
  76. // We are on the right side, drag the current page
  77. flips[page].dragging = true;
  78. }
  79. }
  80.  
  81. // Prevents the text selection
  82. event.preventDefault();
  83. }
  84.  
  85. function mouseUpHandler( event ) {
  86. for( var i = 0; i < flips.length; i++ ) {
  87. // If this flip was being dragged, animate to its destination
  88. if( flips[i].dragging ) {
  89. // Figure out which page we should navigate to
  90. if( mouse.x < 0 ) {
  91. flips[i].target = -1;
  92. page = Math.min( page + 1, flips.length );
  93. }
  94. else {
  95. flips[i].target = 1;
  96. page = Math.max( page - 1, 0 );
  97. }
  98. }
  99.  
  100. flips[i].dragging = false;
  101. }
  102. }
  103.  
  104. function render() {
  105.  
  106. // Reset all pixels in the canvas
  107. context.clearRect( 0, 0, canvas.width, canvas.height );
  108.  
  109. for( var i = 0, len = flips.length; i < len; i++ ) {
  110. var flip = flips[i];
  111.  
  112. if( flip.dragging ) {
  113. flip.target = Math.max( Math.min( mouse.x / PAGE_WIDTH, 1 ), -1 );
  114. }
  115.  
  116. // Ease progress towards the target value
  117. flip.progress += ( flip.target - flip.progress ) * 0.2;
  118.  
  119. // If the flip is being dragged or is somewhere in the middle of the book, render it
  120. if( flip.dragging || Math.abs( flip.progress ) < 0.997 ) {
  121. drawFlip( flip );
  122. }
  123.  
  124. }
  125.  
  126. }
  127.  
  128. function drawFlip( flip ) {
  129. // Strength of the fold is strongest in the middle of the book
  130. var strength = 1 - Math.abs( flip.progress );
  131.  
  132. // Width of the folded paper
  133. var foldWidth = ( PAGE_WIDTH * 0.5 ) * ( 1 - flip.progress );
  134.  
  135. // X position of the folded paper
  136. var foldX = PAGE_WIDTH * flip.progress + foldWidth;
  137.  
  138. // How far the page should outdent vertically due to perspective
  139. var verticalOutdent = 20 * strength;
  140.  
  141. // The maximum width of the left and right side shadows
  142. var paperShadowWidth = ( PAGE_WIDTH * 0.5 ) * Math.max( Math.min( 1 - flip.progress, 0.5 ), 0 );
  143. var rightShadowWidth = ( PAGE_WIDTH * 0.5 ) * Math.max( Math.min( strength, 0.5 ), 0 );
  144. var leftShadowWidth = ( PAGE_WIDTH * 0.5 ) * Math.max( Math.min( strength, 0.5 ), 0 );
  145.  
  146.  
  147. // Change page element width to match the x position of the fold
  148. flip.page.style.width = Math.max(foldX, 0) + "px";
  149.  
  150. context.save();
  151. context.translate( CANVAS_PADDING + ( BOOK_WIDTH / 2 ), PAGE_Y + CANVAS_PADDING );
  152.  
  153.  
  154. // Draw a sharp shadow on the left side of the page
  155. context.strokeStyle = 'rgba(0,0,0,'+(0.05 * strength)+')';
  156. context.lineWidth = 30 * strength;
  157. context.beginPath();
  158. context.moveTo(foldX - foldWidth, -verticalOutdent * 0.5);
  159. context.lineTo(foldX - foldWidth, PAGE_HEIGHT + (verticalOutdent * 0.5));
  160. context.stroke();
  161.  
  162.  
  163. // Right side drop shadow
  164. var rightShadowGradient = context.createLinearGradient(foldX, 0, foldX + rightShadowWidth, 0);
  165. rightShadowGradient.addColorStop(0, 'rgba(0,0,0,'+(strength*0.2)+')');
  166. rightShadowGradient.addColorStop(0.8, 'rgba(0,0,0,0.0)');
  167.  
  168. context.fillStyle = rightShadowGradient;
  169. context.beginPath();
  170. context.moveTo(foldX, 0);
  171. context.lineTo(foldX + rightShadowWidth, 0);
  172. context.lineTo(foldX + rightShadowWidth, PAGE_HEIGHT);
  173. context.lineTo(foldX, PAGE_HEIGHT);
  174. context.fill();
  175.  
  176.  
  177. // Left side drop shadow
  178. var leftShadowGradient = context.createLinearGradient(foldX - foldWidth - leftShadowWidth, 0, foldX - foldWidth, 0);
  179. leftShadowGradient.addColorStop(0, 'rgba(0,0,0,0.0)');
  180. leftShadowGradient.addColorStop(1, 'rgba(0,0,0,'+(strength*0.15)+')');
  181.  
  182. context.fillStyle = leftShadowGradient;
  183. context.beginPath();
  184. context.moveTo(foldX - foldWidth - leftShadowWidth, 0);
  185. context.lineTo(foldX - foldWidth, 0);
  186. context.lineTo(foldX - foldWidth, PAGE_HEIGHT);
  187. context.lineTo(foldX - foldWidth - leftShadowWidth, PAGE_HEIGHT);
  188. context.fill();
  189.  
  190.  
  191. // Gradient applied to the folded paper (highlights & shadows)
  192. var foldGradient = context.createLinearGradient(foldX - paperShadowWidth, 0, foldX, 0);
  193. foldGradient.addColorStop(0.35, '#fafafa');
  194. foldGradient.addColorStop(0.73, '#eeeeee');
  195. foldGradient.addColorStop(0.9, '#fafafa');
  196. foldGradient.addColorStop(1.0, '#e2e2e2');
  197.  
  198. context.fillStyle = foldGradient;
  199. context.strokeStyle = 'rgba(0,0,0,0.06)';
  200. context.lineWidth = 0.5;
  201.  
  202. // Draw the folded piece of paper
  203. context.beginPath();
  204. context.moveTo(foldX, 0);
  205. context.lineTo(foldX, PAGE_HEIGHT);
  206. context.quadraticCurveTo(foldX, PAGE_HEIGHT + (verticalOutdent * 2), foldX - foldWidth, PAGE_HEIGHT + verticalOutdent);
  207. context.lineTo(foldX - foldWidth, -verticalOutdent);
  208. context.quadraticCurveTo(foldX, -verticalOutdent * 2, foldX, 0);
  209.  
  210. context.fill();
  211. context.stroke();
  212.  
  213.  
  214. context.restore();
  215. }
  216.  
  217. })();
Add Comment
Please, Sign In to add comment