当前位置:首页 > VUE

vue实现蜂窝图

2026-03-10 00:38:37VUE

Vue 实现蜂窝图的方法

蜂窝图(Hexagon Grid)通常用于数据可视化或游戏界面,以下是几种实现方式:

使用 SVG 绘制蜂窝网格

通过 SVG 的 <polygon> 元素可以精确绘制六边形:

vue实现蜂窝图

<template>
  <svg width="500" height="500">
    <polygon 
      v-for="(hex, index) in hexagons"
      :key="index"
      :points="getHexPoints(hex.x, hex.y, size)"
      fill="#4CAF50"
      stroke="#333"
    />
  </svg>
</template>

<script>
export default {
  data() {
    return {
      size: 30, // 六边形边长
      hexagons: [] // 存储六边形中心坐标
    };
  },
  mounted() {
    this.generateGrid(5, 5); // 生成5x5网格
  },
  methods: {
    getHexPoints(x, y, size) {
      const points = [];
      for (let i = 0; i < 6; i++) {
        const angle = Math.PI / 3 * i;
        points.push(
          (x + size * Math.cos(angle)).toFixed(2),
          (y + size * Math.sin(angle)).toFixed(2)
        );
      }
      return points.join(' ');
    },
    generateGrid(rows, cols) {
      const offsetX = this.size * 1.5;
      const offsetY = this.size * Math.sqrt(3);

      for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
          this.hexagons.push({
            x: c * offsetX + (r % 2) * offsetX / 2,
            y: r * offsetY * 0.75
          });
        }
      }
    }
  }
};
</script>

使用 CSS 实现蜂窝布局

通过 CSS 的 clip-path 属性和网格布局:

<template>
  <div class="honeycomb">
    <div 
      v-for="(item, index) in items"
      :key="index"
      class="hexagon"
      :style="{ backgroundColor: item.color }"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<style>
.honeycomb {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  width: 300px;
}

.hexagon {
  width: 100px;
  height: 115px;
  background: #3498db;
  position: relative;
  clip-path: polygon(
    25% 0%, 75% 0%, 
    100% 50%, 75% 100%, 
    25% 100%, 0% 50%
  );
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}
</style>

使用第三方库(如 vue-hexagon)

对于快速实现,可以安装专用库:

vue实现蜂窝图

npm install vue-hexagon

示例代码:

<template>
  <vue-hexagon
    v-for="(hex, i) in hexData"
    :key="i"
    :x="hex.x"
    :y="hex.y"
    :radius="30"
    :fill="hex.color"
  />
</template>

<script>
import VueHexagon from 'vue-hexagon';

export default {
  components: { VueHexagon },
  data() {
    return {
      hexData: [
        { x: 50, y: 50, color: '#e74c3c' },
        { x: 100, y: 90, color: '#3498db' }
      ]
    };
  }
};
</script>

交互功能增强

为蜂窝添加点击事件和数据绑定:

<template>
  <svg width="500" height="500">
    <polygon 
      v-for="(hex, index) in hexagons"
      :key="index"
      :points="getHexPoints(hex.x, hex.y, size)"
      :fill="hex.active ? '#e74c3c' : '#3498db'"
      @click="toggleHex(index)"
    />
  </svg>
</template>

<script>
export default {
  methods: {
    toggleHex(index) {
      this.$set(this.hexagons[index], 'active', !this.hexagons[index].active);
    }
  }
};
</script>

关键注意事项

  • 坐标计算:六边形中心点坐标需遵循 $x = 1.5 \times size \times column$ 和 $y = \sqrt{3} \times size \times (row + 0.5 \times (column \% 2))$ 的规律
  • 性能优化:大数据量时建议使用 Canvas 而非 SVG
  • 响应式设计:通过监听窗口大小变化重新计算网格布局

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

相关文章

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <…

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置 使…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…