vue3圖片剪裁插件vue-img-cutter使用小結(jié)
很多博主都寫了vue-img-cutter這個插件怎么用,并且官網(wǎng)也有案例,但都是從上傳圖片開始的。我想要的是本來就有圖片不用用戶重新上傳或者先刪掉當(dāng)前的圖片再上傳裁剪,重點就是:已經(jīng)有圖片了。
接下來說說實現(xiàn)代碼吧
1.首先下載、安裝vue-img-cutter依賴
npm install vue-img-cutter --save-dev 或者 yarn add vue-img-cutter --save-dev
2.創(chuàng)建CutterImg.vue文件
先在官網(wǎng)上選擇配置,生成案例代碼,然后扒拉下來,進(jìn)行修改(官網(wǎng):vue-img-cutter),官網(wǎng)上就是從開始上傳圖片開始的,如果需要官網(wǎng)的效果直接復(fù)制官網(wǎng)的代碼修改即可。
3.接下來是根據(jù)我自己的需求修改的代碼:
<template> <div> <div style="display: flex; flex-direction: row"> <ImgCutter ref="imgCutterModal" v-on:cutDown="cutDownImg" :WatermarkText="''" :is-modal="false" :tool="false" :sizeChange="false" :boxWidth="700" :boxHeight="458" :cutWidth="470" :cutHeight="270" @onPrintImg="cutterPrintImg" :originalGraph="true" style="padding: 10px" > <template #cancel> <div></div> </template> </ImgCutter> <div style="margin-left: 10px; padding: 10px"> <div style=" margin-left: 10px; padding-bottom: 20px; font-size: 18px; font-weight: bold; " > 圖像預(yù)覽 </div> <div style="width: 470px;height: 270px; border: 1px solid #e8e8e8; background-color: rgba(0, 0, 0, 0.1)"> <img :src="temImgPath" style="width: 100%" /> </div> </div> </div> </div> </template>
<script setup> import ImgCutter from "vue-img-cutter";//引入vue-img-cutter import { ref, watch, onMounted} from "vue"; const emit = defineEmits(["getCoverImage"]);//與父組件事件通信 const props = defineProps({ imgUrl: String, }); const temImgPath = ref(""); const imgCutterModal=ref(); watch( () => props.imgUrl, (val, o) => { temImgPath.value = props.imgUrl; imgCutterModal.value.handleOpen({ name:"封面圖片", src:props.imgUrl }); } ); //實時更新預(yù)覽效果 function cutterPrintImg(obj) { temImgPath.value = obj.dataURL; } // 圖片裁剪點擊確認(rèn)后觸發(fā) function cutDownImg(option) { //向后端發(fā)送請求等 //請求成功時,emit("getCoverImage"),讓父組件再次獲取剛修改的新圖片 } </script>
<style lang="less" scoped> .remove-btn-wrap { position: absolute; left: 0; bottom: 0; right: 0; top: 0; text-align: center; display: none; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.5); } .ant-upload:hover .remove-btn-wrap { display: flex; } :deep(.ant-upload) { img { width: 100%; height: 100%; object-fit: cover; border-radius: 2px; } } :deep(.copyright a[data-v-391393b9]){ display: none !important; } </style>
父組件:也就是你想要用到圖片剪裁的組件
<template> <CutterImg :imgUrl="imgData.propsURL" @getCoverImage="getCoverImage"></CutterImg> </template> <script setup> import CutterImg from "@/components/CutterImg.vue"; //獲取封面圖片 const getCoverImage = () => { //發(fā)送請求 } </script>
imgData.propsURL是來自于后端請求獲取到的目前頁面顯示的圖片地址。請自行替換。
4.修改之后的效果如下:
4.1頁面顯示當(dāng)前的圖片
4.2點擊圖片出現(xiàn)彈窗,以上代碼沒有加彈窗的代碼是因為每個人使用的UI組件庫不同,如果需要自己添加彈窗的代碼即可。
4.3可以看到裁剪區(qū)域顯示的是當(dāng)前圖片,圖像預(yù)覽區(qū)域顯示的是當(dāng)前圖片剪裁后的效果。
4.4圖片剪裁區(qū)域可以鼠標(biāo)放大縮小和拖動剪裁框,右側(cè)的圖像預(yù)覽區(qū)域會實時變化。
4.5可以點擊【選擇圖片】按鈕,上傳其他的圖片。最后點擊確定將圖片信息傳遞給后端。
到此這篇關(guān)于vue3圖片剪裁插件vue-img-cutter的文章就介紹到這了,更多相關(guān)vue圖片剪裁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從vue基礎(chǔ)開始創(chuàng)建一個簡單的增刪改查的實例代碼(推薦)
這篇文章主要介紹了從vue基礎(chǔ)開始創(chuàng)建一個簡單的增刪改查的實例代碼,需要的朋友參考下2018-02-02解決vue2+vue-router動態(tài)路由添加及路由刷新后消失問題
這篇文章主要介紹了解決vue2+vue-router動態(tài)路由添加及路由刷新后消失問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Vue封裝DateRangePicker組件流程詳細(xì)介紹
在后端管理項目中使用vue來進(jìn)行前端項目的開發(fā),但我們都知道Vue實際上無法監(jiān)聽由第三方插件所引起的數(shù)據(jù)變化。也無法獲得JQuery這樣的js框架對元素值的修改的。而日期控件daterangepicker又基于JQuery來實現(xiàn)的2022-11-11