cropperjs实现放大缩小图片后配合canvas合成图片

原图:(这是两张图片,小车所在的图片是相框,芭比娃娃是可缩放的图片)

 放大后合成导出:

html:

<div class="lila">
    <img src="~@/static/images/lila/lila_two.png" id="image" class="image">
    <img src="~@/static/images/test/studo_2.png" id="bg1" class="bg1" />
</div>
<el-button type="success" @click="daochu" style="margin-top:50px">导出图片</el-button>

css:

.lila {
    height: 300px;
    width: 300px;
    margin: 50px auto 0;
    position: relative;
    border-radius: 15px;
    overflow: hidden;
    img {
        width: 100%;
        height: auto;
        border-radius: 15px;
    }
    .bg1 {
        position: absolute;
        top: 0;
        left: 0;
        pointer-events: none;
    }
    .image {
        pointer-events: auto;
    }
}

 js实现:

首先,下载cropperjs:

npm install cropper jquery

导入:

import Cropper from "cropperjs";

对cropperjs进行配置:

let image = document.getElementById("image")
this.cropper = new Cropper(image, {
    viewMode: 3,
    dragMode: 'move',
    autoCrop: 1,
    autoCropArea: 1,
    restore: false,
    modal: false,
    guides: false,
    highlight: false,
    cropBoxMovable: false,
    cropBoxResizable: false,
    guides: false,
    modal: false
})

cropperjs配置大全:

https://blog.csdn.net/qq_38048243/article/details/82013533

导出方法:

daochu() {
    var man1 = document.getElementById("image"),
        canvasBg = document.getElementById("bg1");
    let canvas = this.cropper.getCroppedCanvas({ width: 300, height: 300 })
    let ctx = canvas.getContext("2d").drawImage(canvasBg, 0, 0,300,300);
    let mybase = this.convertCanvasToImage(canvas)
    this.mybase = mybase
},
// 从 canvas 提取图片 image  
convertCanvasToImage(canvas) {
    return canvas.toDataURL("image/jpeg", 1);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值