Guest User

Untitled

a guest
May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Chapter 4 Example 7: Animating an image with rotation</title>
  6. <script src="modernizr-1.6.min.js"></script>
  7. <script type="text/javascript">
  8. window.addEventListener('load', eventWindowLoaded, false);
  9. function eventWindowLoaded() {
  10. canvasApp();
  11. }
  12.  
  13. function canvasSupport () {
  14. return Modernizr.canvas;
  15. }
  16.  
  17.  
  18. function canvasApp(){
  19. if (!canvasSupport()) {
  20. return;
  21. }else{
  22. var theCanvas = document.getElementById('canvas');
  23. var context = theCanvas.getContext('2d');
  24. }
  25.  
  26. var tileSheet=new Image();
  27. tileSheet.addEventListener('load', eventShipLoaded , false);
  28. tileSheet.src="tanks_sheet.png";
  29.  
  30. var tileMap = [
  31. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  32. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,25,29,1,1],
  33. [1,25,26,1,1,1,1,1,1,1,1,1,1,1,1,1,1,25,1,1],
  34. [1,1,1,1,1,1,1,1,28,1,1,1,1,1,1,1,1,1,1,1],
  35. [1,1,1,1,1,1,1,1,30,1,1,1,1,25,26,27,1,1,1,1],
  36. [1,1,1,1,1,1,25,25,25,1,1,1,1,1,1,1,1,1,1,1],
  37. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  38. [1,1,1,27,25,25,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  39. [1,1,1,1,1,1,1,1,1,25,1,1,1,1,1,1,1,1,29,1],
  40. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  41. [1,1,1,25,1,1,1,1,25,26,1,1,1,1,1,1,1,1,1,1],
  42. [1,1,1,25,1,1,1,1,1,1,1,1,1,1,25,26,25,26,1,1],
  43. [28,25,25,25,1,1,1,1,1,1,1,1,1,1,28,1,1,1,1,1],
  44. [30,1,1,1,1,1,1,1,1,1,1,1,1,1,30,1,1,1,1,1],
  45. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ];
  46. var mapRows = 15;
  47. var mapCols = 20;
  48. var mapIndexOffset = -1;
  49. var animationFrames=[1,2,3,4,5,6,7,8];
  50. var frameIndex=0;
  51. var rotation=90;
  52. var x=0;
  53. var y=0;
  54. var dx=0;
  55. var dy=0;
  56. var keyDown = false;
  57.  
  58. function eventShipLoaded() {
  59. startUp();
  60. }
  61.  
  62. function drawScreen() {
  63. context.fillStyle="#aaaaaa";
  64. context.fillRect(0,0,500,500);
  65.  
  66. for (var rowCtr=0;rowCtr<mapRows;rowCtr++) {
  67. for (var colCtr=0;colCtr<mapCols;colCtr++){
  68. var tileId = tileMap[rowCtr][colCtr]+mapIndexOffset;
  69. var sourceX = Math.floor(tileId % 8) *32;
  70. var sourceY = Math.floor(tileId / 8) *32;
  71. context.drawImage(tileSheet, sourceX, sourceY,32,32,colCtr*32,rowCtr*32,32,32);
  72. }
  73. }
  74.  
  75. context.save();
  76. context.setTransform(1,0,0,1,0,0)
  77. context.translate(x+16, y+16);
  78.  
  79. var angleInRadians =rotation * Math.PI / 180;
  80.  
  81. context.rotate(angleInRadians);
  82.  
  83. var sourceX=Math.floor(animationFrames[frameIndex] % 8) *32;
  84. var sourceY=Math.floor(animationFrames[frameIndex] / 8) *32;
  85.  
  86. y = y+dy;
  87. x = x+dx;
  88.  
  89.  
  90. context.drawImage(tileSheet, sourceX, sourceY,32,32,-16,-16,32,32);
  91.  
  92. dy = 0;
  93. dx = 0;
  94.  
  95. if(keyDown == true){
  96. frameIndex++;
  97. }
  98. if (frameIndex ==animationFrames.length) {
  99. frameIndex=0;
  100. }
  101. context.restore();
  102. keyDown = false;
  103. }
  104.  
  105. function startUp(){
  106. setInterval(drawScreen, 10 );
  107. }
  108.  
  109. document.onkeydown=function(e){
  110.  
  111. e=e?e:window.event;
  112. console.log(e.keyCode + "down");
  113.  
  114. switch (e.keyCode){
  115. case 38:
  116. //up
  117. if (rotation == 0){
  118. dy-=8; }
  119. keyDown = true;
  120. rotation = 0;
  121. break;
  122. case 40:
  123. //down
  124. if (rotation == 180){
  125. dy+=8; }
  126. keyDown = true;
  127. rotation = 180;
  128. break;
  129. case 37:
  130. //left
  131. if (rotation == 270){
  132. dx-=8; }
  133. keyDown = true;
  134. rotation = 270;
  135. break;
  136. case 39:
  137. //right
  138. if (rotation == 90){
  139. dx+=8; }
  140. keyDown = true;
  141. rotation = 90;
  142. }
  143. }
  144. }
  145. </script>
  146. </head>
  147. <body>
  148. <div style="position: absolute; top: 50px; left: 50px;">
  149. <canvas id="canvas" width="640" height="480">
  150. Your browser does not support the HTML 5 Canvas.
  151. </canvas>
  152. </div>
  153. </body>
  154. </html>
Add Comment
Please, Sign In to add comment