vue项目--图片自定义圈出热点区域,配置链接url
最近遇到一个需求,用户可以在页面上对某张图片进行自定义圈区域,然后类似一个文本输入框,可手动配置跳转的url;一个图片可以配置多个热点区域,还可进行增删改查,请问有前 辈做过此类需求吗?有没有好的插件推荐,或者demo可以参考,谢谢啦
let res= await DownloadService.get2(name);//封装的后台接口API(name是返回的文件名)
let blob= new Blob([res.data], {//res.data是后台返回的二进制文件
type: “application/vnd.ms-excel;”//定义文件类型
// type: “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8”
});
let url= URL.createObjectURL(blob);
let link= document.createElement(‘a’);
link.setAttribute(“href”, url);
link.setAttribute(“download”, name);
link.style.visibility = ‘hidden’;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
我可以给出一个思路作为参考,但是具体实现方式需要根据具体的项目情况和代码结构来决定。建议先理解思路,再考虑如何用具体的代码来实现。
思路: 1. 找到一个合适的插件来圈出热点区域,比如Vue-Crop,vue-image-annotator等等; 2. 给每个圈出的热点区域增加点击事件,触发编辑热点区域的弹窗,或者直接在页面上实现增删改查操作; 3. 在编辑热点区域的弹窗中,增加一个链接输入框,让用户手动输入跳转链接; 4. 把圈出的热点区域的位置信息和跳转链接存储到对应的数据结构中,可以考虑用数组或者对象存储; 5. 在页面加载时,把存储的热点区域信息渲染到对应的图片上,可以考虑使用绝对定位把圈出的热点区域位置信息转换成样式表中的left、top、width、height属性; 6. 监听图片的点击事件,根据点击的位置判断用户是否点击了圈出的热点区域,如果点击了则跳转到对应的链接。
具体实现方式可以参考以下代码:
<vue-crop
:src="imageUrl"
@cropend="onCropEnd"
/>
<template v-for="(region, index) in hotspots">
<div :key="index" :style="region.style" @click="editHotspot(index)">
<span>{{ region.url }}</span>
</div>
</template>
data() {
return {
hotspots: [],
imageUrl: 'http://example.com/image.jpg'
}
},
methods: {
onCropEnd(coords) {
// convert coords to style object
const style = {
left: coords.x + 'px',
top: coords.y + 'px',
width: coords.width + 'px',
height: coords.height + 'px'
}
// add new hotspot to array
this.hotspots.push({
style: style,
url: ''
})
},
editHotspot(index) {
// open a dialog to edit the url of the hotspot
const url = prompt('Enter the URL:', this.hotspots[index].url)
if (url !== null) {
this.hotspots[index].url = url
}
}
}
<div class="image-wrapper">
<img :src="imageUrl" @click="onImageClick">
<template v-for="(region, index) in hotspots">
<div :key="index" :style="region.style"></div>
</template>
</div>
请注意:为了实现绝对定位,需要把图片和热点区域都放到一个DIV容器中,并用CSS对该容器进行定位。对于热点区域的样式,比如背景颜色、透明度等,可以根据需要自行设定。
methods: {
onImageClick(event) {
// get click position relative to the image
const x = event.clientX - event.target.offsetLeft
const y = event.clientY - event.target.offsetTop
// search through hotspots to see if the click is inside one of them
for (let i = 0; i < this.hotspots.length; i++) {
const region = this.hotspots[i]
if (x >= parseFloat(region.style.left)
&& x <= parseFloat(region.style.left) + parseFloat(region.style.width)
&& y >= parseFloat(region.style.top)
&& y <= parseFloat(region.style.top) + parseFloat(region.style.height)
) {
// open the url in a new window
window.open(region.url)
return
}
}
}
}
以上代码仅供参考,具体实现方式需要根据具体需求和实际情况进行调整。
有点牛逼这个需求
我知道得用canvas才能实现你说的 我不会写