当前位置:首页 > 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 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas 进行…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件,…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…