当前位置:首页 > VUE

vue项目实现截取图片

2026-01-21 13:14:09VUE

使用HTML5 Canvas实现截图

在Vue项目中创建一个隐藏的Canvas元素,将目标图片绘制到Canvas上,通过Canvas API截取指定区域。

vue项目实现截取图片

<template>
  <div>
    <img ref="targetImage" src="your-image.jpg" @load="onImageLoaded">
    <canvas ref="canvas" style="display:none"></canvas>
  </div>
</template>

<script>
export default {
  methods: {
    onImageLoaded() {
      const canvas = this.$refs.canvas
      const img = this.$refs.targetImage
      const ctx = canvas.getContext('2d')

      canvas.width = img.width
      canvas.height = img.height
      ctx.drawImage(img, 0, 0)

      // 截取区域(x,y,width,height)
      const imageData = ctx.getImageData(100, 100, 200, 200)

      // 创建新Canvas存放截图
      const newCanvas = document.createElement('canvas')
      newCanvas.width = 200
      newCanvas.height = 200
      newCanvas.getContext('2d').putImageData(imageData, 0, 0)

      // 获取截图数据URL
      const screenshot = newCanvas.toDataURL('image/png')
      console.log(screenshot)
    }
  }
}
</script>

使用第三方库html2canvas

安装html2canvas库实现更复杂的截图功能,支持DOM元素转图片。

vue项目实现截取图片

npm install html2canvas
<template>
  <div>
    <div ref="captureArea" class="content-to-capture">
      <!-- 需要截图的内容 -->
    </div>
    <button @click="capture">截图</button>
  </div>
</template>

<script>
import html2canvas from 'html2canvas'

export default {
  methods: {
    async capture() {
      const element = this.$refs.captureArea
      const canvas = await html2canvas(element, {
        backgroundColor: null,
        logging: false,
        useCORS: true
      })

      const image = canvas.toDataURL('image/png')
      this.downloadImage(image, 'screenshot.png')
    },

    downloadImage(dataUrl, filename) {
      const link = document.createElement('a')
      link.href = dataUrl
      link.download = filename
      link.click()
    }
  }
}
</script>

实现区域选择截图

结合鼠标事件实现用户自定义区域截图功能。

<template>
  <div>
    <div 
      @mousedown="startSelection"
      @mousemove="drawSelection"
      @mouseup="endSelection"
      class="image-container"
    >
      <img ref="image" src="your-image.jpg">
      <div v-if="selecting" class="selection-box" :style="selectionStyle"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selecting: false,
      startX: 0,
      startY: 0,
      endX: 0,
      endY: 0
    }
  },
  computed: {
    selectionStyle() {
      const left = Math.min(this.startX, this.endX)
      const top = Math.min(this.startY, this.endY)
      const width = Math.abs(this.endX - this.startX)
      const height = Math.abs(this.endY - this.startY)

      return {
        left: `${left}px`,
        top: `${top}px`,
        width: `${width}px`,
        height: `${height}px`
      }
    }
  },
  methods: {
    startSelection(e) {
      this.selecting = true
      this.startX = e.offsetX
      this.startY = e.offsetY
      this.endX = e.offsetX
      this.endY = e.offsetY
    },
    drawSelection(e) {
      if (this.selecting) {
        this.endX = e.offsetX
        this.endY = e.offsetY
      }
    },
    endSelection() {
      this.selecting = false
      this.captureSelectedArea()
    },
    captureSelectedArea() {
      const canvas = document.createElement('canvas')
      const img = this.$refs.image
      const ctx = canvas.getContext('2d')

      const left = Math.min(this.startX, this.endX)
      const top = Math.min(this.startY, this.endY)
      const width = Math.abs(this.endX - this.startX)
      const height = Math.abs(this.endY - this.startY)

      canvas.width = width
      canvas.height = height
      ctx.drawImage(
        img,
        left, top, width, height,
        0, 0, width, height
      )

      const screenshot = canvas.toDataURL('image/png')
      console.log(screenshot)
    }
  }
}
</script>

<style>
.image-container {
  position: relative;
  display: inline-block;
}
.selection-box {
  position: absolute;
  border: 2px dashed #3498db;
  background-color: rgba(52, 152, 219, 0.3);
  pointer-events: none;
}
</style>

响应式截图处理

处理不同设备像素比(DPR)下的截图清晰度问题。

async captureHighDPI() {
  const element = this.$refs.captureElement
  const dpr = window.devicePixelRatio || 1

  const canvas = await html2canvas(element, {
    scale: dpr,
    logging: false,
    useCORS: true
  })

  // 调整画布尺寸以适应原始显示尺寸
  canvas.style.width = `${canvas.width / dpr}px`
  canvas.style.height = `${canvas.height / dpr}px`

  const image = canvas.toDataURL('image/png', 1.0)
  this.downloadImage(image, 'high-dpi-screenshot.png')
}

标签: 项目图片
分享给朋友:

相关文章

uniapp项目源码

uniapp项目源码

Uniapp 项目源码获取方式 官方示例与模板 Uniapp 官方提供了多个开源项目模板和示例,涵盖基础框架、UI组件库等。可通过以下途径获取: 在 DCloud 插件市场 搜索“uniapp模…

vue实现图片单选

vue实现图片单选

Vue 实现图片单选功能 在 Vue 中实现图片单选功能可以通过多种方式完成,以下是两种常见的实现方法: 方法一:使用 v-model 和单选按钮 通过将单选按钮与图片绑定,利用 v-mod…

react项目如何启动

react项目如何启动

启动React项目的步骤 确保已安装Node.js(建议版本12以上)和npm/yarn。可通过以下命令检查版本: node -v npm -v 全局安装create-react-app脚手架工具(…

vue实现放大图片

vue实现放大图片

实现图片放大功能的方法 在Vue中实现图片放大功能,可以通过多种方式实现。以下是几种常见的方法: 使用CSS transform属性 通过CSS的transform: scale()属性实现图片放…

如何部署react项目

如何部署react项目

部署 React 项目到生产环境 方法一:使用静态服务器部署(如 Nginx、Apache) 构建生产版本:运行 npm run build 或 yarn build,生成优化后的静态文件(位于 b…

vue实现加载图片

vue实现加载图片

Vue 实现图片加载的方法 在 Vue 中加载图片可以通过多种方式实现,以下是一些常见的方法: 使用静态资源路径 将图片放在 public 或 assets 目录下,通过相对路径或绝对路径引用。…