Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. </head>
  5.  
  6.  
  7. <body>
  8. <canvas id="canvas1" width="400" height="400"></canvas>
  9. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
  10. <script>
  11.     $(document).ready(function () {
  12.         var canvas = $("#canvas1")[0];
  13.         var context = canvas.getContext("2d");
  14.         var height = $("#canvas1").height();
  15.         var width = $("#canvas1").width();
  16.         var scale = 20;
  17.         var nx,ny;
  18.  
  19.  
  20.         function paint(x, y) {
  21.                 //фон
  22.                 context.fillStyle = "grey";
  23.                 context.fillRect(0, 0, width, height);
  24.  
  25.                 //квадрат
  26.                 context.fillStyle = "blue";
  27.                 context.fillRect(x * scale, y * scale, scale, scale);
  28.                 nx = x;
  29.                 ny = y;
  30.         }
  31.  
  32.         paint(1, 1);
  33.  
  34.         $(document).keydown(function (e){
  35.                 var key = e.which;
  36.                 if (key == "39") {
  37.                     nx++;
  38.                 } else if (key == "37") {
  39.                     nx--;
  40.                 }
  41.                 else if (key == "40") {
  42.                     ny++;
  43.                 }
  44.                 else if (key == "38") {
  45.                     ny--;
  46.                 }
  47.             if (Math.abs(nx) * scale < width && Math.abs(ny) * scale < height) {
  48.                 paint(nx, ny);
  49.             }
  50.         })
  51.     });
  52. </script>
  53. </body>
  54. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement