当前位置:首页 > VUE

vue实现蜂窝图

2026-01-17 18:56:48VUE

Vue 实现蜂窝图

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

使用 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 属性创建六边形效果:

vue实现蜂窝图

<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 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…