前端截图并保存

<template>
  <div>
    <canvas ref="canvas"></canvas>
    <button @click="takeScreenshot">截图</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      originalImage: null,
      comparisonImage: null,
    };
  },
  mounted() {
    this.loadImages();
  },
  methods: {
    loadImages() {
      // 加载原图和对比图
      this.originalImage = new Image();
      this.comparisonImage = new Image();
      this.originalImage.src = 'path/to/original/image.jpg';
      this.comparisonImage.src = 'path/to/comparison/image.jpg';
      
      this.originalImage.onload = () => {
        this.drawImage(this.originalImage, 0, 0);
      };
      this.comparisonImage.onload = () => {
        this.drawImage(this.comparisonImage, this.originalImage.width, 0);
      };
    },
    drawImage(image, x, y) {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      ctx.drawImage(image, x, y);
    },
    takeScreenshot() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      const x = 100; // 截图起始x坐标
      const y = 100; // 截图起始y坐标
      const width = 200; // 截图宽度
      const height = 200; // 截图高度

      // 创建一个新的Canvas用于截图
      const screenshotCanvas = document.createElement('canvas');
      screenshotCanvas.width = width;
      screenshotCanvas.height = height;

      // 将截图区域绘制到新的Canvas上
      const screenshotCtx = screenshotCanvas.getContext('2d');
      screenshotCtx.drawImage(canvas, x, y, width, height, 0, 0, width, height);

      // 保存截图
      const dataURL = screenshotCanvas.toDataURL('image/png');
      this.downloadImage(dataURL);
    },
    downloadImage(dataURL) {
      const link = document.createElement('a');
      link.download = 'screenshot.png';
      link.href = dataURL;
      link.click();
    }
  }
};
</script>

<style>
/* 样式可以根据需要添加 */
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值