【问题标题】:js / canvas populate mapjs / canvas 填充地图
【发布时间】:2015-11-14 21:18:53
【问题描述】:

我正在学习 js 和画布。我想用小图块(例如 20 像素 x 20 像素)填充地图 到目前为止,我已经用字符填充了它,但图块会更好。我必须获得一组小图像还是有绘制瓷砖的方法? 我想我可以在主画布内创建很多 20x20 的画布,但我想这远非最佳。

这是我到目前为止所得到的。

var mapArray = [
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,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,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,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,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],
[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,0,0,0,0,0,0,0,0,0,0,0,0,0]
];

function popMap() {
    var canvas = document.getElementById('playground');
    var ctx = canvas.getContext('2d');

    ctx.font="18px Georgia";
    ctx.fillStyle="white";
    for (i=0;i<mapArray.length;i++) {
        for (j=0;j<mapArray.length;j++) {
            ctx.fillText(mapArray[i][j], i*20, j*20);
        }
    }
}

popMap();

顺便说一句,最终,我想制作一个简单的 rpg 游戏(一个用按键控制的角色在地图上移动),所以请就地图上的最佳方法提出建议。谢谢。

【问题讨论】:

    标签: javascript canvas tiles


    【解决方案1】:

    这是在游戏画布上绘制图块的方法。

    从这样的平铺 spritesheet 图像开始:

    左侧(草)瓷砖由 mapArray 值 0 表示。右侧(粘土)瓷砖由值 1 表示。

    然后使用 drawImage 的扩展形式在游戏画布上的任意位置绘制任一图块。

    drawImage(
    
        // use the spritesheet image as the image source
        spritesheetImage, 
    
        // clip a portion from the spritesheet image
        clipAtX, clipAtY, clipWidth, clipHeight,
    
        // draw the clipped portion to the canvas
        canvasX, canvasY, scaledWidth, scaledHeight
    
    );
    

    这是示例代码和演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    var mapArray = [
      [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,1,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,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,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,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],
      [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,0,0,0,0,0,0,0,0,0,0,0,0,0]
    ];
    
    var tw=20;
    var th=20;
    var spritesheet=new Image();
    spritesheet.onload=function(){
      canvas.width=tw*mapArray[0].length;
      canvas.height=th*mapArray.length;
      popMap();
    }
    spritesheet.src='https://dl.dropboxusercontent.com/u/139992952/multple/gametiles.png';
    
    function popMap() {
      for (i=0;i<mapArray.length;i++) {
        for (j=0;j<mapArray[i].length;j++){
          var tile=mapArray[i][j];
          ctx.drawImage(spritesheet,
                        tile*20,0,tw,th,
                        j*20,i*20,tw,th
    
                       );
        }
      }
    }
    body{ background-color: ivory; }
    #canvas{border:1px solid red; margin:0 auto; }
    &lt;canvas id="canvas" width=500 height=500&gt;&lt;/canvas&gt;

    【讨论】:

      【解决方案2】:

      一种解决方案是创建一组函数来绘制图块,每种图块类型都有一个函数。然后创建一个查找对象来保存所有这些函数。在填充地图的循环中,从查找对象中找到所需的函数并使用找到的函数绘制图块。

      例如,下面的代码将类型 0 的瓷砖绘制为浅红色正方形,将瓷砖类型 1 绘制为浅绿色正方形...

      var mapArray = [
          [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,1,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,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,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,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],
          [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,0,0,0,0,0,0,0,0,0,0,0,0,0]
      ];
      
      function fillTile0(ctx, x, y, width, height) {
          ctx.fillStyle = "#FF7777"
          ctx.fillRect(x, y, width, height);
      }
      
      function fillTile1(ctx, x, y, width, height) {
          ctx.fillStyle = "#77FF77"
          ctx.fillRect(x, y, width, height);
      }
      
      var fillTileFunctions = {
          "0": fillTile0,
          "1": fillTile1
      };
      
      function popMap() {
          var canvas = document.getElementById('playground');
          var ctx = canvas.getContext('2d');
          for (i=0;i<mapArray.length;i++) {
              for (j=0;j<mapArray[i].length;j++) {
                  var fillTile = fillTileFunctions[mapArray[i][j]];
                  if (fillTile) {
                      fillTile(ctx, i*20, j*20, 20, 20);
                  }
              }
          }
      }
      
      popMap();
      

      另一种解决方案是为每种图块类型创建单独的图像(例如 png、svg 等)。然后创建一个查找对象来保存这些图像。在填充地图的循环中,从查找对象中找到所需的图像并使用 ctx->drawImage() 函数绘制图块。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        • 2022-08-18
        • 2021-07-13
        • 2018-08-26
        相关资源
        最近更新 更多