【问题标题】:Display isometric tiles correctly with HTML and CSS使用 HTML 和 CSS 正确显示等距图块
【发布时间】:2021-10-05 11:21:56
【问题描述】:

我想用 HTML 和 CSS 创建一个简单的等距平铺地图。

图块宽 128 像素,高 64 像素,但其中一些(如下图)包含图形元素,总高度为 128 像素。

在这个简化的示例中,等距平铺地图只有 2x2 平铺,应如下所示:

如何使用 HTML 和 CSS 正确显示这些图块,以便它们具有正确的位置和比例?

请注意,2x2 tilemap 只是一个示例。如果地图是 5x5 瓦片或 25x75 瓦片,该解决方案需要同样有效(或差不多)。无论瓷砖顺序如何,它也需要工作。

到目前为止,我的想法是旋转容器以使其等距,然后以相反方向旋转图块以“补偿”容器的旋转(我更喜欢以它们的方式显示图块绘制)。但我不确定这是正确的方法。位置不正确,图像失真。

你能告诉我我做错了什么吗?

.container {
  width: 256px;
  height: 256px;
  transform: rotateX(60deg) rotateZ(45deg);
  display: flex;
  flex-wrap: wrap;
  background-color: blue;
}

.tile {
  width: 128px;
  transform: rotateX(0deg) rotateZ(-45deg);
}

.grass {
  height: 64px;
  background-image: url("https://i.stack.imgur.com/39MxJ.png");
}

.bush {
  height: 128px;
  background-image: url("https://i.stack.imgur.com/GpNEI.png");
}
<div class="container">
  <div class="tile grass"></div>
  <div class="tile grass"></div>
  <div class="tile bush"></div>
  <div class="tile bush"></div>
</div>

【问题讨论】:

  • 您的定位似乎是相对于128pxheight,但因为瓷砖只是64px,这可能有助于改善定位
  • @Rana 我不确定你从哪里得到这张图片,但我只是更改了 sn-p 以使用帖子本身中提到的图片,并且与发布的结果相符。
  • 为什么选择 Canvas 方式?旁边你可以走 tmx 方式stackoverflow.com/questions/21349883/…
  • @HereticMonkey 你是如何获得图像的,因为我无法在下面的答案中做到这一点
  • 我刚刚edit提出了这个问题,并从问题底部的参考资料中复制了URL(在[1]:和[2]:旁边)。

标签: html css tile


【解决方案1】:

以目前的结构,只是改变一些CSS属性。

我认为不需要使用“转换”属性,这也可以通过display: grid 属性来实现。

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  grid-row: span 2;
  grid-auto-flow: dense;
  width: 258px;
  grid-auto-rows: 32px;
  min-height: 140px;
}

.tile {
  width: 128px;
  height: 87px;
}

.tile:first-child,
.bush:last-child {
  grid-column-start: 1;
  grid-column-end: 3;
  width: 100%;
}

.grass {
  background-image: url("https://i.stack.imgur.com/39MxJ.png");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: auto;
}

.bush {
  height: 128px;
  background-image: url("https://i.stack.imgur.com/GpNEI.png");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: auto;
  margin-top: -52px;
}
<div class="container">
  <div class="tile grass"></div>
   <div class="tile bush"></div>
  <div class="tile grass"></div>
  <div class="tile bush"></div>
</div>
这是Codepen 上的工作示例

【讨论】:

  • 谢谢。这看起来很不错。不幸的是,您的解决方案是为这种特殊布局量身定制的,有 2 个草瓦和 2 个灌木瓦。如果地图是 5x5 瓦片或 25x75 瓦片,我正在寻找一种同样有效(或差不多)的解决方案。无论瓷砖(草、灌木等)在地图中的位置如何,它都需要工作。很抱歉,我在问题中不清楚这一点。
【解决方案2】:

您必须使用 hit and trial 方法分别重新定位每个图块,如下面的 sn-p: 以下 sn-p 仅用于演示,您可以根据需要重新定位,蓝色是您想要的任何位置

.container {
  width: 256px;
  height: 256px;
  transform: rotateX(60deg) rotateZ(45deg);
  display: flex;
  flex-wrap: wrap;
  background-color: blue;
}

.tile {
  width: 128px;
}
.tile:nth-child(1) {
  transform: translateX(64px) translateY(64px) rotateX(0deg) rotateZ(-45deg);
}

.tile:nth-child(2) {
  transform: translateX(0) translateY(42px) rotateX(0deg) rotateZ(-45deg);
}

.tile:nth-child(3) {
  transform: translateX(16px) translateY(-18px) rotateX(0deg) rotateZ(-45deg);
}

.tile:nth-child(4) {
  transform: translateX(-45px) translateY(-42px) rotateX(0deg) rotateZ(-45deg);
}

.grass {
  height: 64px;
  background-image: url("https://i.stack.imgur.com/39MxJ.png");
}

.bush {
  height: 128px;
  background-image: url("https://i.stack.imgur.com/GpNEI.png");
}
<div class="container">
  <div class="tile grass"></div>
  <div class="tile grass"></div>
  <div class="tile bush"></div>
  <div class="tile bush"></div>
</div>

【讨论】:

  • 我只看到这个例子中的蓝色瓷砖
  • @JoshuaSwain 错误地删除了 width 的 .tile 属性。这就是不显示图片的原因
  • 谢谢 Rana,但我担心运行代码 sn-p 时结果看起来不太正确。它看起来像这样:i.imgur.com/SAAygVB.png 如果可能的话,我希望能够以相同的方式渲染所有图块,无论它们在地图中的位置如何(或至少所有相同类型的图块)。如果地图是 100x100 的图块,如果您必须单独设置它们的格式,CSS 将非常长。
  • @Candleout 仅供演示。我只是附上它们来展示如何制作方形瓷砖。可以根据需要重新定位。相反,您可以使用上面答案中所示的网格
  • 我正在寻找一种更灵活/通用的解决方案,它可以处理任意数量的图块(10x10、10x75 等)以及图块排列。可以为不同的瓦片类型(草、灌木等)设置单独的 CSS 规则,但我发现必须单独处理每个瓦片/位置是不切实际的。我在我的问题和对上述答案的评论中添加了对此的评论。很抱歉没有从一开始就说清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
相关资源
最近更新 更多