当前位置:首页 > VUE

vue实现蜂窝图

2026-01-17 18:56:48VUE

Vue 实现蜂窝图

实现蜂窝图(Hexagonal Grid)在 Vue 中可以通过 SVG 或 CSS 布局完成。以下是两种常见的方法:

vue实现蜂窝图

使用 SVG 绘制蜂窝图

通过 SVG 的 polygon 元素绘制六边形,并动态计算位置:

<template>
  <svg :width="width" :height="height">
    <g v-for="(hex, index) in hexagons" :key="index" :transform="`translate(${hex.x}, ${hex.y})`">
      <polygon
        :points="hexPoints"
        fill="#4CAF50"
        stroke="#2E7D32"
        @click="handleHexClick(index)"
      />
      <text x="0" y="5" text-anchor="middle" fill="white">{{ index + 1 }}</text>
    </g>
  </svg>
</template>

<script>
export default {
  data() {
    return {
      width: 600,
      height: 400,
      radius: 30, // 六边形半径
      hexagons: [] // 存储六边形坐标
    };
  },
  computed: {
    hexPoints() {
      const points = [];
      for (let i = 0; i < 6; i++) {
        const angle = (Math.PI / 3) * i;
        const x = this.radius * Math.cos(angle);
        const y = this.radius * Math.sin(angle);
        points.push(`${x},${y}`);
      }
      return points.join(" ");
    }
  },
  mounted() {
    this.generateHexGrid(5, 5); // 生成5x5的蜂窝网格
  },
  methods: {
    generateHexGrid(rows, cols) {
      const hexHeight = this.radius * 2;
      const hexWidth = Math.sqrt(3) * this.radius;
      const horizontalSpacing = hexWidth * 0.75;
      const verticalSpacing = hexHeight * 0.5;

      for (let row = 0; row < rows; row++) {
        for (let col = 0; col < cols; col++) {
          const x = col * horizontalSpacing;
          const y = row * verticalSpacing + (col % 2) * (hexHeight / 2);
          this.hexagons.push({ x, y });
        }
      }
    },
    handleHexClick(index) {
      console.log(`Hexagon ${index + 1} clicked`);
    }
  }
};
</script>

使用 CSS 实现蜂窝图

通过 CSS 的 clip-path 属性创建六边形效果:

<template>
  <div class="hex-grid">
    <div
      v-for="(hex, index) in hexagons"
      :key="index"
      class="hexagon"
      :style="{
        left: `${hex.x}px`,
        top: `${hex.y}px`
      }"
      @click="handleHexClick(index)"
    >
      {{ index + 1 }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hexWidth: 60,
      hexHeight: 70,
      hexagons: []
    };
  },
  mounted() {
    this.generateHexGrid(5, 5);
  },
  methods: {
    generateHexGrid(rows, cols) {
      const horizontalSpacing = this.hexWidth * 0.75;
      const verticalSpacing = this.hexHeight * 0.5;

      for (let row = 0; row < rows; row++) {
        for (let col = 0; col < cols; col++) {
          const x = col * horizontalSpacing;
          const y = row * verticalSpacing + (col % 2) * (this.hexHeight / 2);
          this.hexagons.push({ x, y });
        }
      }
    },
    handleHexClick(index) {
      console.log(`Hexagon ${index + 1} clicked`);
    }
  }
};
</script>

<style>
.hex-grid {
  position: relative;
  width: 600px;
  height: 400px;
  margin: 0 auto;
}

.hexagon {
  position: absolute;
  width: 60px;
  height: 70px;
  background-color: #4CAF50;
  clip-path: polygon(
    25% 0%,
    75% 0%,
    100% 50%,
    75% 100%,
    25% 100%,
    0% 50%
  );
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s;
}

.hexagon:hover {
  background-color: #2E7D32;
}
</style>

关键点说明

  • 六边形坐标计算:蜂窝网格需要交错排列,奇数行和偶数行的垂直偏移不同。
  • 交互处理:通过 @click 事件实现六边形的点击交互。
  • 响应式设计:可以根据容器尺寸动态调整蜂窝图的大小和位置。

这两种方法各有优势,SVG 适合复杂的图形操作,CSS 方法则更轻量且易于与现有样式集成。

vue实现蜂窝图

标签: 蜂窝vue
分享给朋友:

相关文章

vue实现文档导入

vue实现文档导入

Vue 实现文档导入的方法 在 Vue 中实现文档导入功能通常涉及文件上传、解析和处理。以下是几种常见的方法: 使用原生文件输入和 FileReader 通过 HTML 的原生 <input…

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vue…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 meth…

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用X…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> &l…