前提・実現したいこと
sub = getSubMap( 0, 0, MAP_WIDTH, MAP_HEIGHT );
この一つ目の引数を変更すると描画をずらすことができますが、引数の二つ目を変更しても上下にずらして描画することができません。
これはなぜでしょうか。
ご教授よろしくお願いいたします。
該当のソースコード
HTML
1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <link rel="stylesheet" href="main.css"> 6 <script src="../../jquery-3.5.1.min.js"></script> 7 </head> 8 <body> 9 <canvas id="canvas"></canvas> 10 <script type="text/javascript" src="main.js"></script> 11 </body> 12</html>
JavaScript
1var map = [ 2 [ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], 3 [ 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6], 4 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 5 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 6 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 7 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 8 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 9 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 11 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 12 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 13 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 14 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 15 [ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], 16 [ 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6], 17]; 18 19var CHIP_WIDTH = 32; 20var CHIP_HEIGHT = 32; 21var IMG_CACHE = {}; 22 23var MAP_WIDTH = 29; 24var MAP_HEIGHT= 14; 25 26const 27 X_MAX = 33, 28 Y_MAX = 14 29; 30 31function getSubMap( left, top, width=3, height=3 ) { 32 let rect = [], tmp; 33 if( top > Y_MAX - height ) { 34 top = Y_MAX - height; 35 } 36 if( left > X_MAX - width ) { 37 left = X_MAX - width; 38 } 39 40 for( let y = top; y < top+height; ++y ) { 41 for( let x = left; x < left+width; ++x ) { 42 if( x == left ) { rect.push(tmp=[]); } 43 tmp.push( map[y][x] ); 44 } 45 } 46 return rect; 47} 48 49function main() { 50 var canvas = document.getElementById('canvas'); 51 canvas.width = CHIP_WIDTH * MAP_WIDTH; 52 canvas.height = CHIP_HEIGHT * MAP_HEIGHT; 53 54 var ctx = canvas.getContext('2d'); 55 56 IMG_CACHE["ground"] = new Image(); 57 IMG_CACHE["ground"].src = 'img/yuka1.png'; 58 59 ctx.fillStyle = "rgb( 0, 0, 0 )"; 60 ctx.fillRect(0, 0, CHIP_WIDTH*32, CHIP_HEIGHT*32); 61 62 sub = getSubMap( 0, 0, MAP_WIDTH, MAP_HEIGHT ); 63 64 for (var y=0; y<sub.length; y++) { 65 for (var x=0; x<sub[y].length; x++) { 66 if (sub[y][x] === 0) 67 ctx.drawImage(IMG_CACHE["ground"], 68 0, 0,CHIP_WIDTH, CHIP_HEIGHT, 69 CHIP_WIDTH*x,CHIP_HEIGHT*y,CHIP_WIDTH,CHIP_HEIGHT); 70 } 71 } 72 73 window.requestAnimationFrame( main ); 74 } 75window.addEventListener('load', main(), false); 76
回答1件
あなたの回答
tips
プレビュー