vue 實(shí)現(xiàn)無規(guī)則截圖
大家所見到的大多數(shù)都是有規(guī)則截圖,可以應(yīng)付大部分的應(yīng)用場景,但是對于圖片處理,想要將規(guī)則交給用戶,普通的截圖已經(jīng)滿足不了用戶了,那我們能不能前端實(shí)現(xiàn)圖片的任意規(guī)則截取,接下來讓我一起探討一下吧!

通過 svg 實(shí)現(xiàn) 圖片截取
使用svg中clipPath image標(biāo)簽 通過id 映射, 動(dòng)態(tài)位置polygon的坐標(biāo),實(shí)現(xiàn)圖片的截取
<div>
<div class="content" @mousemove="mousemove" @mouseup="(e) => {mouseup(e);}">
<!-- 畫布展示 -->
<svg
ref="blackSvg"
class="blackSvg"
xmlns="http://www.w3.org/2000/svg"
width="300"
height="300"
>
<defs>
<clipPath id="clippath">
<polygon :points="points"></polygon>
</clipPath>
</defs>
<image
xmlns:link="http://www.w3.org/1999/xlink"
rel="external nofollow"
width="300"
height="300"
preserveAspectRatio="none"
style="clip-path: url(#clippath)"
></image>
</svg>
<!-- 拖拽點(diǎn) -->
<ul class="interception">
<li
v-for="item in 4"
:ref="`li${item}`"
:key="item"
@mousedown="(e) => {mousedown(e, item);}"
></li>
</ul>
<!-- 底圖展示 -->
<img
class="blackImge"
src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg"
alt=""
/>
<!-- 遮罩層 -->
<div class="blackDiv"></div>
</div>
</div>
css部分
<style lang="sass">
.blackDiv
width: 100%
height: 100%
position: absolute
top: 0
z-index: 2
background: rgba(0,0,0, 1)
.content
width:300px
height:300px
text-align: left
position: relative
.blackSvg
position: absolute
top: 0
z-index: 3
.blackImge
position: absolute
top: 0
left: 0
width: 300px
height: 300px
.interception
list-style: none
position: absolute
top: 0
margin: 0
padding: 0
z-index: 3
>li
position: absolute
width: 10px
height: 10px
background: blue
border-radius: 50%
cursor: pointer
&:hover
transform: scale(1.2)
transition-duration: .2
>li:nth-child(1)
top: 10px
left: 10px
>li:nth-child(2)
top: 10px
left: 100px
>li:nth-child(3)
top: 100px
left: 100px
>li:nth-child(4)
top: 100px
left: 10px
</style>
<script>
export default {
name: 'Canvas',
data() {
return {
points: '0 0,300 0,300 300,0 300', // 圖片展示初始化
status: false,
index: 0,
disX: 0,
disY: 0,
coordinates: { // 初始化拖拽點(diǎn)
1: [0, 0],
2: [300, 0],
3: [300, 300],
4: [0, 300],
},
};
},
mounted() {
this.$nextTick(() => {
for (let key in this.coordinates) {
const left = this.coordinates[key][0];
const top = this.coordinates[key][1];
this.$refs[`li${key}`].style.left = `${left}px`;
this.$refs[`li${key}`].style.top = `${top}px`;
if (key == 2 || key == 3) {
this.$refs[`li${key}`].style.left = `${left - 10}px`;
}
if (key == 3 || key == 4) {
this.$refs[`li${key}`].style.top = `${top - 10}px`;
}
}
document.onmouseup = () => {
this.status = false;
};
});
},
methods: {
//鼠標(biāo)按下
mousedown(e, index) {
this.status = true;
this.index = index;
this.disX = e.clientX - this.$refs[`li${index}`].offsetLeft;
this.disY = e.clientY - this.$refs[`li${index}`].offsetTop;
},
// 鼠標(biāo)抬起
mouseup(e) {
this.status = false;
},
// 鼠標(biāo)移動(dòng)
mousemove(e) {
if (this.status) {
let left = e.clientX - this.disX;
let top = e.clientY - this.disY;
this.$refs[`li${this.index}`].style.left = `${left}px`;
this.$refs[`li${this.index}`].style.top = `${top}px`;
this.coordinates[this.index] = [left, top];
const pointsArr = [];
for (let item in this.coordinates) {
pointsArr.push(
Array.from(this.coordinates[item], (e) => {
return e + 5;
})
);
}
this.points = pointsArr.join(' ');
}
},
},
};
效果圖展示

源碼地址
github地址--> github.com/lgxin/captu…
以上就是vue 實(shí)現(xiàn)無規(guī)則截圖的詳細(xì)內(nèi)容,更多關(guān)于vue 無規(guī)則截圖的資料請關(guān)注腳本之家其它相關(guān)文章!
- JS實(shí)現(xiàn)預(yù)加載視頻音頻/視頻獲取截圖(返回canvas截圖)
- vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能
- vue3如何將html元素變成canvas(海報(bào)生成),進(jìn)行圖片保存/截圖
- 原生js基于canvas實(shí)現(xiàn)一個(gè)簡單的前端截圖工具代碼實(shí)例
- 微信小程序canvas拖拽、截圖組件功能
- JavaScript+html5 canvas實(shí)現(xiàn)本地截圖教程
- vue 實(shí)現(xiàn)網(wǎng)頁截圖功能詳解
- 如何用vue實(shí)現(xiàn)網(wǎng)頁截圖你知道嗎
- Vue拖動(dòng)截圖功能的簡單實(shí)現(xiàn)方法
- electron+vue實(shí)現(xiàn)div contenteditable截圖功能
- Vue+canvas實(shí)現(xiàn)視頻截圖功能
相關(guān)文章
Vue全局?jǐn)r截所有請求并在請求頭中添加token方式
這篇文章主要介紹了Vue全局?jǐn)r截所有請求并在請求頭中添加token方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue3+vite實(shí)現(xiàn)使用svg可改變顏色的全過程
Vue3 + Vite 使用 SVG 的方法主要是為了引入和利用圖標(biāo)庫、自定義組件以及通過插件簡化項(xiàng)目構(gòu)建過程,這篇文章給大家介紹了Vue3+vite實(shí)現(xiàn)使用svg可改變顏色的全過程,需要的朋友可以參考下2024-07-07
解決vue?vite啟動(dòng)項(xiàng)目報(bào)錯(cuò)ERROR:?Unexpected?“\x88“?in?JSON?的問題
這篇文章主要介紹了vue?vite啟動(dòng)項(xiàng)目報(bào)錯(cuò)ERROR:?Unexpected?“\x88“?in?JSON?原因,本文給出出現(xiàn)此類問題的原因所在并給出解決方法,需要的朋友可以參考下2022-09-09
vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量
這篇文章主要給大家介紹了關(guān)于vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
15分鐘學(xué)會(huì)vue項(xiàng)目改造成SSR(小白教程)
這篇文章主要介紹了15分鐘學(xué)會(huì)vue項(xiàng)目改造成SSR(小白教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue3?使用provide?inject父子組件傳值失敗且子組件不響應(yīng)
這篇文章主要介紹了vue3使用provide?inject父子組件傳值傳不過去且傳遞后子組件不具備響應(yīng)性問題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

