vue 實(shí)現(xiàn)無規(guī)則截圖
大家所見到的大多數(shù)都是有規(guī)則截圖,可以應(yīng)付大部分的應(yīng)用場(chǎng)景,但是對(duì)于圖片處理,想要將規(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ī)則截圖的資料請(qǐng)關(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è)簡(jiǎn)單的前端截圖工具代碼實(shí)例
- 微信小程序canvas拖拽、截圖組件功能
- JavaScript+html5 canvas實(shí)現(xiàn)本地截圖教程
- vue 實(shí)現(xiàn)網(wǎng)頁截圖功能詳解
- 如何用vue實(shí)現(xiàn)網(wǎng)頁截圖你知道嗎
- Vue拖動(dòng)截圖功能的簡(jiǎn)單實(shí)現(xiàn)方法
- electron+vue實(shí)現(xiàn)div contenteditable截圖功能
- Vue+canvas實(shí)現(xiàn)視頻截圖功能
相關(guān)文章
Vue項(xiàng)目環(huán)境搭建詳細(xì)總結(jié)
這篇文章主要為大家介紹了Vue項(xiàng)目環(huán)境搭建總結(jié)篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Vue3項(xiàng)目pc端瀏覽器樣式正常但移動(dòng)端部分樣式失效問題的解決方法
這篇文章主要介紹了Vue3項(xiàng)目pc端瀏覽器樣式正常但移動(dòng)端部分樣式失效問題的解決方法,文中通過圖文講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-07-07
Vue+Echarts報(bào)錯(cuò)Cannot?set?properties?of?undefined?(settin
這篇文章主要介紹了Vue+Echarts報(bào)錯(cuò)Cannot?set?properties?of?undefined?(setting?‘plate‘)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue2.0開發(fā)實(shí)踐總結(jié)之入門篇
這篇文章主要為大家總結(jié)了vue2.0開發(fā)實(shí)踐之入門,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Vue Element校驗(yàn)validate的實(shí)例
這篇文章主要介紹了Vue Element校驗(yàn)validate的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09

