vue canvas拼接图片(纵向)

vue 图片竖向拼接组件代码

底部有demo

<template>
  <div>
    <div>
      <input
        type="file"
        id="upFile"
        @change="upFile($event)"
        accept="image/*"
        multiple="multiple"
        style="display:none"
      />
      <label for="upFile">
        <span class="choosefile-btn">选择文件</span>
      </label>
    </div>
    <div v-if="isPreview">
      <img :src="imgurl" style="width:100%" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      returnData: null,
      imgurl: null,
    };
  },
  props: {
    isPreview: {
      type: Boolean,
      required: false,
      default: true,
    },
    isAutoDownload: {
      type: Boolean,
      required: false,
      default: false,
    },
    returnType: {
      type: String,
      required: false,
      default: "base64",
      validator: (value) => {
        return ["base64", "file"].indexOf(value) !== -1;
      },
    },
    widths: {
      type: [String, Number],
      required: false,
      default: 1366,
    },
    quality: {
      type: [String, Number],
      required: false,
      default: 0.618,
    },
  },
  methods: {
    base64ToBlob(code) {
      let parts = code.split(";base64,");
      let contentType = parts[0].split(":")[1];
      let raw = window.atob(parts[1]);
      let rawLength = raw.length;
      let uInt8Array = new Uint8Array(rawLength);

      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      return new Blob([uInt8Array], { type: contentType });
    },
    downloadFile(fileName, content) {
      let aLink = document.createElement("a");
      let blob = this.base64ToBlob(content); // new Blob([content]);
      let evt = document.createEvent("HTMLEvents");
      evt.initEvent("click", true, true); // initEvent 不加后两个参数在FF下会报错  事件类型,是否冒泡,是否阻止浏览器的默认行为
      aLink.download = fileName;
      aLink.href = URL.createObjectURL(blob);
      aLink.dispatchEvent(
        new MouseEvent("click", {
          bubbles: true,
          cancelable: true,
          view: window,
        })
      ); // 兼容火狐
    },

    upFile() {
      if (this.returnType !== "file") {
        const files = Array.from(event.target.files);
        this.filesToInstances(files);
      } else {
        this.returnData = event.target.files;
        this.returnRes();
      }
    },
    filesToInstances(files) {
      const length = files.length;
      let instances = [];
      let finished = 0;

      files.forEach((file, index) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = (e) => {
          const image = new Image();
          image.src = e.target.result;
          image.onload = () => {
            // 图片实例化成功后存起来
            instances[index] = image;
            finished++;
            if (finished === length) {
              this.drawImages(instances);
            }
          };
        };
      });
    },
    drawImages(images) {
      const width = this.widths;
      const heights = images.map((item) => (width / item.width) * item.height);
      const canvas = document.createElement("canvas");
      canvas.width = width;
      canvas.height = heights.reduce((total, current) => total + current);
      const context = canvas.getContext("2d");

      let y = 0;

      images.forEach((item, index) => {
        const height = heights[index];
        context.drawImage(item, 0, y, width, height);
        y += height;
      });
      const base64Url = canvas.toDataURL("image/jpeg", this.quality);

      this.dealImages(base64Url);
      if (this.returnType == "base64" && this.isAutoDownload) {
        this.downloadFile(this.createFileName(), base64Url);
      }
    },
    dealImages(url) {
      this.imgurl = url;
      this.returnData = url;
      this.returnRes();
    },
    returnRes() {
      this.$emit("getRes", this.returnData);
    },
    createFileName() {
      return "图片拼接" + new Date().getTime();
    },
  },
};
</script>

<style>
.choosefile-btn {
  display: inline-block;
  padding: 10px 20px;
  margin-top: 40px;
  background: rgb(83, 170, 148);
  border-radius: 8px;
  color: #efeefe;
}
</style>

使用

<Splicing :isPreview="true" @getRes="getSpellData"/>
...
import Splicing from '@/components/Splicing.vue';
components: {
    Splicing
  },
  getSpellData(e){
  //返回的base64或者file结果
      console.log(e);
    },

react拼接图片代码:react拼接图片代码

angular拼接图片代码:angular拼接图片代码

demo地址(vue):demo

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中使用Canvas进行图片放缩可以通过以下步骤实现: 1. 首先,在Vue组件中引入Canvas元素,并获取到Canvas的上下文对象: ```javascript <template> <canvas ref="canvas"></canvas> </template> <script> export default { mounted() { this.canvas = this.$refs.canvas; this.ctx = this.canvas.getContext('2d'); }, } </script> ``` 2. 加载图片并绘制到Canvas上: ```javascript methods: { loadImage() { const img = new Image(); img.src = 'path/to/image.jpg'; img.onload = () => { this.ctx.drawImage(img, 0, 0); }; }, } ``` 3. 实现图片放缩功能: ```javascript methods: { zoomIn() { const scale = 1.2; // 放大倍数 const canvasWidth = this.canvas.width; const canvasHeight = this.canvas.height; // 清空画布 this.ctx.clearRect(0, 0, canvasWidth, canvasHeight); // 缩放画布 this.canvas.width = canvasWidth * scale; this.canvas.height = canvasHeight * scale; // 绘制缩放后的图片 this.ctx.drawImage(this.$refs.canvas, 0, 0, canvasWidth * scale, canvasHeight * scale); }, zoomOut() { const scale = 0.8; // 缩小倍数 const canvasWidth = this.canvas.width; const canvasHeight = this.canvas.height; // 清空画布 this.ctx.clearRect(0, 0, canvasWidth, canvasHeight); // 缩放画布 this.canvas.width = canvasWidth * scale; this.canvas.height = canvasHeight * scale; // 绘制缩放后的图片 this.ctx.drawImage(this.$refs.canvas, 0, 0, canvasWidth * scale, canvasHeight * scale); }, } ``` 以上是一个简单的Vue组件,实现了图片的放大和缩小功能。你可以根据实际需求进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值