vue-cropper 图片裁剪(修改裁剪框的大小以及位置)

一、安装使用

# npm 安装
npm install vue-cropper
// 组件内使用
import { VueCropper }  from 'vue-cropper' 
components: {
    VueCropper,
},

// main.js里面使用
import VueCropper from 'vue-cropper' 
Vue.use(VueCropper)

// cdn方式使用
<script src="vuecropper.js"></script>
Vue.use(window['vue-cropper'])

<vueCropper
    ref="cropper"
    :img="option.img"
    :outputSize="option.size"
    :outputType="option.outputType"
></vueCropper>

二、示例

<div class="show-info">
    <h2>example1 基本例子 无限制</h2>
    <div class="test test1">
        <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :fixedBox="option.fixedBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :autoCropWidth="option.autoCropWidth"
            :autoCropHeight="option.autoCropHeight"
            :centerBox="option.centerBox"
            :high="option.high"
            :infoTrue="option.infoTrue"
            @realTime="realTime"
            @imgLoad="imgLoad"
            @cropMoving="cropMoving"
            :enlarge="option.enlarge"
        ></vueCropper>
    </div>
    <div class="test-button">
        <button @click="changeImg" class="btn">changeImg</button>
        <label class="btn" for="uploads">upload</label>
        <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event, 1)">
        <button @click="startCrop" v-if="!crap" class="btn">start</button>
        <button @click="stopCrop" v-else class="btn">stop</button>
        <button @click="clearCrop" class="btn">clear</button>
        <button @click="refreshCrop" class="btn">refresh</button>
        <button @click="changeScale(1)" class="btn">+</button>
        <button @click="changeScale(-1)" class="btn">-</button>
        <button @click="rotateLeft" class="btn">rotateLeft</button>
        <button @click="rotateRight" class="btn">rotateRight</button>
        <button @click="finish('base64')" class="btn">preview(base64)</button>
        <button @click="finish('blob')" class="btn">preview(blob)</button>
        <a @click="down('base64')" class="btn">download(base64)</a>
        <a @click="down('blob')" class="btn">download(blob)</a>
        <a :href="downImg" download="demo.png" ref="downloadDom"></a>
    </div>

    <p>截图框大小</p>
    <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden',
            'margin': '5px'}">
        <div :style="previews.div">
            <img :src="previews.url" :style="previews.img">
        </div>
    </div>
    
    <p>中等大小</p>
    <div :style="previewStyle1"> 
        <div :style="previews.div">
            <img :src="previews.url" :style="previews.img">
        </div>
    </div>

    <p>迷你大小</p>
    <div :style="previewStyle2"> 
        <div :style="previews.div">
            <img :src="previews.url" :style="previews.img">
        </div>
    </div>


    <div style="display:block; width: 100%;">
        <label class="c-item">
            <span>上传图片是否显示原始宽高 (针对大图 可以铺满)</span>
            <input type="checkbox" v-model="option.original">
            <span>original: {{ option.original}}</span>
        </label>
        <label class="c-item">
            <span>是否根据dpr生成适合屏幕的高清图片</span>
            <input type="checkbox" v-model="option.high">
            <span>high: {{ option.high}}</span>
        </label>
        <label class="c-item">
            <span>是否输出原图比例的截图</span>
            <input type="checkbox" v-model="option.full">
            <span>full: {{ option.full}}</span>
        </label>
        <label class="c-item">
            <span>截图信息展示是否是真实的输出宽高</span>
            <input type="checkbox" v-model="option.infoTrue">
            <span>infoTrue: {{ option.infoTrue}}</span>							
        </label>
        <label class="c-item">
            <span>能否拖动图片</span>
            <input type="checkbox" v-model="option.canMove">
            <span>canMove: {{ option.canMove}}</span>
        </label>
        <label class="c-item">
            <span>能否拖动截图框</span>
            <input type="checkbox" v-model="option.canMoveBox">
            <span>canMoveBox: {{ option.canMoveBox}}</span>
        </label>
        <label class="c-item">
            <span>截图框固定大小</span>
            <input type="checkbox" v-model="option.fixedBox">
            <span>fixedBox: {{ option.fixedBox}}</span>
        </label>
        <label class="c-item">
            <span>是否自动生成截图框</span>
            <input type="checkbox" v-model="option.autoCrop">
            <span>autoCrop: {{ option.autoCrop}}</span>
        </label>
        <label class="c-item">
            <span>截图框是否限制在图片里(只有在自动生成截图框时才能生效)</span>
            <input type="checkbox" v-model="option.centerBox">
            <span>centerBox: {{ option.centerBox}}</span>
        </label>
        <label class="c-item">
            <span>是否按照截图框比例输出 默认为1 </span>
            <input type="number" v-model="option.enlarge">
        </label>
        <p>输出图片格式</p>
        <label class="c-item">
            <label>jpg  <input type="radio" name="type" value="jpeg" v-model="option.outputType"></label>
            <label>png  <input type="radio" name="type" value="png" v-model="option.outputType"></label>
            <label>webp <input type="radio" name="type" value="webp" v-model="option.outputType"></label>
        </label>
    </div>
    <codes>
        <div slot="body">{{ code1 }}</div>
    </codes>
</div>

三、关键配置项

在这里插入图片描述
在这里插入图片描述

四、官方参考文档

官方参考demo

五、定制化配置

修改裁剪框的大小以及位置

this.$refs.cropper.getCropAxis() // 获取裁剪框基于容器的坐标点 {x1: 50, x2: 50, y1: 50, y2: 50}
this.$refs.cropper.cropW  // 裁剪框宽
this.$refs.cropper.cropH // 裁剪框高

update(refWord) {
      this.$refs.cropper.goAutoCrop();// 重新生成裁剪框
      this.$nextTick(() => {
        // if cropped and has position message, update crop box
        // 设置自动裁剪框的宽高和位置
        this.$refs.cropper.cropOffsertX = refWord.cropInfo.offsetX;// x1
        this.$refs.cropper.cropOffsertY = refWord.cropInfo.offsetY;// y1
        this.$refs.cropper.cropW = refWord.cropInfo.width;// 裁剪框宽
        this.$refs.cropper.cropH = refWord.cropInfo.height;// 裁剪框高
      });
 }

// 坐标点位置
{
   axis: {
     x1: 1, // 左上角
     x2: 1// 右上角
     y1: 1// 左下角
     y2: 1 // 右下角
   }
 }
Vue-cropper 是一个基于 Vue.js图片裁剪组件,它允许用户在网页上通过拖拽等操作来裁剪图片。使用该组件时,你可以很方便地添加图片裁剪功能到你的项目中,用户可以通过它选择需要裁剪的区域,并且裁剪(也就是选区)的位置大小是可以调整的。 要在 Vue 1.x 版本中使用 vue-cropper修改裁剪位置,你需要首先安装 vue-cropper 组件,然后在你的 Vue 实例中引入并注册该组件。可以通过 npm 或者 yarn 来安装: ```bash npm install vue-cropper --save # 或者 yarn add vue-cropper ``` 然后在你的 Vue 组件中引入并使用它: ```javascript <template> <div> <vue-cropper ref="myCropper" :src="imageSrc" @init="initCrop" @ready="ready" @done="doneCrop" ></vue-cropper> <button @click="moveCropper">移动裁剪</button> </div> </template> <script> import VueCropper from 'vue-cropper'; export default { components: { VueCropper }, data() { return { imageSrc: 'your-image-url.jpg' // 设置图片地址 }; }, methods: { initCrop(cropper) { // 初始化裁剪器实例,可设置初始参数,例如裁剪大小等 }, ready(cropper) { // 裁剪器准备就绪后调用 }, doneCrop(img) { // 裁剪完成后的回调函数,返回裁剪后的图片 console.log(img); }, moveCropper() { // 通过引用 vue-cropper 实例来移动裁剪 this.$refs.myCropper.moveCropper(10, 10); // 参数为 x 轴和 y 轴的移动距离 } } }; </script> ``` 在上面的代码中,`moveCropper` 方法通过引用 `vue-cropper` 的实例并调用 `moveCropper` 方法来移动裁剪。你可以在适当的位置,比如按钮点击事件中调用这个方法来实现裁剪位置修改
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值