캠핑과 개발

HTML5의 canvas를 이용하여 객체를 생성한 후 해당 객체를 키보드 이벤트를 통하여 이동하는 예제입니다.

키보드의 방향키 ← → ↑↓를 이용하여 이동 할 수 이동합니다.



  1. <!DOCTYPE html>
  2. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  3. canvas {
  4.     border:1px solid #d3d3d3;
  5.     background-color: #f1f1f1;
  6. }
  7. </head>
  8. <body onload="startGame()">
  9.  
  10. var myGamePiece;
  11.  
  12. function startGame() {
  13.     myGamePiece = new component(30, 30, "red", 225, 225);
  14.     myGameArea.start();
  15. }
  16.  
  17. var myGameArea = {
  18.     canvas : document.createElement("canvas"),
  19.     start : function() {
  20.         this.canvas.width = 480;
  21.         this.canvas.height = 270;
  22.         this.context = this.canvas.getContext("2d");
  23.         document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  24.         this.frameNo = 0;
  25.         this.interval = setInterval(updateGameArea, 20);
  26.         window.addEventListener('keydown', function (e) {
  27.             e.preventDefault();
  28.             myGameArea.keys = (myGameArea.keys || []);
  29.             myGameArea.keys[e.keyCode] = (e.type == "keydown");
  30.         })
  31.         window.addEventListener('keyup', function (e) {
  32.             myGameArea.keys[e.keyCode] = (e.type == "keydown");
  33.         })
  34.     },
  35.     stop : function() {
  36.         clearInterval(this.interval);
  37.     },    
  38.     clear : function() {
  39.         this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  40.     }
  41. }
  42.  
  43. function component(width, height, color, x, y, type) {
  44.  
  45.     this.type = type;
  46.     this.width = width;
  47.     this.height = height;
  48.     this.speed = 0;
  49.     this.angle = 0;
  50.     this.moveAngle = 0;
  51.     this.x = x;
  52.     this.y = y;    
  53.     this.update = function() {
  54.         ctx = myGameArea.context;
  55.         ctx.save();
  56.         ctx.translate(this.x, this.y);
  57.         ctx.rotate(this.angle);
  58.         ctx.fillStyle = color;
  59.         ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
  60.         ctx.restore();    
  61.     }
  62.     this.newPos = function() {
  63.         this.angle += this.moveAngle * Math.PI / 180;
  64.         this.x += this.speed * Math.sin(this.angle);
  65.         this.y -= this.speed * Math.cos(this.angle);
  66.     }
  67. }
  68.  
  69. function updateGameArea() {
  70.     myGameArea.clear();
  71.     myGamePiece.moveAngle = 0;
  72.     myGamePiece.speed = 0;
  73.     if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.moveAngle = -1; }
  74.     if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.moveAngle = 1; }
  75.     if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speed= 1; }
  76.     if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speed= -1; }
  77.     myGamePiece.newPos();
  78.     myGamePiece.update();
  79. }
  80.  
  81. <p>Make sure the gamearea has focus, and use the arrow keys to move the red square around.</p>
  82.  
  83. </body>
  84. </html>
  85.  



참고 : 

http://www.w3schools.com/games/game_movement.asp

'DEVELOPMENT > HTML & CSS' 카테고리의 다른 글

[canvas] HTML5의 canvas 객체의 중력 예제  (0) 2016.06.01
[canvas] 2D를 이용한 객체 회전  (0) 2016.06.01
CSS framework  (0) 2014.01.17
[TIP] <IMG> 태그의 ALT에 줄바꿈 하기  (0) 2009.10.29
DOCTYPE.  (0) 2009.10.28