怎样用vue实现无规则截图的功能?
Admin 2021-08-30 群英技术资讯 1699 次浏览

使用svg中clipPath image标签 通过id 映射, 动态位置polygon的坐标,实现图片的截取
<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"
href="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg" rel="external nofollow"
width="300"
height="300"
preserveAspectRatio="none"
style="clip-path: url(#clippath)"
></image>
</svg>
<!-- 拖拽点 -->
<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>
<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: { // 初始化拖拽点
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: {
//鼠标按下
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;
},
// 鼠标抬起
mouseup(e) {
this.status = false;
},
// 鼠标移动
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(' ');
}
},
},
};

关于vue实现无规则截图功能就介绍到这,上述有具体的代码,有需要的朋友可以借鉴参考,希望能对大家学习vue功能有帮助,想要了解更多使用vue实现截图功能的内容,大家可以关注群英网络其它相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是有关nodejs错误处理过程的内容。小编觉得挺实用的,下文有具体实例可以个参考,感兴趣的朋友不妨跟随小编一起看看吧。
uniapp自定义弹框的方法 本文实例为大家分享了uniapp自定义弹框,适用所有类型,供大家参考,具体内容如下 效果原理 利用透明页面,点击进入当前页面,内容根据自己需求去实现,随便自定义,出来的效果就是一个弹框的效果.解决的难题(原生tabbar中间按钮的弹框,升级弹框不能遮挡原生tabbar) 创建一个vue页面 <template> <view @click="close()" class="mask"> <view cla ...
jQuery跨域访问的解决方法及要点是什么?跨域访问一直都是程序员们头疼的问题,对于很多用户们来说都不知道如何解决,好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目,查阅了相关资料,算是解决了跨域问题..有必要记下来备忘.
//表示引入http模块varhttp=require('http');//接收两个参数,一个是request和response//request获取url传过来的信息//response给浏览器响应信息http.createServer(function(request,response){ //设置响应头 response.writeHead(200,{'C
Vue组件生命周期执行原理是什么?一些新手可能对Vue组件生命周期的内容不是很了解,对此这文章就给大家介绍一下Vue组件生命周期的内容,对大家学习Vue组件生命周期有一定的帮助,需要的朋友可以参考。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008