Advertisement
Guest User

responsive2

a guest
Dec 10th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Responsive Art</title>
  4. <script>
  5. var selector = 0;
  6. var canvas;
  7. var context;
  8. var triangles = [];
  9. var diamonds = [];
  10. var NinjaStars = [];
  11.  
  12. function init() {
  13. canvas = document.getElementById('canvas');
  14. context = canvas.getContext("2d");
  15. //resize canvas fit the window
  16. resizeCanvas();
  17. window.addEventListener('resize', resizeCanvas, false);
  18. window.addEventListener('orientationchange', resizeCanvas, false);
  19. //add a trangle where the user clicks
  20. canvas.onclick = function(event) {
  21. handleClick(event.clientX, event.clientY);
  22.  
  23. };
  24. this.addEventListener( "keydown", doKeyDown, true);
  25. //add a timer that redraws the canvas (function) --remember to move the triangles
  26. return setInterval(draw, 15);
  27. }
  28. function Diamond(x,y, color){
  29. this.x = x;
  30. this.y = y;
  31. this.color = color;
  32. this.vx = Math.random() * 20 - 5;
  33. this.vy = Math.random() * 20 - 5;
  34. }
  35. function NinjaStar(x,y,color){
  36. this.x = x;
  37. this.y = y;
  38. this.color = color;
  39. this.vx = Math.random() * 20 - 5;
  40. this.vy = Math.random() * 20 - 5;
  41. }
  42. function Triangle(x,y,color){
  43. this.x = x;
  44. this.y = y;
  45. this.color = color;
  46. //give them velocities
  47. this.vx = Math.random() * 20 - 5;
  48. this.vy = Math.random() * 20 - 5;
  49. }
  50. function doKeyDown( letter ){
  51. alert( letter.keyCode )
  52. //68 is d diamond
  53. if ( letter.keyCode == 68 ) {
  54. selector = 0;
  55. }
  56. //78 is n for ninjaStar
  57. if ( letter.keyCode == 78 ) {
  58. selector = 1;
  59. }
  60. //83 is s for star
  61. if ( letter.keyCode == 83 ) {
  62. selector = 2;
  63. }
  64. //76 is l for lightning bolt
  65. if ( letter.keyCode == 76 ) {
  66. selector = 3;
  67. }
  68. //84 is t for triangle
  69. if ( letter.keyCode == 84 ) {
  70. selector = 4;
  71. }
  72. }
  73. function handleClick(x,y) {
  74. var colors = ['orange','red','yellow',];
  75. var color = colors[Math.floor(Math.random()*colors.length)];
  76. if (selector == 0) {
  77. diamonds.push(new Diamond(x,y,color));
  78. for (var i=0; i<diamonds.length; i++) {
  79. drawDiamond(diamonds[i]);
  80. }
  81. }
  82.  
  83. if (selector == 1) {
  84. NinjaStars.push(new NinjaStar(x,y,color));
  85. for (var i=0; i< NinjaStars.length; i++) {
  86. drawNinjaStar(NinjaStars[i]);
  87. }
  88. }
  89.  
  90. triangles.push(new Triangle(x,y,color));
  91. for (var i=0; i<triangles.length; i++) {
  92. drawTriangle(triangles[i]);
  93. }
  94. }
  95. function drawDiamond(diamond) {
  96. context.beginPath();
  97.  
  98. //Store the diamonds CURRENT x and y position, into local variables
  99. var xPos = diamond.x;
  100. var yPos = diamond.y;
  101.  
  102. //Set the "start point" of our pen, to the current x and y position. (this is the LEFT POINT OF THE DIAMOND)
  103. context.moveTo(xPos,yPos);
  104. //From start position, draw > 20 pixels, and ^ 25 pixels (this is the TOP point of the diamond)
  105. context.lineTo(xPos+20,yPos+25);
  106. //From start position, draw > 20 pixels, and v 25 pixels (this is the BOTTOM point of the diamond)
  107. context.lineTo(xPos+20,yPos-25);
  108.  
  109. //Move our "pen" coordinates 40 pixels >
  110. xPos += 40;
  111.  
  112. //Set the "start point" of our pen, to the new x and y position. (this is the RIGHT POINT OF THE DIAMOND)
  113. context.moveTo(xPos, yPos);
  114. //From new position, draw < 20 pixels and ^ 25 pixels (this is the TOP point of the diamond)
  115. context.lineTo(xPos-20,yPos+25);
  116. //From new position, draw < 20 pixels and v 25 pixels (this is the BOTTOM point of the diamond)
  117. context.lineTo(xPos-20,yPos-25);
  118.  
  119.  
  120. context.fillStyle = diamond.color;
  121. context.fill();
  122. }
  123. function drawNinjaStar(Ninjastar) {
  124. context.beginPath();
  125.  
  126. //Store the diamonds CURRENT x and y position, into local variables
  127. var xPos = NinjaStar.x;
  128. var yPos = NinjaStar.y;
  129.  
  130. //Set the "start point" of our pen, to the current x and y position. (this is the LEFT POINT OF THE DIAMOND)
  131. context.moveTo(xPos,yPos);
  132. //From start position, draw > 20 pixels, and ^ 25 pixels (this is the TOP point of the diamond)
  133. context.lineTo(xPos+40,yPos+45);
  134. //From start position, draw > 20 pixels, and v 25 pixels (this is the BOTTOM point of the diamond)
  135.  
  136.  
  137. //Move our "pen" coordinates 40 pixels >
  138. xPos += 40;
  139.  
  140. //Set the "start point" of our pen, to the new x and y position. (this is the RIGHT POINT OF THE DIAMOND)
  141. context.moveTo(xPos, yPos);
  142. //From new position, draw < 20 pixels and ^ 25 pixels (this is the TOP point of the diamond)
  143. context.lineTo(xPos-40,yPos+45);
  144. //From new position, draw < 20 pixels and v 25 pixels (this is the BOTTOM point of the diamond)
  145. context.lineTo(xPos-40,yPos-45);
  146.  
  147.  
  148. context.fillStyle = NinjaStar.color;
  149. context.fill();
  150. }
  151. function drawTriangle(triangle) {
  152. context.beginPath();
  153. context.moveTo(triangle.x,triangle.y); //assumes x is 75, y is 50
  154. context.lineTo(triangle.x+25,triangle.y+25);
  155. context.lineTo(triangle.x+25,triangle.y-25);
  156. context.fillStyle = triangle.color;
  157. context.fill();
  158. }
  159.  
  160.  
  161.  
  162. function draw(){
  163. context.clearRect(0,0, canvas.width, canvas.height);
  164. context.fillStyle = "black";
  165. resizeCanvas();
  166.  
  167. for (var i=0; i<diamonds.length; i++) {
  168. drawDiamond(diamonds[i]);
  169. }
  170. for (var i=0; i<NinjaStars.length; i++) {
  171. drawNinjaStar(NinjaStars[i]);
  172. }
  173.  
  174. for (var i=0; i < triangles.length; i++) {
  175. var t = triangles[i];
  176. drawTriangle(t);
  177. //move the triangle
  178.  
  179. if (t.x + t.vx > canvas.width || t.x + t.vx < 0)
  180. t.vx = -t.vx
  181. if (t.y + t.vy > canvas.height || t.y + t.vy < 0)
  182. t.vy = -t.vy
  183.  
  184. t.x += t.vx;
  185. t.y += t.vy;
  186. }
  187. }
  188.  
  189. function resizeCanvas() {
  190. canvas.width = window.innerWidth-10;
  191. canvas.height = window.innerHeight-10;
  192. fillBackgroundColor();
  193. for (var i=0; i<triangles.length; i++) {
  194. drawTriangle(triangles[i]);
  195. }
  196. }
  197. function fillBackgroundColor() {
  198. //make the background black
  199. context.fillStyle = "black";
  200. context.fillRect(0,0,canvas.width,canvas.height);
  201. }
  202. window.onload = init;
  203. </script>
  204. </head>
  205. <body>
  206. <canvas id="canvas" width="500" height="500"></canvas>
  207. </body>
  208. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement