Advertisement
ProjcheskiF1

[ИП] Колоквиум 2, Задача 2 (2016/17)

Jan 23rd, 2017
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Canvas Test</title>
  6. </head>
  7. <body>
  8. <section>
  9.  
  10. <div>
  11. <canvas id="canvas" width="300" height="200">
  12. This text is displayed if your browser does not support HTML5 Canvas.
  13. </canvas>
  14. </div>
  15.  
  16. <script type="text/javascript">
  17. var canvas;
  18. var ctx;
  19. var dx = 5;
  20. var dy = 5;
  21. var x = 150;
  22. var y = 100;
  23. var WIDTH = 300;
  24. var HEIGHT = 200;
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. function circle(x,y,r) {
  33. ctx.beginPath();
  34. ctx.arc(x, y, r, 0, Math.PI*2, true);
  35. ctx.fill();
  36. }
  37.  
  38. function rect(x,y,w,h) {
  39. ctx.beginPath();
  40. ctx.rect(x,y,w,h);
  41. ctx.closePath();
  42. ctx.fill();
  43. ctx.stroke();
  44. }
  45.  
  46. function clear() {
  47. ctx.clearRect(0, 0, WIDTH, HEIGHT);
  48. }
  49.  
  50. function init() {
  51. canvas = document.getElementById("canvas");
  52. ctx = canvas.getContext("2d");
  53.  
  54.  
  55.  
  56. return setInterval(draw, 10);
  57.  
  58.  
  59.  
  60. }
  61.  
  62. function doKeyDown(evt){
  63. switch (evt.keyCode) {
  64. case 38: /* Up arrow was pressed */
  65. if (y - dy > 0){
  66. y -= dy;
  67. }
  68. break;
  69. case 40: /* Down arrow was pressed */
  70. if (y + dy < HEIGHT){
  71. y += dy;
  72. }
  73. break;
  74. case 37: /* Left arrow was pressed */
  75. if (x - dx > 0){
  76. x -= dx;
  77. }
  78. break;
  79. case 39: /* Right arrow was pressed */
  80. if (x + dx < WIDTH){
  81. x += dx;
  82. }
  83. break;
  84. }
  85. }
  86.  
  87. function draw() {
  88.  
  89. ctx.fillStyle = "white";
  90. ctx.strokeStyle = "black";
  91. rect(0,0,WIDTH,HEIGHT);
  92.  
  93. ctx.fillStyle="yellow";
  94. ctx.fillRect(0,0,20,20);
  95. ctx.fillStyle="red";
  96. ctx.fillRect(279,0,299,20);
  97. ctx.fillStyle="green";
  98. ctx.fillRect(0,179,20,199);
  99. ctx.fillStyle="blue";
  100. ctx.fillRect(279,179,299,199);
  101.  
  102.  
  103. ctx.fillStyle = "yellow";
  104. circle(x, y, 10);
  105.  
  106. if(((x-dx)>=0 && (x-dx)<=20) && ((y-dy)>=0 && (y-dy)<=20) && !((((x+dx)>=279 && (x-+x)<=299) && ((y-dy)>=0 && (y-dy)<=20))
  107. &&(((x-dx)>=0 && (x-dx)<=20) && ((y+dy)>=179 && (y+dy)<=199)) && (((x+dx)>=279 && (x+dx)<=299) && ((y+dy)>=179 && (y+dy)<=199)))){
  108. ctx.fillStyle="yellow";
  109. circle(x,y,10);
  110. }
  111. if(((x+dx)>=279 && (x-+x)<=299) && ((y-dy)>=0 && (y-dy)<=20)){
  112. ctx.fillStyle="red";
  113. circle(x,y,10);
  114. }
  115. if(((x-dx)>=0 && (x-dx)<=20) && ((y+dy)>=179 && (y+dy)<=199)){
  116. ctx.fillStyle="green";
  117. circle(x,y,10);
  118. }
  119. if(((x+dx)>=279 && (x+dx)<=299) && ((y+dy)>=179 && (y+dy)<=199)){
  120. ctx.fillStyle="blue";
  121. circle(x,y,10);
  122. }
  123.  
  124.  
  125. }
  126.  
  127.  
  128. init();
  129. window.addEventListener('keydown',doKeyDown,true);
  130.  
  131.  
  132. </script>
  133. </section>
  134. </body>
  135. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement