476179

matthewPt2

Jan 6th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="utf-8">
  6. <title>Blocks Mitchell Palermo</title>
  7. </head>
  8. <canvas id="canvas1" width="640" height="480" style="border: 1px solid black;position:absolute; left:0; top 0; z-index: 0,"></canvas>
  9.  
  10. <body>
  11.  
  12. <script>
  13. var xPos = 0;
  14. var yPos = 0;
  15. var speed = 80;
  16. var layer1 = document.getElementById("canvas1");
  17. var dt1 = layer1.getContext("2d");
  18.  
  19. document.addEventListener("click",runFunc1);
  20.  
  21. function runFunc1()
  22. {
  23. dt1.clearRect(0,0,canvas1.width,canvas1.height);
  24. xPos = 0;
  25. yPos = 0;
  26.  
  27. }
  28. function randomColour()
  29. {
  30. red = Math.floor(Math.random()*255)+1;
  31. green = Math.floor(Math.random()*255)+1;
  32. blue = Math.floor(Math.random()*255)+1;
  33.  
  34. return "rgb("+red+","+green+","+blue+")";
  35. }
  36. setInterval(draw, 100);
  37. function draw()
  38. {
  39. blocks();
  40. }
  41.  
  42. function blocks()
  43. {
  44.  
  45. dt1.fillStyle = randomColour();
  46. dt1.fillRect(xPos,yPos, 80, 80);
  47. dt1.fill();
  48.  
  49. xPos = xPos + speed;
  50.  
  51. if (xPos <0 || xPos >560)
  52. {
  53. yPos = yPos+80;
  54. speed = speed * -1;
  55.  
  56. }
  57.  
  58. }
  59.  
  60.  
  61.  
  62. </script>
  63.  
  64. </body>
  65. </html>
Add Comment
Please, Sign In to add comment