当前位置:首页 > VUE

vue实现svg心电图

2026-02-19 17:11:59VUE

Vue 中实现 SVG 心电图

使用 SVG 路径绘制动态心电图

在 Vue 中可以通过 <svg><path> 元素实现动态心电图效果。关键点在于动态更新路径的 d 属性,模拟心电图波形。

<template>
  <div class="ecg-container">
    <svg width="100%" height="100" viewBox="0 0 500 100">
      <path 
        :d="pathData" 
        fill="none" 
        stroke="#FF0000" 
        stroke-width="2"
      />
    </svg>
  </div>
</template>

<script>
export default {
  data() {
    return {
      points: [],
      maxPoints: 50,
      amplitude: 20,
      frequency: 0.1
    }
  },
  computed: {
    pathData() {
      if (this.points.length === 0) return ''

      let d = `M ${this.points[0].x} ${this.points[0].y}`
      for (let i = 1; i < this.points.length; i++) {
        d += ` L ${this.points[i].x} ${this.points[i].y}`
      }
      return d
    }
  },
  mounted() {
    this.generatePoints()
    setInterval(this.updatePoints, 100)
  },
  methods: {
    generatePoints() {
      for (let i = 0; i < this.maxPoints; i++) {
        this.points.push({
          x: i * (500 / this.maxPoints),
          y: 50 + Math.sin(i * this.frequency) * this.amplitude
        })
      }
    },
    updatePoints() {
      // 移除第一个点
      this.points.shift()

      // 添加新点
      const lastX = this.points[this.points.length - 1].x
      this.points.push({
        x: lastX + (500 / this.maxPoints),
        y: 50 + Math.sin(lastX * this.frequency) * this.amplitude
      })
    }
  }
}
</script>

<style>
.ecg-container {
  width: 100%;
  background: #f0f0f0;
  padding: 20px;
}
</style>

使用 SVG 动画增强效果

可以通过 SVG 的动画特性使心电图看起来更真实:

vue实现svg心电图

<template>
  <svg width="100%" height="100" viewBox="0 0 500 100">
    <defs>
      <linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#FF0000" />
        <stop offset="100%" stop-color="#FF0000" stop-opacity="0" />
      </linearGradient>
    </defs>

    <path 
      :d="pathData" 
      fill="none" 
      stroke="url(#gradient)" 
      stroke-width="2"
    >
      <animate 
        attributeName="stroke-dashoffset"
        values="500;0" 
        dur="5s" 
        repeatCount="indefinite"
      />
    </path>
  </svg>
</template>

使用第三方库优化性能

对于更复杂的心电图效果,可以考虑使用专门的图表库:

  1. 安装 Chart.js 和 vue-chartjs:

    vue实现svg心电图

    npm install chart.js vue-chartjs
  2. 创建心电图组件:

    
    import { Line } from 'vue-chartjs'

export default { extends: Line, data() { return { chartData: { labels: Array(100).fill(''), datasets: [{ label: 'ECG', borderColor: '#FF0000', pointRadius: 0, borderWidth: 2, data: Array(100).fill(0) }] }, options: { responsive: true, maintainAspectRatio: false, animation: { duration: 0 }, scales: { xAxes: [{ display: false }], yAxes: [{ display: false, ticks: { min: -100, max: 100 } }] } } } }, mounted() { this.renderChart(this.chartData, this.options) setInterval(this.updateChart, 100) }, methods: { updateChart() { const newData = this.chartData.datasets[0].data.slice(1) newData.push(Math.random() * 100 - 50) this.chartData.datasets[0].data = newData this.$data._chart.update() } } }


#### 真实心电图数据模拟

要模拟更真实的心电图波形,可以使用数学函数组合:

```javascript
function generateECGPoint(x) {
  // 基础心率
  const base = Math.sin(x * 0.1) * 10

  // P波
  const pWave = x % 50 < 5 ? Math.sin((x % 50) * 0.6) * 8 : 0

  // QRS复合波
  let qrs = 0
  if (x % 50 >= 5 && x % 50 < 15) {
    const pos = (x % 50) - 5
    if (pos < 2) qrs = -pos * 15
    else if (pos < 4) qrs = -30 + (pos-2) * 25
    else if (pos < 6) qrs = 20 - (pos-4) * 10
    else qrs = 0
  }

  // T波
  const tWave = x % 50 >= 15 && x % 50 < 25 ? 
    Math.sin((x % 50 - 15) * 0.3) * 5 : 0

  return base + pWave + qrs + tWave
}

这些方法可以根据实际需求进行调整和组合,实现不同复杂度的心电图效果。

标签: 心电图vue
分享给朋友:

相关文章

vue实现共享屏幕

vue实现共享屏幕

共享屏幕的基本实现 在Vue中实现共享屏幕功能通常需要结合浏览器的getDisplayMedia API。以下是一个基础实现示例: <template> <div>…

vue 移动端实现签名

vue 移动端实现签名

Vue 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直接使…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…