PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/prototypes/golden_ratio.html

https://bitbucket.org/dotnetCarpenter/pragmatic
HTML | 203 lines | 170 code | 33 blank | 0 comment | 0 complexity | 024f46b44f749ef7a4e9fa3ea5b4cb5b MD5 | raw file
  1. <html>
  2. <head>
  3. <title>Golden Ratio Fun</title>
  4. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
  5. <script type="text/javascript">
  6. function loadCanvas(){
  7. // get available screen size
  8. var availWidth = $(window).width() - 20;
  9. var availHeight = $(window).height() - 20;
  10. // instantiate golden ratio constant
  11. var PHI = (1+ Math.sqrt(5))/2;
  12. // resize canvas based on golden ratio
  13. if(availWidth/availHeight > PHI){
  14. availWidth = availHeight * PHI;
  15. } else if(availWidth/availHeight < PHI){
  16. availHeight = availWidth/PHI;
  17. }
  18. // add canvas HTML tag to body
  19. var canvasTag = '<canvas id="canvas" width="'+availWidth+'" height="'+availHeight+'"></canvas>';
  20. $('body').html(canvasTag);
  21. // prepare to draw
  22. var canvas = $('canvas').get(0);
  23. if(canvas.getContext){
  24. var ctx = canvas.getContext('2d');
  25. }
  26. // draw first inner golden rectangle
  27. var x1 = availWidth/PHI;
  28. ctx.beginPath();
  29. ctx.moveTo(x1, 0);
  30. ctx.lineTo(x1, availHeight);
  31. ctx.stroke();
  32. // draw arc
  33. ctx.beginPath();
  34. ctx.moveTo(x1, availHeight);
  35. ctx.arc(x1, availHeight, x1, Math.PI, (3/2*Math.PI));
  36. ctx.stroke();
  37. // draw second inner rectangle
  38. var y1 = availHeight/PHI;
  39. ctx.beginPath();
  40. ctx.moveTo(x1, y1);
  41. ctx.lineTo(availWidth, y1);
  42. ctx.stroke();
  43. // draw arc
  44. ctx.beginPath();
  45. ctx.moveTo(x1, y1);
  46. ctx.arc(x1, y1, y1, (3/2*Math.PI), 0);
  47. ctx.stroke();
  48. // draw 3rd
  49. var x2 = availWidth - (availWidth-x1)/PHI;
  50. ctx.beginPath();
  51. ctx.moveTo(x2, y1);
  52. ctx.lineTo(x2, availHeight);
  53. ctx.stroke();
  54. // draw arc
  55. ctx.beginPath();
  56. ctx.moveTo(availWidth, y1);
  57. ctx.arc(x2, y1, (availHeight-y1), 0, (1/2*Math.PI));
  58. ctx.stroke();
  59. // draw 4th
  60. var y2 = availHeight - (availHeight-y1)/PHI;
  61. ctx.beginPath();
  62. ctx.moveTo(x1, y2);
  63. ctx.lineTo(x2, y2);
  64. ctx.stroke();
  65. // draw arc
  66. ctx.beginPath();
  67. ctx.moveTo(x2, y2);
  68. ctx.arc(x2, y2, (availHeight-y2), (1/2*Math.PI), Math.PI);
  69. ctx.stroke();
  70. // draw 5th
  71. var x3 = x1 + (x2-x1)/PHI;
  72. ctx.beginPath();
  73. ctx.moveTo(x3, y1);
  74. ctx.lineTo(x3, y2);
  75. ctx.stroke();
  76. // draw arc
  77. ctx.beginPath();
  78. ctx.moveTo(x3, y2);
  79. ctx.arc(x3, y2, (x3-x1), Math.PI, (3/2*Math.PI));
  80. ctx.stroke();
  81. // draw 6th
  82. var y3 = y1 + (y2-y1)/PHI;
  83. ctx.beginPath();
  84. ctx.moveTo(x3, y3);
  85. ctx.lineTo(x2, y3);
  86. ctx.stroke();
  87. // draw arc
  88. ctx.beginPath();
  89. ctx.moveTo(x3, y3);
  90. ctx.arc(x3, y3, (x2-x3), (3/2*Math.PI), 0);
  91. ctx.stroke();
  92. // draw 7th
  93. var x4 = x2 - (x2-x3)/PHI;
  94. ctx.beginPath();
  95. ctx.moveTo(x4, y3);
  96. ctx.lineTo(x4, y2);
  97. ctx.stroke();
  98. // draw arc
  99. ctx.beginPath();
  100. ctx.moveTo(x4, y3);
  101. ctx.arc(x4, y3, (x2-x4), 0, (1/2*Math.PI));
  102. ctx.stroke();
  103. // draw 8th
  104. var y4 = y2 - (y2-y3)/PHI;
  105. ctx.beginPath();
  106. ctx.moveTo(x3, y4);
  107. ctx.lineTo(x4, y4);
  108. ctx.stroke();
  109. // draw arc
  110. ctx.beginPath();
  111. ctx.moveTo(x4, y4);
  112. ctx.arc(x4, y4, (x4-x3), (1/2*Math.PI), Math.PI);
  113. ctx.stroke();
  114. // draw 9th
  115. var x5 = x3 + (x4-x3)/PHI;
  116. ctx.beginPath();
  117. ctx.moveTo(x5, y3);
  118. ctx.lineTo(x5, y4);
  119. ctx.stroke();
  120. // draw arc
  121. ctx.beginPath();
  122. ctx.moveTo(x5, y4);
  123. ctx.arc(x5, y4, (x5-x3), Math.PI, (3/2*Math.PI));
  124. ctx.stroke();
  125. // draw 10th
  126. var y5 = y3 + (y4-y3)/PHI;
  127. ctx.beginPath();
  128. ctx.moveTo(x4, y5);
  129. ctx.lineTo(x5, y5);
  130. ctx.stroke();
  131. // draw arc
  132. ctx.beginPath();
  133. ctx.moveTo(x5, y5);
  134. ctx.arc(x5, y5, (x4-x5), (3/2*Math.PI), 0);
  135. ctx.stroke();
  136. // draw 11th
  137. var x6 = x4 - (x4-x5)/PHI;
  138. ctx.beginPath();
  139. ctx.moveTo(x6, y4);
  140. ctx.lineTo(x6, y5);
  141. ctx.stroke();
  142. // draw arc
  143. ctx.beginPath();
  144. ctx.moveTo(x6, y5);
  145. ctx.arc(x6, y5, (x4-x6), 0, (1/2*Math.PI));
  146. ctx.stroke();
  147. // draw 12th
  148. var y6 = y4 - (y4-y5)/PHI;
  149. ctx.beginPath();
  150. ctx.moveTo(x5, y6);
  151. ctx.lineTo(x6, y6);
  152. ctx.stroke();
  153. // draw arc
  154. ctx.beginPath();
  155. ctx.moveTo(x6, y6);
  156. ctx.arc(x6, y6, (x6-x5), (1/2*Math.PI), Math.PI);
  157. ctx.stroke();
  158. }
  159. // redraws golden rectangles if window is resized
  160. $(window).resize(function() {
  161. loadCanvas();
  162. });
  163. </script>
  164. <style type="text/css">
  165. canvas { border: 2px solid black; }
  166. </style>
  167. </head>
  168. <body onload="loadCanvas()">
  169. </body>
  170. </html>