PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/uuCanvas.js/demo.canvas/6_2_canvas_clipping+resize.htm

http://uupaa-js-spinoff.googlecode.com/
HTML | 238 lines | 210 code | 28 blank | 0 comment | 0 complexity | 5134a09ab69c548faced66764c31e866 MD5 | raw file
Possible License(s): Apache-2.0
  1. <!doctype html><html><head>
  2. <title>A canvas clip example</title>
  3. <meta name="DC.creator" content="Kamiel Martinet, http://www.martinet.nl/">
  4. <meta name="DC.publisher" content="Mozilla Developer Center, http://developer.mozilla.org">
  5. <script src="../uuCanvas.js"></script>
  6. <script>
  7. function xcanvas() {
  8. drawShape(document.getElementById('slcanvas'), 0, 1);
  9. drawShape(document.getElementById('vmlcanvas'), 0, 1);
  10. drawShape(document.getElementById('flashcanvas'), 0, 1);
  11. }
  12. function matrix() {
  13. drawShape(document.getElementById('slcanvas'), 1);
  14. drawShape(document.getElementById('vmlcanvas'), 1);
  15. drawShape(document.getElementById('flashcanvas'), 1);
  16. }
  17. function redraw() {
  18. drawShape(document.getElementById('slcanvas'), 0, 1);
  19. drawShape(document.getElementById('vmlcanvas'), 0, 1);
  20. drawShape(document.getElementById('flashcanvas'), 0, 1);
  21. }
  22. var globalAlpha = 1.0;
  23. function switchAlpha() {
  24. globalAlpha -= 0.2;
  25. drawShape(document.getElementById('slcanvas'));
  26. drawShape(document.getElementById('vmlcanvas'));
  27. drawShape(document.getElementById('flashcanvas'));
  28. }
  29. var lineWidth = 1.0;
  30. function switchLineWidth() {
  31. lineWidth += 2;
  32. drawShape(document.getElementById('slcanvas'));
  33. drawShape(document.getElementById('vmlcanvas'));
  34. drawShape(document.getElementById('flashcanvas'));
  35. }
  36. function drawShape(canvas, matrix, redraw){
  37. if (!canvas.getContext){ return; }
  38. var ctx = canvas.getContext('2d');
  39. ctx.clear();
  40. ctx.fillStyle = "black";
  41. ctx.fillRect(0,0,canvas.width,canvas.height);
  42. if (matrix) {
  43. ctx.translate(-10, 5);
  44. ctx.scale(1.2, 0.8);
  45. ctx.rotate(5 * Math.PI / 180);
  46. }
  47. ctx.globalAlpha = globalAlpha;
  48. ctx.lineWidth = lineWidth;
  49. if (redraw) {
  50. ctx.translate(canvas.width / 2, canvas.height / 2);
  51. }
  52. // Create a circular clipping path
  53. ctx.beginPath();
  54. ctx.arc(0,0,60,0,Math.PI*2,true);
  55. ctx.clip();
  56. // draw background
  57. var lingrad = ctx.createLinearGradient(0,-75,0,75);
  58. lingrad.addColorStop(0, '#232256');
  59. lingrad.addColorStop(1, '#143778');
  60. ctx.fillStyle = lingrad;
  61. ctx.fillRect(-75,-75,150,150);
  62. // draw stars
  63. for (var j=1;j<50;j++){
  64. ctx.save();
  65. ctx.fillStyle = '#fff';
  66. ctx.translate(75-Math.floor(Math.random()*150),75-Math.floor(Math.random()*150));
  67. drawStar(ctx,Math.floor(Math.random()*4)+2);
  68. ctx.restore();
  69. }
  70. }
  71. function drawStar(ctx,r){
  72. ctx.save();
  73. ctx.beginPath()
  74. ctx.moveTo(r,0);
  75. for (var i=0;i<9;i++){
  76. ctx.rotate(Math.PI/5);
  77. if(i%2 == 0) {
  78. ctx.lineTo((r/0.525731)*0.200811,0);
  79. } else {
  80. ctx.lineTo(r,0);
  81. }
  82. }
  83. ctx.closePath();
  84. ctx.fill();
  85. ctx.restore();
  86. }
  87. function reduce() {
  88. resize("slcanvas", -10, -10);
  89. resize("vmlcanvas", -10, -10);
  90. resize("flashcanvas", -10, -10);
  91. }
  92. function wide() {
  93. resize("slcanvas", 10, 10);
  94. resize("vmlcanvas", 10, 10);
  95. resize("flashcanvas", 10, 10);
  96. }
  97. function resize(id, width, height) {
  98. var node = document.getElementById(id),
  99. ctx = node.getContext("2d"),
  100. w = parseInt(node.width) + width,
  101. h = parseInt(node.height) + height;
  102. if (ctx.clear) {
  103. ctx.clear();
  104. } else {
  105. ctx.clearRect(0, 0, w, h);
  106. }
  107. node.width = w;
  108. node.height = h;
  109. if (ctx.resize) {
  110. ctx.resize(w, h);
  111. }
  112. }
  113. </script>
  114. <style type="text/css">
  115. body { margin: 20px; font-family: arial,verdana,helvetica; background: #fff;}
  116. h1 { font-size: 140%; font-weight:normal; color: #036; border-bottom: 1px solid #ccc; }
  117. canvas { border: 2px solid #000; float: left; margin-right: 20px; margin-bottom: 20px; }
  118. pre { float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
  119. </style>
  120. </head>
  121. <body>
  122. <h1>A canvas <code>clip</code> example</h1>
  123. <div>
  124. <input type="button" value="[+] wide" onclick="wide()" />
  125. <input type="button" value="[-] reduce" onclick="reduce()" />
  126. <input type="button" value="[R] redraw" onclick="redraw()" />
  127. <input type="button" value="matrix effect" onclick="matrix()" />
  128. <input type="button" value="alpha -0.2" onclick="switchAlpha()" />
  129. <input type="button" value="lineWidth +2" onclick="switchLineWidth()" />
  130. <br />
  131. <canvas id="slcanvas" class="sl" width="150" height="150"></canvas>
  132. <canvas id="vmlcanvas" class="vml" width="150" height="150"></canvas>
  133. <canvas id="flashcanvas" class="flash" width="150" height="150"></canvas>
  134. <pre>
  135. function xcanvas() {
  136. draw(document.getElementById('slcanvas'));
  137. draw(document.getElementById('vmlcanvas'));
  138. draw(document.getElementById('flashcanvas'));
  139. }
  140. function matrix() {
  141. draw(document.getElementById('slcanvas'), 1);
  142. draw(document.getElementById('vmlcanvas'), 1);
  143. draw(document.getElementById('flashcanvas'), 1);
  144. }
  145. var globalAlpha = 1.0;
  146. function switchAlpha() {
  147. globalAlpha -= 0.2;
  148. drawShape(document.getElementById('slcanvas'));
  149. drawShape(document.getElementById('vmlcanvas'));
  150. drawShape(document.getElementById('flashcanvas'));
  151. }
  152. var lineWidth = 1.0;
  153. function switchLineWidth() {
  154. lineWidth += 2;
  155. drawShape(document.getElementById('slcanvas'));
  156. drawShape(document.getElementById('vmlcanvas'));
  157. drawShape(document.getElementById('flashcanvas'));
  158. }
  159. function draw(canvas, matrix){
  160. if (!canvas.getContext){ return; }
  161. var ctx = canvas.getContext('2d');
  162. ctx.clear();
  163. if (matrix) {
  164. ctx.translate(-10, 5);
  165. ctx.scale(1.2, 0.8);
  166. ctx.rotate(5 * Math.PI / 180);
  167. }
  168. ctx.globalAlpha = globalAlpha;
  169. ctx.lineWidth = lineWidth;
  170. ctx.fillRect(0,0,150,150);
  171. ctx.translate(75,75);
  172. // Create a circular clipping path
  173. ctx.beginPath();
  174. ctx.arc(0,0,60,0,Math.PI*2,true);
  175. ctx.clip();
  176. // draw background
  177. var lingrad = ctx.createLinearGradient(0,-75,0,75);
  178. lingrad.addColorStop(0, '#232256');
  179. lingrad.addColorStop(1, '#143778');
  180. ctx.fillStyle = lingrad;
  181. ctx.fillRect(-75,-75,150,150);
  182. // draw stars
  183. for (var j=1;j<50;j++){
  184. ctx.save();
  185. ctx.fillStyle = '#fff';
  186. ctx.translate(75-Math.floor(Math.random()*150),75-Math.floor(Math.random()*150));
  187. drawStar(ctx,Math.floor(Math.random()*4)+2);
  188. ctx.restore();
  189. }
  190. }
  191. function drawStar(ctx,r){
  192. ctx.save();
  193. ctx.beginPath()
  194. ctx.moveTo(r,0);
  195. for (var i=0;i<9;i++){
  196. ctx.rotate(Math.PI/5);
  197. if(i%2 == 0) {
  198. ctx.lineTo((r/0.525731)*0.200811,0);
  199. } else {
  200. ctx.lineTo(r,0);
  201. }
  202. }
  203. ctx.closePath();
  204. ctx.fill();
  205. ctx.restore();
  206. }
  207. </pre>
  208. </div>
  209. </body>
  210. </html>